首页 > 解决方案 > Adding data to existing table with primary key violates unique constraint despite being unique?

问题描述

I am attempting to add new data to the database I have created, however, I keep getting an error "ORA-00001" when adding to the table despite the data within the PK column being unique.

How am I able to add new data to the primary column, or do constraints need to be disabled every time?

I was attempting to do so via:

UPDATE Specialists 
SET Specialist_ID = 210000000005, 
    First_Name = 'Aaron', 
    Last_Name = 'Black', 
    Specialist_Category = 'Software', 
    Cases_In_Progress = 0;

I have Googled as specifically as I can to narrow down search results but I haven't been able to find the answer to just add new data into the PK column of an existing table.

Much appreciated!

标签: sqloracleprimary-key

解决方案


您的代码显然违反了primary key约束。这是出于以下两个原因之一:

  • 已经有一行主键具有您为其分配的值。
  • 您的更新更新不止一行。

我会选择后者。

假设这Specialist_ID是主键,您可能打算

UPDATE Specialists
    SET First_Name = 'Aaron',
        Last_Name = 'Black',
        Specialist_Category = 'Software',
        Cases_In_Progress = 0
    WHERE Specialist_ID = 210000000005;

在任何情况下,UPDATE没有WHERE子句的简单语句都是可疑的,因为它会更新表中的所有行。


推荐阅读