ORA-02266: A unique/primary key-enabled foreign key reference in the table

Source: Internet
Author: User
    • Summary

When deleting data in a table with a parent-child relationship, we all know that you must delete the child table before deleting the data in the parent table.

Yesterday we used to delete the child table first, then delete the parent table, delete the use of truncate, the results in the deletion of the parent table, given the prompt: ORA-02266 this error message.

When the data in the parent table is deleted using the delete statement, the operation is successful.

The fundamental problem is: difference between DDL and DML (because truncate isn' t going to verify the constraint, truncate is DDL .)

The following operations are required:

Cancel the foreign key constraint of the child table foreign key, delete the parent table data, and enable the foreign key constraint again.

    • Prepare the Parent and Child tables
 --  Create a person table  Create     Table  Person (PID  Integer   Primary   Key , Pname  Varchar2 ( 10 ) Not   Null  ) Tablespace Cici;  --  Create Department  Create   Table  Department (depid  Integer   Primary   Key  , Deptname Varchar2 ( 10 ) Not   Null  ) Tablespace Cici;  --  Add Field  Alter   Table Person Add Depid Integer  ;  --  Add a foreign key constraint person --> Department  -- Deptid --> deptid  Alter   Table  Person  Add   Constraint  Fk_person_dept  Foreign   Key (Depid) References Department (depid );
    • Run the following statement when the problem occurs:
Truncate TablePerson;
-- Execution successful
Truncate TableDepartment;
-- Encountered error message: ORA-02266: Unique/foreign key reference enabled in the table
    • Problem Solving

The FK foreign key constraint of the distable sub-Table. After the data is deleted, entable again;

1. query foreign key constraints

Select * fromAll_constraints
Where constraint_type = 'r' and owner = 'ss _ hr ';

The constraint_name is fk_person_dept.

2. The foreign key constraint of the invalid sub-table

Alter table person disable ConstraintFk_person_dept;

3. Delete the data in the parent table again.

 
Truncate TableDepartment;

4. Foreign key constraints

 
Alter TablePerson enableConstraintFk_person_dept

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.