Solved

Unable to truncate a table referenced by other tables using foreign keys

Posted on 2001-06-25
3
1,514 Views
Last Modified: 2008-03-10
Table A has userid as a primary key, table B has userid as a primary key and references A.userid thru a foreign key, even if both tables are empty, I cannot truncate table A, why ?
I can delete table A by setting 'on delete cascade' option in the foreign key constraint, is
there a similar option for truncate ?
0
Comment
Question by:AtulPadhye
3 Comments
 
LVL 35

Accepted Solution

by:
Mark Geerlings earned 50 total points
ID: 6225345
Why can you not truncate a parent table?  Here is the quote from the Oracle8.1.6 documentation:

"You cannot truncate the parent table of an enabled referential integrity constraint. You must disable the constraint before truncating the table. (An exception is that you may truncate the table if the integrity constraint is self-referential.)"

No, I don't know of a way around this.  In fact, just disabling the foreign key constraint is not always enough.  I usually need to drop the foreign key constraint(s) first, then truncate the table and re-create the constraint(s), if they need to be in place.

0
 

Expert Comment

by:bbchang97
ID: 6225351
It won't allow u to delete the master records if child exists.  Since, without the master record, it's a violation for the forigen key constraints.

By using cascade delete, it's basically remove all child records related to the master row.

Ususally, if I'm trying to clean up both master and detail tables by using 'TRUNCATE' statement, I'll disable the foreign key ('ALTER TABLE XXX disable contraints FK_XXX'). I could then truncate both tables.  Afterwards, I could enable the foreign key again.

Hope this helps.
0
 
LVL 1

Expert Comment

by:ashokskumar
ID: 6225358
Hi,

As Delete handles row by row, it does with cascade option.
Truncate option handles on entire table. You should disable the foreign key constraint and try truncate.

Ashok.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to take different types of Oracle backups using RMAN.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question