Solved

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

Posted on 2001-06-25
3
1,501 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

776 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