Cannot truncate table because it is being referenced by a FOREIGN KEY constraint? Cannot truncate table because it is being referenced by a FOREIGN KEY constraint? sql-server sql-server

Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?


Correct; you cannot truncate a table which has an FK constraint on it.

Typically my process for this is:

  1. Drop the constraints
  2. Trunc the table
  3. Recreate the constraints.

(All in a transaction, of course.)

Of course, this only applies if the child has already been truncated. Otherwise I go a different route, dependent entirely on what my data looks like. (Too many variables to get into here.)

The original poster determined WHY this is the case; see this answer for more details.


DELETE FROM TABLENAMEDBCC CHECKIDENT ('DATABASENAME.dbo.TABLENAME',RESEED, 0)

Note that this isn't probably what you'd want if you have millions+ of records, as it's very slow.


Because TRUNCATE TABLE is a DDL command, it cannot check to see whether the records in the table are being referenced by a record in the child table.

This is why DELETE works and TRUNCATE TABLE doesn't: because the database is able to make sure that it isn't being referenced by another record.