Sie sind auf Seite 1von 4

================================================================================

========
ORA-02292: integrity constraint (HR.FK_SUPPLIER) violated - child record found
================================================================================
========
SQL> CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
Table created.
SQL> CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier (supplier_id)
);
Table created.
SQL> INSERT INTO supplier
(supplier_id, supplier_name, contact_name)
VALUES (1000, 'Microsoft', 'Bill Gates');
1 row created.
SQL> INSERT INTO products
(product_id, supplier_id)
VALUES (50000, 1000);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from supplier;
SUPPLIER_ID SUPPLIER_NAME
CONTACT_NAME
----------- -------------------------------------------------- ---------------------1000 Microsoft
Bill Gates
SQL> select * from products;
PRODUCT_ID SUPPLIER_ID
---------- ----------50000
1000
================================================================================
=================
SQL> DELETE from supplier
WHERE supplier_id = 1000;

DELETE from supplier


*
ERROR at line 1:
ORA-02292: integrity constraint (HR.FK_SUPPLIER) violated - child record found
================================================================================
====================

SQL> select OWNER,TABLE_NAME,COLUMN_NAME,POSITION,CONSTRAINT_NAME from user_cons


_columns where Table_name='SUPPLIER';
OWNER

TABLE_NAME
COLUMN_NAME
POSITION CONSTRAINT_NAME
------------------------------ ------------------------------ --------------------------------------- ---------- -----------------------------HR
SUPPLIER
SUPPLIER_ID
SYS_C0011787
HR
SUPPLIER
SUPPLIER_NAME
SYS_C0011788
HR
SUPPLIER
SUPPLIER_ID
1 SUPPLIER_PK
SQL> select OWNER,TABLE_NAME,COLUMN_NAME,POSITION,CONSTRAINT_NAME from user_cons
_columns where Table_name='PRODUCTS';
OWNER

TABLE_NAME
COLUMN_NAME
POSITION CONSTRAINT_NAME
------------------------------ ------------------------------ --------------------------------------- ---------- -----------------------------HR
PRODUCTS
PRODUCT_ID
SYS_C0011790
HR
PRODUCTS
SUPPLIER_ID
SYS_C0011791
HR
PRODUCTS
SUPPLIER_ID
1 FK_SUPPLIER
SQL> COL constraint_source FORMAT A38 HEADING "Constraint Name:| Table.Column"
COL references_column FORMAT A38 HEADING "References:| Table.Column"
SQL> SQL>
SQL> SELECT uc.constraint_name||CHR(10)
2 ||
'('||ucc1.TABLE_NAME||'.'||ucc1.column_name||')' constraint_source
3 ,
'REFERENCES'||CHR(10)
4 ||
'('||ucc2.TABLE_NAME||'.'||ucc2.column_name||')' references_column
5 FROM
user_constraints uc
6 ,
user_cons_columns ucc1
7 ,
user_cons_columns ucc2
8 WHERE
uc.constraint_name = ucc1.constraint_name
9 AND
uc.r_constraint_name = ucc2.constraint_name
10 AND
uc.constraint_type = 'R'
11 AND
uc.constraint_name = UPPER('&input_constraint_name');
Enter value for input_constraint_name: FK_SUPPLIER
old 11: AND
uc.constraint_name = UPPER('&input_constraint_name')
new 11: AND
uc.constraint_name = UPPER('FK_SUPPLIER')
Constraint Name:
Table.Column
-------------------------------------FK_SUPPLIER

References:
Table.Column
-------------------------------------REFERENCES

(PRODUCTS.SUPPLIER_ID)

(SUPPLIER.SUPPLIER_ID)

================================================================================
===============
SQL> DELETE from products WHERE supplier_id = 1000;
1 row deleted.
SQL> DELETE from supplier WHERE supplier_id = 1000;
1 row deleted.
SQL> select * from products;
no rows selected
SQL> select * from supplier;
no rows selected
================================================================================
=================
SQL> delete from departments where department_name='Finance';
delete from departments where department_name='Finance'
*
ERROR at line 1:
ORA-02292: integrity constraint (HR.EMP_DEPT_FK) violated - child record found
SQL> COL constraint_source FORMAT A38 HEADING "Constraint Name:| Table.Column"
COL references_column FORMAT A38 HEADING "References:| Table.Column"
SQL> SELECT uc.constraint_name||CHR(10)
2 ||
'('||ucc1.TABLE_NAME||'.'||ucc1.column_name||')' constraint_source
3 ,
'REFERENCES'||CHR(10)
||
'('||ucc2.TABLE_NAME||'.'||ucc2.column_name||')' references_column
4
5 FROM
user_constraints uc
,
user_cons_columns ucc1
6
7 ,
user_cons_columns ucc2
8 WHERE
uc.constraint_name = ucc1.constraint_name
9 AND
uc.r_constraint_name = ucc2.constraint_name
10 AND
uc.constraint_type = 'R'
11 AND
uc.constraint_name = UPPER('&input_constraint_name');
Enter value for input_constraint_name: EMP_DEPT_FK
old 11: AND
uc.constraint_name = UPPER('&input_constraint_name')
new 11: AND
uc.constraint_name = UPPER('EMP_DEPT_FK')
Constraint Name:
Table.Column
-------------------------------------EMP_DEPT_FK
(EMPLOYEES.DEPARTMENT_ID)

References:
Table.Column
-------------------------------------REFERENCES
(DEPARTMENTS.DEPARTMENT_ID)

SQL> alter table employees disable constraint EMP_DEPT_FK;


Table altered.
SQL> delete from departments where department_name='Finance';

1 row deleted.

SQL> alter table employees enable constraint EMP_DEPT_FK;


Table altered.
SQL> select OWNER,TABLE_NAME,CONSTRAINT_NAME,CONSTRAINT_TYPE,STATUS from user_co
nstraints where table_name='EMPLOYEES';
OWNER
C STATUS
----------------------------------------- - -------HR
R ENABLED
HR
C ENABLED
HR
C ENABLED
HR
C ENABLED
HR
C ENABLED
HR
C ENABLED
HR
U ENABLED
HR
P ENABLED
HR
R ENABLED
HR
R ENABLED

TABLE_NAME

CONSTRAINT_NAME

------------------------------ -----------------EMPLOYEES

EMP_MANAGER_FK

EMPLOYEES

EMP_LAST_NAME_NN

EMPLOYEES

EMP_EMAIL_NN

EMPLOYEES

EMP_HIRE_DATE_NN

EMPLOYEES

EMP_JOB_NN

EMPLOYEES

EMP_SALARY_MIN

EMPLOYEES

EMP_EMAIL_UK

EMPLOYEES

EMP_EMP_ID_PK

EMPLOYEES

EMP_DEPT_FK

EMPLOYEES

EMP_JOB_FK

10 rows selected.
================================================================================
===================