Updated March 28, 2023
What is Oracle FOREIGN Key?
An Oracle FOREIGN Key is used to define an integrity constraint that restricts the values in a Database Table. The table which contains a FOREIGN key called the child table. The FOREIGN key column data reference the parent column data.
Declaration Style of FOREIGN Key
It can be declared in two ways. They are,
1. Column Level (In-Line) Style
- It can be declared as part of the definition of an individual column or attribute.
- Usually applied when the constraint is specific to that column only.
2. Table Level (Out of Line) Style
- It can be declared as part of the table definition.
- It can be declared on the combination of columns together.
Points of Concentration
- An Oracle FOREIGN key called REFERENTIAL INTIGRITY CONSTRAINT.
- A FOREIGN KEY column makes a relationship with a specified PRIMARY or UNIQUE KEY.
- If a FOREIGN KEY is applied on multiple columns, called a composite FOREIGN KEY.
- A Table or VIEW which contains the FOREIGN Key is known as child object and if FOREIGN Key column(s) references the TABLE or VIEW is known as PARENT object.
- The same TABLE or VIEW can contain FOREIGN KEY and REFERENCE KEY.
- ORDER and DATA type must match with the corresponding FOREIGN key and REFERENCE key column(s).
- A COMPOSITE UNIQUE or PRIMARY key of the PARENT TABLE or VIEW can only be referred by a composite FOREIGN key.
- Those Tables which contain only FOREIGN keys that relate to another table (s) PRIMARY key, called PURE Details.
Restrictions
The FOREIGN KEY cannot be applied to:
- LOB, LONG, LONG RAW, VARRAY, NESTED TABLE, OBJECT, BFILE, REF, TIMESTAMP WITH TIME ZONE.
- The REFERENCED UNIQUE / PRIMARY KEY must be existed on the PARENT TABLE or VIEW before declaring the FOREIGN KEY to reference that column.
- Maximum 32 columns can be a part of a COMPOSITE FOREIGN key combination.
- Both Child and Parent tables that are being used for FOREIGN KEY must belong to the same Database.
- ORACLE TRIGGERS can be used on a distributed Database across nodes to make enable REFERENTIAL integrity.
- Within a single user or in the same schema, no two FOREIGN keys can have the same name.
References Clause
The REFERENCES clause should be used when the FOREIGN key constraint is INLINE declared. When the constraint is OUT, OF LINE, FOREIGN KEY keyword must be specified.
Syntax #1 – For INLINE FOREIGN Key
CREATE table Table_Name (col_1 Datatype (width), col_2 Data type (width) CONSTRAINT cons_name REFERENCES Table (column name));
Syntax #2 – For OUT OF LINE FOREIGN Key
CREATE table Table_Name (col_1 Datatype (width), col_2 Datatype (width), col_2 Datatype (width), CONSTRAINT cons_name FOREIGN KEY (col_1, col_2) REFERENCES Table (col_1, col_2);
Explanation: Col_1/2/n: The column(s) or calculation as per your requirement. Table_Name: As per your requirement. Table (col_1, col_2): Table is the table name that we use for referencing and col_1/2 are the columns from the Table for FOREIGN key reference.
Implementations of FOREIGN Key with Examples
In this section, we’ll see the implementation of Oracle FOREIGN Key and its behavior.
Example #1
Code:
CREATE TABLE emp_1 (emp_id NUMBER (2), emp_dept NUMBER (2) CONSTRAINT DeptID REFERENCES dept (deptno));
Output:
Explanation: In the above example, FOREIGN key CONSTRAINT applies on the emp_dept column that FOREIGN key referencing the deptno column from the dept table. It means the emp_dept column of the emp_1 table can contain only record(s) which exist in the deptno column of the dept table. This kind of record is called the child record.
Example #2
Code:
INSERT INTO Emp_1 VALUES (1, 70);
Output:
Explanation: The above INSERT query inserts one record (see below image) and emp_dept value (70) exits in the dept table.
Output:
Example #3
Code:
INSERT INTO Emp_1 VALUES (1, 90);
Output:
Explanation: But we run the same insert query with a different value (90) and it returns an error. WHY? Because the value (90) does not exist in the column deptno of the dept table (see dept table data below). As per FOREIGN key rule, the column which consists of a FOREIGN key can only contain referential data, not new value.
Code:
SELECT Deptno from Dept;
Output:
Composite FOREIGN Key in Oracle
Composite FOREIGN KEY / Table Level (Out of Line) style:
Code:
CREATE TABLE Samp_PK
(
SamID NUMBER (2),
SamName VARCHAR2 (10),
SamDate DATE
) ;
Output:
Explanation: The above SELECT statement creates a table Samp_PK without any CONSTRAINT. We’ll use this table in the below CREATE table statement to declare/reference the FOREIGN key.
Code:
CREATE TABLE Samp_FK
(
SamID NUMBER (2),
SamName VARCHAR2 (10),
SamDate DATE,
CONSTRAINT sampFK_01 FOREIGN KEY (SamID, SamName)
REFERENCES Samp_PK (SamID, SamName)
);
Output:
Explanation: In the above example, we tried to create a composite FOREIGN key using table Samp_PK for reference the columns but it throws an error. WHY? Because as per rule a composite FOREIGN key only can refer to a COMPOSITE UNIQUE key or a composite PRIMARY key column in the PARENT TABLE or VIEW.
1. FOREIGN Key Maintenance
- FOREIGN key can be ADDED, DROPPED, ENABLED or DISABLED but cannot modify the physical structure of the table.
- The FOREIGN key constraint name can be checked form the
- USER_CONSTRAINTS
- USER_CONS_COLUMNS data dictionary views
2. Viewing FOREIGN Key
Syntax:
SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME= ‘Table_Name’;
Code:
SELECT Constraint_Name, Constraint_type FROM USER_CONSTRAINTS WHERE TABLE_NAME= ‘Emp_1’;
Output:
Output showing Constraint_Name and Type. Here R denotes REFERENTIAL INTEGRITY means FOREIGN Key.
3. Adding FOREIGN Key
Syntax:
ALTER TABLE Table_Name ADD [CONSTRAINT <Constraint Name>] Constraint_Type (Column_Name);
Code:
ALTER TABLE Samp_PK ADD CONSTRAINT samp_FK UNIQUE (SamID, SamName);
Output:
Earlier SAMP_PK table had no CONSTRAINT.
4. Droping FOREIGN Key
Syntax:
ALTER TABLE Table_Name DROP [CONSTRAINT <Constraint Name>] Constraint_Type (Column_Name);
Code:
ALTER TABLE Emp_1 DROP CONSTRAINT DeptID;
Output:
In the above example, the FOREIGN KEY (DeptID) from the Emp_1 table got dropped. Now, Emp_1 doesn’t have any FOREIGN Key (see image below).
Code:
SELECT Constraint_Name, Constraint_type FROM USER_CONSTRAINTS WHERE TABLE_NAME= ‘Emp_1’;
Output:
TIP
1. FOREIGN Key constraint can be disabled or enabled as well. The syntax is given below.
Syntax:
ALTER TABLE Table_Name ENABLE / DISABLE CONSTRAINT <Constraint Name>];
2. PARENT record can’t be deleted if child record(s) exist. If you try to delete it, it will throw an error message (given below).
Output:
Conclusion
Oracle FOREIGN KEY is CONSTRAINT which provides REFERENTIAL integrity and which restricts the value(s) that doesn’t exist in parent TABLE or VIEW. FOREIGN KEY is the best practice to link one table with another.
Recommended Articles
This is a guide to Oracle FOREIGN Key. Here we discuss declared in two ways, References clause, with examples and Composite key. You can also go through our other related articles to learn more –