You Asked
Tom ,
why truncating empty table generates the error ORA-02266 ?
SQL> select count(*) from t_data;
COUNT(*)
----------
0
SQL> truncate table t_data
*
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
SQL> delete t_data;
0 rows deleted.
Thanks,
Michael.
and Tom said...
You cannot truncate a table with an enabled foreign key that points to it. Truncate does not fire any triggers, does not validate any constraints. It does not care of the child table is empty or not -- in this case the child table might actually not be empty.
You would have to disable or drop the foreign keys that reference this table.
Here is an example:
ops$tkyte@DEV816> create table p ( x int primary key );
Table created.
ops$tkyte@DEV816> create table c ( y int primary key,
2 x int,
3 constraint c_fk foreign key(x) references p);
Table created.
ops$tkyte@DEV816> insert into c ( y ) values ( 1 );
1 row created.
ops$tkyte@DEV816> truncate table p;
truncate table p
*
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
ops$tkyte@DEV816> alter table c disable constraint c_fk;
Table altered.
ops$tkyte@DEV816> truncate table p;
Table truncated.
Now we can truncate since the foreign key is no longer enabled, all we need to do now is re-enable it which will also check it and make sure none of the rows in C point to a row in P
ops$tkyte@DEV816> alter table c enable constraint c_fk;
Table altered.
Alternatively, you can run a query to find all of the foriegn key constraints and generate an "alter table" command that would rebuild them. In this fashion, you can drop them, run the truncate and the rebuild them. This is handy if you do not know the constraints before hand -- it'll show you what they are:
ops$tkyte@DEV816> column fkey format a80 word_wrapped
ops$tkyte@DEV816> select
2 'alter table "' || child_tname || '"' || chr(10) ||
3 'add constraint "' || child_cons_name || '"' || chr(10) ||
4 'foreign key ( ' || child_columns || ' ) ' || chr(10) ||
5 'references "' || parent_tname || '" ( ' ||
parent_columns || ');' fkey
6 from
7 ( select a.table_name child_tname, a.constraint_name
child_cons_name,
8 b.r_constraint_name parent_cons_name,
9 max(decode(position, 1, '"'||
10 substr(column_name,1,30)||'"',NULL)) ||
11 max(decode(position, 2,', '||'"'||
12 substr(column_name,1,30)||'"',NULL)) ||
13 max(decode(position, 3,', '||'"'||
14 substr(column_name,1,30)||'"',NULL)) ||
15 max(decode(position, 4,', '||'"'||
16 substr(column_name,1,30)||'"',NULL)) ||
17 max(decode(position, 5,', '||'"'||
18 substr(column_name,1,30)||'"',NULL)) ||
19 max(decode(position, 6,', '||'"'||
20 substr(column_name,1,30)||'"',NULL)) ||
21 max(decode(position, 7,', '||'"'||
22 substr(column_name,1,30)||'"',NULL)) ||
23 max(decode(position, 8,', '||'"'||
24 substr(column_name,1,30)||'"',NULL)) ||
25 max(decode(position, 9,', '||'"'||
26 substr(column_name,1,30)||'"',NULL)) ||
27 max(decode(position,10,', '||'"'||
28 substr(column_name,1,30)||'"',NULL)) ||
29 max(decode(position,11,', '||'"'||
30 substr(column_name,1,30)||'"',NULL)) ||
31 max(decode(position,12,', '||'"'||
32 substr(column_name,1,30)||'"',NULL)) ||
33 max(decode(position,13,', '||'"'||
34 substr(column_name,1,30)||'"',NULL)) ||
35 max(decode(position,14,', '||'"'||
36 substr(column_name,1,30)||'"',NULL)) ||
37 max(decode(position,15,', '||'"'||
38 substr(column_name,1,30)||'"',NULL)) ||
39 max(decode(position,16,', '||'"'||
40 substr(column_name,1,30)||'"',NULL))
41 child_columns
42 from user_cons_columns a, user_constraints b
43 where a.constraint_name = b.constraint_name
44 and b.constraint_type = 'R'
45 group by a.table_name, a.constraint_name,
b.r_constraint_name ) child,
46 ( select a.constraint_name parent_cons_name, a.table_name
parent_tname,
47 max(decode(position, 1, '"'||
48 substr(column_name,1,30)||'"',NULL)) ||
49 max(decode(position, 2,', '||'"'||
50 substr(column_name,1,30)||'"',NULL)) ||
51 max(decode(position, 3,', '||'"'||
52 substr(column_name,1,30)||'"',NULL)) ||
53 max(decode(position, 4,', '||'"'||
54 substr(column_name,1,30)||'"',NULL)) ||
55 max(decode(position, 5,', '||'"'||
56 substr(column_name,1,30)||'"',NULL)) ||
57 max(decode(position, 6,', '||'"'||
58 substr(column_name,1,30)||'"',NULL)) ||
59 max(decode(position, 7,', '||'"'||
60 substr(column_name,1,30)||'"',NULL)) ||
61 max(decode(position, 8,', '||'"'||
62 substr(column_name,1,30)||'"',NULL)) ||
63 max(decode(position, 9,', '||'"'||
64 substr(column_name,1,30)||'"',NULL)) ||
65 max(decode(position,10,', '||'"'||
66 substr(column_name,1,30)||'"',NULL)) ||
67 max(decode(position,11,', '||'"'||
68 substr(column_name,1,30)||'"',NULL)) ||
69 max(decode(position,12,', '||'"'||
70 substr(column_name,1,30)||'"',NULL)) ||
71 max(decode(position,13,', '||'"'||
72 substr(column_name,1,30)||'"',NULL)) ||
73 max(decode(position,14,', '||'"'||
74 substr(column_name,1,30)||'"',NULL)) ||
75 max(decode(position,15,', '||'"'||
76 substr(column_name,1,30)||'"',NULL)) ||
77 max(decode(position,16,', '||'"'||
78 substr(column_name,1,30)||'"',NULL))
79 parent_columns
80 from user_cons_columns a, user_constraints b
81 where a.constraint_name = b.constraint_name
82 and b.constraint_type in ( 'P', 'U' )
83 group by a.table_name, a.constraint_name ) parent
84 where child.parent_cons_name = parent.parent_cons_name
85 and parent.parent_tname = upper('&1')
86 /
old 85: and parent.parent_tname = upper('&1')
new 85: and parent.parent_tname = upper('p')
FKEY
---------------------------------------------------------
alter table "C"
add constraint "C_FK"
foreign key ( "X" )
references "P" ( "X");
Rating
(5 ratings)
Is this answer out of date? If it is, please let us know via a Comment