How To Use Foreign Key In Oracle
Foreign key in Oracle
A foreign key establishes and enforces a link between the data in the two tables. We can create a foreign key by defining a foreign key constraint, while creating or modifying/altering a table.
The referenced table is called the parent table, while the table with the foreign key is called the child table.
The referenced table is called the parent table, while the table with the foreign key is called the child table.
A foreign key can be defined in either a create table or an alter table statement.
Syntax
Using a create table statement
- Create table table_name
- (
- Column 1 datatype(),
- Column 2 datatype(),
- . .
- . .
- . .
- Column n datatype()
- CONSTRAINT fk_constraint_name
- FOREIGN KEY (column1, column2, ... column_n)
- REFERENCES parent_table (column1, column2, ... column_n)
- );
- Commit;
- create table department
- (
- dept_id number(20),
- dept_name varchar2(14),
- loc varchar2(15),
- constraint pk_department primary key (dept_id)
- );
- Commit;
- create table employee
- (
- dept_id number(20),
- emp_id number(20),
- emp_name varchar2(15),
- emp_address varchar2(100),
- emp_mob number(10),
- constraint pk_employee primary key (emp_id),
- constraint fk_dept_id foreign key (dept_id) references department (dept_id)
- );
Using alter table statement
Syntax
- Alter table table_name
- Add constraint Constraint_name
- Foreign key(Column 1, column 2……column n)
- References parent_table(column 1, column 2….column n);
- Alter table employee
- Add constraint FK_department
- Foreign key(Dept_id)
- Reference department(dept_id)
Foreign key with cascade delete
A foreign key with cascade delete means, if a parent table record is deleted, the child table record will automatically be deleted in Oracle.
Syntax
- Create table table_name
- (
- Column 1 datatype(),
- Column 2 datatype(),
- . .
- . .
- Column n datatype(),
- Constraint fk_constraint_name
- Foreign key (column1, column2, ... column_n)
- References parent_table (column1, column2, ... column_n)
- ON DELETE CASCADE
- );
- create table employee
- (
- dept_id number(20),
- emp_id number(20),
- emp_name varchar2(15),
- emp_address varchar2(100),
- emp_mob number(10),
- constraint pk_employee primary key (emp_id),
- constraint fk_dept_id foreign key (dept_id) references department (dept_id)
- ON DELETE CASCADE
- );
Foreign key with set null on delete
A foreign key with set null on delete means the parent table is deleted and the child table will have the foreign key is set to null. The child table will not be deleted.
Syntax
- Create table table_name
- (
- Column 1 datatype(),
- Column 2 datatype(),
- . .
- . .
- Column n datatype(),
- CONSTRAINT fk_constraint_name
- Foreign key (column 1, column 2, ... column n)
- References parent_table (column 1, column 2, ... column n)
- ON DELETE SET NULL
- );
- create table employee
- (
- dept_id number(20),
- emp_id number(20),
- emp_name varchar2(15),
- emp_address varchar2(100),
- emp_mob number(10),
- constraint pk_employee primary key (emp_id),
- constraint fk_dept_id foreign key (dept_id) references department (dept_id)
- ON DELETE SET NULL
- );
Output
Summary
Thus, we learnt, a foreign key establishes and enforces a link between the data in the two tables i.e. called Foreign key and we learnt, how to use foreign key in Oracle with the examples.