Add constraint foreign key references : Create Foreign Key « Constraints « Oracle PL / SQL






Add constraint foreign key references

  

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.

   
    
  








Related examples in the same category

1.Create table with foreign key
2.Create a foreign key with more than one field
3.Referential Integrity Constraints
4.Violate a foreign key
5.Declaring a Foreign Key