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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pravin MorePosted Nov 16, 2011, 1:40 AM
Akash AhlawatPosted Nov 16, 2011, 1:25 AM
AartiPosted Nov 15, 2011, 6:55 AM
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.
Pravin MorePosted Nov 15, 2011, 1:28 AM
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.
TulasiPosted Nov 15, 2011, 12:58 AM
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 .