问题
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