Solved

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

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

Accepted Solution

by:
Mark Geerlings earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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.

Join & Write a Comment

Suggested Solutions

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
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…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now