In Oracle SQL / PLSQL a FOREIGN KEY is column / field that appears in one table and must appear in another table.
Important points about FOREIGN KEY in Oracle SQL / PLSQL:
- A FOREIGN KEY creates a parent child pattern between two tables.
- The referenced table is called the PARENT table and the table having the FOREIGN KEY is called as the CHILD table.
- The FOREIGN KEY in child table generally references PRIMARY KEY in parent table.
- A foreign key can be defined in CREATE TABLE or ALTER TABLE statement.
Syntax to create a FOREIGN KEY in CREATE statement is:
CREATE TABLE table_name
(column_name1 datatype NULL/NOT NULL
,column_name2 datatype NULL/NOT NULL
, column_name3 datatype NULL/NOT NULL
.
.
column_nameN datatype NULL/NOT NULL
,CONSTRAINT constraint_name
FOREIGN KEY (column_name1, column_name2, . . . column_nameN )
REFERENCES parent_table_name(column_name1, column_name2, . . . column_nameN)
);
Syntax to create a FOREIGN KEY in ALTER statement is:
ALTER TABLE TABLE_NAME
ADD CONSTRAINT constraint_name
FOREIGN KEY (column_name1,column_name2, . . column_nameN)
REFERENCES parent_table_name (column_name1,column_name2, . . column_nameN);
Let’s take an example for understanding:
Scenario 1:
Step 1:
Suppose we want to create a table named ‘employee’ in the database as shown below, with ‘employee_id’ as the primary key for the ‘employee’ table
Employee_Id | Employee_Name | Salary | Department |
101 | Emp A | 10000 | Sales |
102 | Emp B | 20000 | IT |
103 | Emp C | 28000 | IT |
104 | Emp D | 30000 | Support |
105 | Emp E | 32000 | Sales |
And we want to create a table named ‘comm’ with ‘emp_id’ as FOREIGN KEY as shown below:
Emp_Id | Commission_Percent |
102 | 20 |
103 | 20 |
104 | |
105 | 10 |
We can achieve the same as:
1 | CREATE TABLE employee |
2 | (employee_id NUMBER(10) NOT NULL |
3 | ,employee_name VARCHAR2(500) NOT NULL |
4 | ,salary NUMBER(20) NOT NULL |
5 | ,department VARCHAR2(300) NOT NULL |
6 | , CONSTRAINT employee_pk PRIMARY KEY (employee_id) |
7 | ); |
1 | CREATE TABLE comm |
2 | (emp_id NUMBER(10) |
3 | ,commission_percent NUMBER(20) |
4 | , CONSTRAINT fk_employee |
5 | FOREIGN KEY (emp_id) |
6 | REFERENCES employee(employee_id) |
7 | ); |
Here with the help of the above SQL CREATE statement we have created a table named ‘employee’ that has 4 columns namely ‘employee_id’, ‘employee_name’, ‘department’ and ‘salary’ and we are having the ‘employee_id’ column as the primary key for the ‘employee’ table.
Also, we have created a new table named ‘comm’ which has 2 columns namely ‘emp_id’ and ‘commission_percent’ and we are having ‘emp_id’ as foreign key referencing ‘employee_id’ of ‘employee’ table.
Step 2: Inserting the data in the ‘employee’ table.
1 | INSERT INTO employee |
2 | VALUES (101, 'Emp A' ,10000, 'Sales' ); |
3 |
4 | INSERT INTO employee |
5 | VALUES (102, 'Emp B' ,20000, 'IT' ); |
6 |
7 | INSERT INTO employee |
8 | VALUES (103, 'Emp C' ,28000, 'IT' ); |
9 |
10 | INSERT INTO employee |
11 | VALUES (104, 'Emp D' ,30000, 'Support' ); |
12 |
13 | INSERT INTO employee |
14 | VALUES (105, 'Emp E' ,32000, 'Sales' ); |
Step 3: Inserting the data in the ‘comm’ table.
1 | INSERT INTO comm |
2 | VALUES (102,20); |
3 |
4 | INSERT INTO comm |
5 | VALUES (103,20); |
6 |
7 | INSERT INTO comm |
8 | VALUES (104, NULL ); |
9 |
10 | INSERT INTO comm |
11 | VALUES (105,10); |
The above SQL INSERT statements will insert 5 rows in the ‘employee’ table and 4 rows in ‘comm’ table.
Now, if we query the employee table as:
1 | SELECT * |
2 | FROM employee; |
We will get the following result:
Employee_Id | Employee_Name | Salary | Department |
101 | Emp A | 10000 | Sales |
102 | Emp B | 20000 | IT |
103 | Emp C | 28000 | IT |
104 | Emp D | 30000 | Support |
105 | Emp E | 32000 | Sales |
Now, if we query the ‘comm’ table as:
1 | SELECT * |
2 | FROM comm; |
We will get the following result:
Emp_Id | Commission_Percent |
102 | 20 |
103 | 20 |
104 | |
105 | 10 |
Here we have successfully created ‘employee’ and ‘comm’ tables with ‘employee_id’ as PRIMARY KEY for ‘employee’ table and ‘emp_id’ as foreign key for ‘comm’ and also inserted data in ‘employee’ and ‘comm’ tables.
If we try to insert one more record in ‘comm’ table of ‘emp_id = 106’ as:
1 | INSERT INTO comm |
2 | VALUES (106,30); |
We get an error that integrity constraint is getting violated and the data cannot be entered, this is because ‘emp_id = 106’ is not found in ‘employee’ table and hence the record cannot be entered in ‘comm’ table.
Scenario 2:
Using ALTER TABLE statement to create a FOREIGN KEY
Syntax for creating a FOREIGN KEY using ALTER TABLE statement is:
ALTER TABLE TABLE_NAME
ADD CONSTRAINT constraint_name
FOREIGN KEY (column_name1,column_name2, . . column_nameN)
REFERENCES parent_table_name (column_name1,column_name2, . . column_nameN);
Example:
1 | ALTER TABLE comm |
2 | ADD CONSTRAINT comm_fk |
3 | FOREIGN KEY (emp_id) |
4 | REFERENCES employee (employee_id); |