FOREIGN KEY in Oracle SQL – PLSQL

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:

  1. A FOREIGN KEY creates a parent child pattern between two tables.
  2. The referenced table is called the PARENT table and the table having the FOREIGN KEY is called as the CHILD table.
  3. The FOREIGN KEY in child table generally references PRIMARY KEY in parent table.
  4. 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:


CREATE TABLE employee
(employee_id   NUMBER(10)       NOT NULL
,employee_name VARCHAR2(500)    NOT NULL
,salary        NUMBER(20)       NOT NULL
,department    VARCHAR2(300)    NOT NULL
,CONSTRAINT employee_pk PRIMARY KEY (employee_id)
);

CREATE TABLE comm
(emp_id             NUMBER(10)
,commission_percent NUMBER(20)
,CONSTRAINT fk_employee
 FOREIGN KEY (emp_id)
 REFERENCES employee(employee_id)
);

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.


INSERT INTO employee
VALUES (101,'Emp A',10000,'Sales');

INSERT INTO employee
VALUES (102,'Emp B',20000,'IT');

INSERT INTO employee
VALUES (103,'Emp C',28000,'IT');

INSERT INTO employee
VALUES (104,'Emp D',30000,'Support');

INSERT INTO employee
VALUES (105,'Emp E',32000,'Sales');

Step 3: Inserting the data in the ‘comm’ table.

INSERT INTO comm
VALUES (102,20);

INSERT INTO comm
VALUES (103,20);

INSERT INTO comm
VALUES (104,NULL);

INSERT INTO comm
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:

SELECT *
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:

SELECT *
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:

INSERT INTO comm
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:

ALTER TABLE comm
ADD CONSTRAINT comm_fk
FOREIGN KEY (emp_id)
REFERENCES employee (employee_id);

Tagged , , , , , , , , , , , . Bookmark the permalink.