Add constraint foreign key references : FOREIGN KEY « Table « Oracle PL/SQL Tutorial






SQL>
SQL> create table parts
  2  (part_no   number primary key,
  3   part_name varchar2(20),
  4   parent number);

Table created.

SQL>
SQL> insert into parts
  2  values (4, 'part1' , 3)
  3  /

1 row created.

SQL>
SQL> insert into parts
  2  values (5, 'part2' , 3)
  3  /

1 row created.

SQL>
SQL> insert into parts
  2  values (6, 'part3' , 3)
  3  /

1 row created.

SQL>
SQL> insert into parts
  2  values (7, 'part4' , 6)
  3  /

1 row created.

SQL>
SQL> insert into parts
  2  values (10, 'part5' , null)
  3  /

1 row created.

SQL>
SQL> insert into parts
  2  values (2, 'part6' , 10)
  3  /

1 row created.

SQL>
SQL> insert into parts
  2  values (3, 'part7' , 10)
  3  /

1 row created.

SQL>
SQL> alter table parts
  2  add constraint fk_parent foreign key (parent) references parts;

Table altered.

SQL>
SQL>
SQL> drop table parts;

Table dropped.








6.16.FOREIGN KEY
6.16.1.Adding a FOREIGN KEY Constraint
6.16.2.ON DELETE CASCADE clause with a FOREIGN KEY constraint
6.16.3.Use the ON DELETE SET NULL clause with a FOREIGN KEY constraint
6.16.4.ORA-02298: cannot validate (JAVA2S.PRODUCT_ORDER_FK_PRODUCT) - parent keys not found
6.16.5.A foreign key to reference itself
6.16.6.ORA-02270: no matching unique or primary key for this column-list
6.16.7.Add constraint foreign key references
6.16.8.Violate a foreign key
6.16.9.Disable foreign key
6.16.10.Many to many using a primary-key and foreign-key relationship