合并到 table 时违反约束
Constraint violation when merging into table
我有一个暂存 table 和一个数据仓库 table,它们一直给我 constraint violation
。我似乎无法弄清楚为什么 DRIVERID
和 RACEID
它们的组合应该是唯一的?我怎么会得到contraint violation - primary key
table
CREATE TABLE QUALIFYING (
QUALIFYID DECIMAL(18,0) IDENTITY NOT NULL,
RACEID DECIMAL(18,0) DEFAULT '0' NOT NULL,
DRIVERID DECIMAL(18,0) DEFAULT '0' NOT NULL,
CONSTRUCTORID DECIMAL(18,0) DEFAULT '0' NOT NULL,
DRIVERNUMBER DECIMAL(18,0) DEFAULT '0' NOT NULL,
DRIVERPOSITION DECIMAL(18,0) DEFAULT NULL,
Q1 VARCHAR(255) UTF8 DEFAULT NULL,
Q2 VARCHAR(255) UTF8 DEFAULT NULL,
Q3 VARCHAR(255) UTF8 DEFAULT NULL,
PRIMARY KEY(QUALIFYID)
);
分期
CREATE OR REPLACE TABLE STGQUALIFYING(
raceId int DEFAULT '0' NOT NULL,
driverId int DEFAULT '0' NOT NULL,
constructorId int DEFAULT '0' NOT NULL,
driverNumber int DEFAULT '0' NOT NULL,
driverPosition int DEFAULT NULL,
q1 varchar(255) DEFAULT NULL,
q2 varchar(255) DEFAULT NULL,
q3 varchar(255) DEFAULT NULL,
PRIMARY KEY(RACEID, DRIVERID)
);
SQL
MERGE INTO QUALIFYING c
USING STGQUALIFYING n
ON
(n.RACEID = c.RACEID AND n.DRIVERID = c.DRIVERID)
WHEN MATCHED THEN
UPDATE SET
CONSTRUCTORID = n.CONSTRUCTORID, DRIVERNUMBER = n.DRIVERNUMBER, DRIVERPOSITION = n.DRIVERPOSITION, Q1 = n.Q1, Q2 = n.Q2, Q3 = n.Q3
WHEN NOT MATCHED THEN
INSERT (RACEID, DRIVERID, CONSTRUCTORID, DRIVERNUMBER, DRIVERPOSITION, Q1, Q2, Q3) VALUES
(RACEID, DRIVERID, CONSTRUCTORID, DRIVERNUMBER, DRIVERPOSITION, Q1, Q2, Q3);
The EXASolution user manual 说:
The content of an identity column applies to the following rules:
- If you specify an explicit value for the identity column while inserting a row, then this value is inserted.
- In all other cases monotonically increasing numbers are generated by the system, but gaps can occur between the numbers.
和
You should not mistake an identity column with a constraint, i.e. identity columns do not guarantee unique values. But the values are unique as long as values are inserted only implicitly and are not changed manually.
您在标识列上设置了主键约束,因此它必须是唯一的。由于您从合并中获得重复项,或者 (a) 在某些时候,您已经提供了上面第一个项目符号中的显式值或手动更新了一个值,并且单调递增序列已经达到与那些冲突的点现有价值;或者 (b) 他们的 merge
中有错误。前者似乎更有可能。
您可以查看最近插入的值(如果有的话),或者临时插入一个新行(使用合并)以查看它是否会成功创建一行,如果是的话,您是否已经拥有更高的 ID 值比它为该新行分配的那个。如果已经没有更高的值,并且插入工作和合并继续持续失败,那么这听起来像是您需要使用 EXASolution 提出的问题。
我有一个暂存 table 和一个数据仓库 table,它们一直给我 constraint violation
。我似乎无法弄清楚为什么 DRIVERID
和 RACEID
它们的组合应该是唯一的?我怎么会得到contraint violation - primary key
table
CREATE TABLE QUALIFYING (
QUALIFYID DECIMAL(18,0) IDENTITY NOT NULL,
RACEID DECIMAL(18,0) DEFAULT '0' NOT NULL,
DRIVERID DECIMAL(18,0) DEFAULT '0' NOT NULL,
CONSTRUCTORID DECIMAL(18,0) DEFAULT '0' NOT NULL,
DRIVERNUMBER DECIMAL(18,0) DEFAULT '0' NOT NULL,
DRIVERPOSITION DECIMAL(18,0) DEFAULT NULL,
Q1 VARCHAR(255) UTF8 DEFAULT NULL,
Q2 VARCHAR(255) UTF8 DEFAULT NULL,
Q3 VARCHAR(255) UTF8 DEFAULT NULL,
PRIMARY KEY(QUALIFYID)
);
分期
CREATE OR REPLACE TABLE STGQUALIFYING(
raceId int DEFAULT '0' NOT NULL,
driverId int DEFAULT '0' NOT NULL,
constructorId int DEFAULT '0' NOT NULL,
driverNumber int DEFAULT '0' NOT NULL,
driverPosition int DEFAULT NULL,
q1 varchar(255) DEFAULT NULL,
q2 varchar(255) DEFAULT NULL,
q3 varchar(255) DEFAULT NULL,
PRIMARY KEY(RACEID, DRIVERID)
);
SQL
MERGE INTO QUALIFYING c
USING STGQUALIFYING n
ON
(n.RACEID = c.RACEID AND n.DRIVERID = c.DRIVERID)
WHEN MATCHED THEN
UPDATE SET
CONSTRUCTORID = n.CONSTRUCTORID, DRIVERNUMBER = n.DRIVERNUMBER, DRIVERPOSITION = n.DRIVERPOSITION, Q1 = n.Q1, Q2 = n.Q2, Q3 = n.Q3
WHEN NOT MATCHED THEN
INSERT (RACEID, DRIVERID, CONSTRUCTORID, DRIVERNUMBER, DRIVERPOSITION, Q1, Q2, Q3) VALUES
(RACEID, DRIVERID, CONSTRUCTORID, DRIVERNUMBER, DRIVERPOSITION, Q1, Q2, Q3);
The EXASolution user manual 说:
The content of an identity column applies to the following rules:
- If you specify an explicit value for the identity column while inserting a row, then this value is inserted.
- In all other cases monotonically increasing numbers are generated by the system, but gaps can occur between the numbers.
和
You should not mistake an identity column with a constraint, i.e. identity columns do not guarantee unique values. But the values are unique as long as values are inserted only implicitly and are not changed manually.
您在标识列上设置了主键约束,因此它必须是唯一的。由于您从合并中获得重复项,或者 (a) 在某些时候,您已经提供了上面第一个项目符号中的显式值或手动更新了一个值,并且单调递增序列已经达到与那些冲突的点现有价值;或者 (b) 他们的 merge
中有错误。前者似乎更有可能。
您可以查看最近插入的值(如果有的话),或者临时插入一个新行(使用合并)以查看它是否会成功创建一行,如果是的话,您是否已经拥有更高的 ID 值比它为该新行分配的那个。如果已经没有更高的值,并且插入工作和合并继续持续失败,那么这听起来像是您需要使用 EXASolution 提出的问题。