简体   繁体   English

SQL Server复合主键

[英]SQL Server Composite Primary Keys

I am attempting to replace all records for a give day in a certain table. 我正在尝试替换特定表中给定日期的所有记录。 The table has a composite primary key comprised of 7 fields. 该表具有由7个字段组成的复合主键。 One such field is date. 这样的字段之一就是日期。

I have deleted all records which have a date value of 2/8/2010. 我删除了日期值为2/8/2010的所有记录。 When I try to then insert records into the table for 2/8/2010, I get a primary key violation. 当我尝试在2010年2月8日将记录插入表中时,出现主键冲突。 The records I am attempting to insert are only for 2/8/2010. 我尝试插入的记录仅适用于2/8/2010。

Since date is a component of the PK, shouldn't there be no way to violate the constraint as long as the date I'm inserting is not already in the table? 由于date是PK的组成部分,因此只要表中还没有我要插入的日期,就应该没有办法违反约束吗?

Thanks in advance. 提前致谢。

You could have duplicates in the data you are inserting. 您插入的数据中可能有重复项。

Also, it is a very, very, very poor practice to have a primary key that consists of 7 fields. 同样,拥有一个由7个字段组成的主键是一种非常非常非常糟糕的做法。 The proper way to handle this is to havea surrogate identity key and a unique index on the seven fields. 处理此问题的正确方法是在七个字段上具有代理身份密钥和唯一索引。 Joining to child tables on 7 feilds is a guarantee of poor performance and updating records when they have child records becomes a nightmare and can completely lock up your system. 在7个字段上加入子表可以保证性能不佳,当拥有子记录时更新记录成为噩梦,并且可以完全锁定您的系统。 A primary key should be unique adnit should NEVER change. 主键应该是唯一的,并且永不更改。

Do all the rows have only a date component in that field (ie the time is always set to midnight: 00:00:00)? 是否所有行在该字段中仅具有日期部分(即,时间始终设置为午夜:00:00:00)? If not, you'll need to delete the rows >= 2/8/2010 and < 2/9/2010. 如果不是,则需要删除> = 2/8/2010和<2/9/2010行。

Also, are you sure you're not accidentally trying to insert two records with the same date (and same values in the other 6 PK fields)? 另外,您确定您不会意外地插入两个具有相同日期(其他6个PK字段中具有相同值)的记录吗?

Perhaps there's something going on here you're not aware of. 也许这里发生了您不知道的事情。 When you insert a row and get a primary key violation, try doing a SELECT with the appropriate key values from the row which could not be inserted (after doing a ROLLBACK, of course) and see what you get. 当您插入行并遇到主键冲突时,请尝试使用无法插入的行中的适当键值进行SELECT(当然,在执行ROLLBACK之后),然后看看您能得到什么。 Or perhaps there's a trigger on the table into which you're inserting data that is inserting rows into another table which uses the same primary key but was not cleaned out. 也许在表上有一个触发器,您正在其中插入数据,该触发器正在将行插入到使用相同主键但未被清除的另一个表中。

You might try the following SELECT to see what turns up: 您可以尝试以下SELECT来查看结果:

SELECT * FROM YOUR_TABLE WHERE DATE > 2/7/2010 AND DATE < 2/9/2010; 在日期> 2/7/2010和日期<2/9/2010中从您的表中选择*;

(Not sure about the proper format for a date constant in SQL Server as I haven't used it in a few years, but I'm sure you get the idea). (由于几年来我都没有使用过,所以不确定SQL Server中日期常量的格式是否正确,但是我敢肯定,您会明白的。) See what you get. 看你得到什么。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM