- It is used to establish relationship between 2 tables.
- Values of foreign key should match with values of primary key/ unique keys.
- It allows NULL values, duplicate values.
- A parent child relationship will be decalred when once the foreign key constraint is declared.
This relationship is also called as 'Master/ Detail relationship', 'Master/ Transaction relationship' , 'Master/ Salve relationship'.
- The table holding Primary Key is called Parent or Master.
- The table holding Foreign Key is called Child or Details Or Transaction or Slave.
- By default, Oracle creates one- many realtion ship for every parent-child relationship.
- To establish one-to-one relationship, decalre foreign key with Unique constraints.
Syntax:
column_name datatype [size] references table_name ( column_name );
Examples:
(i) Creating a Check Constraint.
Column Level Check declaration.
create table emp
(empno number(4), ..., deptno number(4) CONSTRAINT fk_deptno REFERENCES dept( deptno ), ........ );
Table level Check Constraint.
create table emp
(empno number(4), ...... deptno number(4), .......,
foreign key (deptno) CONSTRAINT fk_deptno REFERENCES dept( deptno ),...............);
Using Alter
create table emp
(empno number(4), ...... deptno number(4), .......)
Alter table emp add ( foreign key (deptno) CONSTRAINT fk_deptno REFERENCES dept( deptno ) );
(ii) Drop Check constraint.
Alter table emp drop CONSTRAINT fk_deptno;
- Values of foreign key should match with values of primary key/ unique keys.
- It allows NULL values, duplicate values.
- A parent child relationship will be decalred when once the foreign key constraint is declared.
This relationship is also called as 'Master/ Detail relationship', 'Master/ Transaction relationship' , 'Master/ Salve relationship'.
- The table holding Primary Key is called Parent or Master.
- The table holding Foreign Key is called Child or Details Or Transaction or Slave.
- By default, Oracle creates one- many realtion ship for every parent-child relationship.
- To establish one-to-one relationship, decalre foreign key with Unique constraints.
Syntax:
column_name datatype [size] references table_name ( column_name );
Examples:
(i) Creating a Check Constraint.
Column Level Check declaration.
create table emp
(empno number(4), ..., deptno number(4) CONSTRAINT fk_deptno REFERENCES dept( deptno ), ........ );
Table level Check Constraint.
create table emp
(empno number(4), ...... deptno number(4), .......,
foreign key (deptno) CONSTRAINT fk_deptno REFERENCES dept( deptno ),...............);
Using Alter
create table emp
(empno number(4), ...... deptno number(4), .......)
Alter table emp add ( foreign key (deptno) CONSTRAINT fk_deptno REFERENCES dept( deptno ) );
(ii) Drop Check constraint.
Alter table emp drop CONSTRAINT fk_deptno;
No comments:
Post a Comment