Primary Key Resetting Issue with foreign keys and delete

半城伤御伤魂 提交于 2020-01-22 02:50:09

问题


I have a set of tables which I am going to clear out and upload new data into. One of these, Person has foreign keys pointing to it which prevent me from using TRUNCATE Table even though the other tables are empty.

I have used DELETE FROM after turning off the foreign key checks to get around this. This works except when I insert new values they start at the old value going up and I need them to reset to start at 1 again (or at least some consistent predictable value)

DBCC CHECKIDENT ([Person], RESEED, -1); or DBCC CHECKIDENT ([Person], RESEED, 0); I have seen suggested on other places for resetting the identity but give no useful results for me instead yielding:

Checking identity information: current identity value 'NULL'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

How can I either get truncate to work with the foreign keys pointing to my table OR using delete to clear the table get the primary key's auto increment to reset and start at 1?


回答1:


You cannot truncate a table, if there are foreign keys pointing to it. One way would be, to temporarily drop the foreign keys and after truncating recreate them.

Disabling foreign key constraint, still can't truncate table? (SQL Server 2005)




回答2:


The DBCC command should work. I have removed the square brackets and added single quotes:

DBCC CHECKIDENT ('dbo.Person', RESEED, 0);


来源:https://stackoverflow.com/questions/29994645/primary-key-resetting-issue-with-foreign-keys-and-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!