Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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 shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

920 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

15 Experts available now in Live!

Get 1:1 Help Now