Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question.

Asked: March 15, 2017 - 7:46 am UTC

Last updated: March 28, 2017 - 12:48 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi,

I loaded data into a table referenced by other table(Pk, Fk Relation and No cascade delete) via sql loader. the data loaded successfully after using "replace into table" statement, before i tried truncate,append,insert statements but data not loaded,thrown error about violation of referential integrity. then i read Oracle Docs about insert,append,Truncate and Replace,every statement has its own functionality. but in that they explained about truncate and replace, as truncate truncate the table and loads data but replace delete the table and loads the data. but with delete statement also we can not delete the data having Child records, so my question is
How is this possible with replace,why with not others like Truncate,Insert?

I hope i provided sufficient information.

Thank you in advance

and Connor said...

Because 'replace' is equivalent to a delete, and 'truncate' is a truncate.

So this is not really a SQL Loader issue, but just part how those statements work in the database.

For example

SQL> create table t1 ( x int primary key );

Table created.

SQL> create table t2 ( y int, x int references t1(x));

Table created.

SQL>
SQL> insert into t1 values (1);

1 row created.

SQL> delete from t1;

1 row deleted.



So a delete worked, because there were no child records... Let us repeat now with a truncate.

SQL>
SQL> insert into t1 values (1);

1 row created.

SQL> truncate table t1;
truncate table t1
               *
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys


Truncate is not allowed - you would need to disable the constraint first, and then truncate. (Version 12c is better with this - you can do truncate cascade).

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

How to Delete Data from a table having child Records

A reader, March 27, 2017 - 6:30 am UTC

Hi,

Thank you for the answer.
i would like to load a data into a table having child records.
so i tried with REPLACE and TRUNCATE in control File. but got Constraint violation Error. so is this possible to load by dleting previous data from a table having Child records with SQLLOADER?
Connor McDonald
March 28, 2017 - 12:48 am UTC

Change your constraint to be "on delete cascade". Then a delete on the parent table will delete the children as well.

If you want to *keep* the child records but just replace the parent records, you would need to

- disable the constraint
- do the parent load (with replace or truncate)
- enable the constraint