在 SQL 中插入时出现唯一键约束错误
Unique key constraint error while inserting in SQL
有两个 table x_ass_table
和 i_ass_table
:
我正在将包含 13000 条记录的 x_ass_table
中的数据插入到 i_ass_table:
i_ass_table
中存在唯一约束
CREATE UNIQUE INDEX i_ass_table_pk ON i_ass_table(ASSIGNMENT_NUMBER,EFFECTIVE_START_DATE,EFFECTIVE_END_DATE,EFFECTIVE_LATEST_CHANGE)
insert into i_ass_table select * from x_ass_table;
我收到唯一约束错误
00001. 00000 - "unique constraint (%s.%s) violated"
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.
这一定是数据有误。但是我如何检查 13000 条记录中哪些数据损坏或重复
您可以使用 count()
函数的分析版本通过您希望唯一的字段组合对源行进行编号,然后查询以查看哪个组合具有多个行。例如:
SELECT *
FROM (SELECT *,
COUNT() OVER (PARTITION BY assignment_number,
effective_start_date,
effective_end_date,
effective_latest_change) AS c
FROM x_ass_table) t
WHERE c > 1
你可以试试
SELECT * FROM x_ass_table GROUP BY ASSIGNMENT_NUMBER HAVING COUNT(*)>1
如您所问,您必须检查两个 table 是否存在唯一约束冲突。
- 首先检查
x_ass_table
是否有任何基于唯一键的重复记录,您可以使用 Mureinik 在上述答案中提供的查询进行检查。
其次,您必须根据唯一键检查 table 中是否存在相同的记录,这也会造成唯一约束冲突。您可以使用以下查询进行检查。
select *
from x_ass_table x,
i_ass_table i
where i.assignment_number = x.assignment_number
and i.effective_start_date = x.effective_start_date
and i.effective_end_date = x.effective_end_date
and i.effective_latest_change = x.effective_latest_change;
有两个 table x_ass_table
和 i_ass_table
:
我正在将包含 13000 条记录的 x_ass_table
中的数据插入到 i_ass_table:
i_ass_table
CREATE UNIQUE INDEX i_ass_table_pk ON i_ass_table(ASSIGNMENT_NUMBER,EFFECTIVE_START_DATE,EFFECTIVE_END_DATE,EFFECTIVE_LATEST_CHANGE)
insert into i_ass_table select * from x_ass_table;
我收到唯一约束错误
00001. 00000 - "unique constraint (%s.%s) violated"
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.
这一定是数据有误。但是我如何检查 13000 条记录中哪些数据损坏或重复
您可以使用 count()
函数的分析版本通过您希望唯一的字段组合对源行进行编号,然后查询以查看哪个组合具有多个行。例如:
SELECT *
FROM (SELECT *,
COUNT() OVER (PARTITION BY assignment_number,
effective_start_date,
effective_end_date,
effective_latest_change) AS c
FROM x_ass_table) t
WHERE c > 1
你可以试试
SELECT * FROM x_ass_table GROUP BY ASSIGNMENT_NUMBER HAVING COUNT(*)>1
如您所问,您必须检查两个 table 是否存在唯一约束冲突。
- 首先检查
x_ass_table
是否有任何基于唯一键的重复记录,您可以使用 Mureinik 在上述答案中提供的查询进行检查。 其次,您必须根据唯一键检查 table 中是否存在相同的记录,这也会造成唯一约束冲突。您可以使用以下查询进行检查。
select * from x_ass_table x, i_ass_table i where i.assignment_number = x.assignment_number and i.effective_start_date = x.effective_start_date and i.effective_end_date = x.effective_end_date and i.effective_latest_change = x.effective_latest_change;