Truncate command on a table which is referenced by FOREIGN KEY?
Can we use Truncate command on a table which is referenced by FOREIGN KEY?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
TulasiRank #2,544 · 549 reputationPosted Nov 14, 2011, 7:58 PM EST
you cannot truncate a table which has an FK constraint on it.
one way is:
- Drop the constraints
- Trunc the table
- Recreate the constraints.
there is no other choise to truncate table which has FK Contraint on it .Pravin MoreRank #1,419 · 1.2K reputationPosted Nov 14, 2011, 8:28 PM EST
Yes but TRUNCATE won´t work while being referenced by Foreign key constraints. So first drop the constraints, truncate the table and recreate the constraints. The other option would be like delete the child records first and afterwards the content of the parent entitiy.
Thanks,
Pravin.
AartiRank #2,383 · 594 reputationPosted Nov 15, 2011, 1:55 AM EST
yes you can use truncate Command on a table which is referenced by Foreign Key by applying DELETE. Cascade If the
FOREIGN KEYconstraint specifiesDELETE CASCADE, rows from the child (referenced) table are deleted, and the truncated table becomes empty. If theFOREIGN KEYconstraint does not specifyCASCADE, the Truncate table statement deletes rows one by one and stops if it encounters a parent row that is referenced by the child, returning this error.Thanks.
Akash AhlawatRank #3,915 · 379 reputationPosted Nov 15, 2011, 8:25 PM EST
Pravin MoreRank #1,419 · 1.2K reputationPosted Nov 15, 2011, 8:40 PM EST