0

I have a table called Folder with 3 fields: Id (PK), Name, UserId (FK)

and another table called User with two fields: Id (PK), UserName

i am trying to run this sql:

truncate table Folder
truncate table User

i get the following error

Cannot truncate table 'Folder' because it is being referenced by a FOREIGN KEY constraint.
Cannot truncate table 'user'   because it is being referenced by a FOREIGN KEY constraint.

...even after deleting all the records in both tables i get this. why am i not allowed to truncate my tables?

how can i reset the seeds of the PK's easily without using truncate if there is now way to run a truncate on pk/fk relationship tables?

Edit: DB diagram added enter image description here

2
  • It looks like Folder is also being used as an FK in another table. That table would need to be dumped first?
    – Kevin Peno
    Commented May 2, 2011 at 19:01
  • all the tables in the DB have no records.
    – kacalapy
    Commented May 2, 2011 at 19:07

1 Answer 1

7

To reseed, use DBCC CHECKIDENT

DBCC CHECKIDENT ('table', RESEED, 1)

Or you'd have to drop the FK, truncate, then recreate the FK.

This is by design: TRUNCATE TABLE is simple page deallocation and constraints are not checked row by row. This limitation means you can't truncate when there are FKs.. even if you disable the FK.

2
  • thanks, but i dont see why i cant truncate tables. i am sure this is possible even with constraints being used.
    – kacalapy
    Commented May 2, 2011 at 19:08
  • 2
    I just told you why: there are foreign keys on the table. msdn.microsoft.com/en-us/library/ms177570.aspx
    – gbn
    Commented May 2, 2011 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.