oracle 插入语句试图包含重复键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19817731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:04:04  来源:igfitidea点击:

Insert statement attempted to include a duplicate key

sqldatabaseoraclesyntaxsyntax-error

提问by WhereAreYouSyntax

Trying to run

试图运行

INSERT INTO BOOKING_EXTRAS (BOOKING_ID, EXTRAS_, EXTRAS_PRICE) VALUES ('1','Phone call: 1.80','1.8');

in Oracle SQL Developer. Ive had it running but when I close it, then reopen it I get this error:

在 Oracle SQL Developer 中。我已经运行了,但是当我关闭它,然后重新打开它时,我收到此错误:

Error starting at line 1 in command:
INSERT INTO BOOKING_EXTRAS (BOOKING_ID, EXTRAS_, EXTRAS_PRICE) VALUES ('1','Phone call: 1.80','1.8')
Error report:
SQL Error: ORA-00001: unique constraint (COURSEWORK_XE.BOOKING_EXTRAS_PK) violated
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.

how would I fix this? Its happening to every table I run!

我将如何解决这个问题?它发生在我运行的每张桌子上!

采纳答案by Tobberoth

You need to either clear the tables or insert new information, a database doesn't want duplicate rows because that makes it impossible to find the correct rows later.

您需要清除表或插入新信息,数据库不想要重复的行,因为这使得以后无法找到正确的行。

回答by Paul Lo

In addition, if that BOOKING_ID(currently serves as primary key if I guess right) doesn't actually mean something for you, you can set it as AUTO INCREMENT in your schema, then afterwards you don't need to insert a value for BOOKING_ID, the system will automatically find a value which is not duplicate for you. This might save you a lot effort.

此外,如果该BOOKING_ID(如果我猜对了,当前用作主键)实际上对您没有意义,您可以在架构中将其设置为 AUTO INCREMENT,然后您不需要为 BOOKING_ID 插入值,系统会自动为您找到一个不重复的值。这可能会为您节省很多精力。

INSERT INTO BOOKING_EXTRAS (EXTRAS_, EXTRAS_PRICE) VALUES ('Phone call: 1.80','1.8');

回答by thebadger2016

If your unique column is being populated by a sequence, check to make sure that your sequence has the same 'last value' as your highest-value unique column. I just encountered a problem where the sequence had a 'last value in' that was much lower than the highest value in my unique column. The DBA ran a script to update the sequence to what the number should have been, and my error went away.

如果您的唯一列由序列填充,请检查以确保您的序列具有与最高值唯一列相同的“最后一个值”。我刚刚遇到一个问题,其中序列的“最后一个值”远低于我的唯一列中的最高值。DBA 运行了一个脚本来将序列更新为应该有的数字,然后我的错误就消失了。

Now to find out why the sequence was so wrong...

现在找出为什么序列如此错误......