×

Foreign key in DBMS

Foreign key in DBMS: The Foreign key is a field or the set of fields in the relational database table, which points to the existing field in another table. It is a key that creates a relationship between the two tables. The main goal is to maintain the integrity of the relational data in the database relations. The foreign key acts as a cross-reference between the database tables because it references another table's primary key in the same database.

We are describing the foreign key concept with the following example, so you can easily understand it.

This example contains two tables. The name of the first table is Teacher_Details, and the name of the second table is Course_Assign.

The first table contains three columns. The name of these columns is Teacher_ID, Teacher_Name, and Teacher_Age. In this table, Teacher_Id acts as a primary key.

The second table also contains the three columns. And, the name of three columns in the second table is Course_ID, Course_name, and Teacher_ID, where Teacher_ID is selected as a foreign key, which points to the primary key in the Teacher table. And, the Course_ID acts as a primary in the Course table. 

First Table: Teacher

Teacher_ID (Primary Key)Teacher_NameTeacher_Age
201Anuj22
202Anik24
203Manoj23
204Anuj24

Second Table: Course

Course_ID (Primary key)Course_NameTeacher_ID (Foreign Key)
401Math201
402C202
403Java201
404DBMS203
405Cloud Computing204
406Big Data202

Create the Foreign Key in RDBMS

We have learned the meaning of foreign key with its example. Now, we will discuss how to create a foreign key in the SQL table. So, we will take the above table to create the foreign key. In SQL, we can create the foreign key as shown below:

CREATE TABLE Course (
    Course_ID int NOT NULL PRIMARY KEY,
    Course_Name varchar NOT NULL,
    Teacher_ID int FOREIGN KEY REFERENCES Teacher(Teacher_ID)
);

Using the following query, we can also create the foreign key constraint on the Teacher_ID column in the Course table when the table is already created.

ALTER TABLE Course
ADD FOREIGN KEY (Teacher_ID) REFERENCES Teacher(Teacher_ID);


Related Topics

What is a Database

A database is a structured collection of data that is often kept electronically on a computer system. Typically, a database is managed by a database management system (DBMS). Together, the...

9 minutes read.

B+ (Plus) Tree in DBMS

What is a B+ tree? The B+ tree is known as a balanced binary search tree. in this tree, we can store the data in the form of nodes. B+ tree...

9 minutes read.

Data Models in DBMS

Data Models in DBMS Data models are the models explaining the logical structure of the database systems. They describe the entities, attributes, and the relationship among the data elements of the...

8 minutes read.

Concurrency Control Protocols

Concurrency Control Protocols Concurrency control protocols ensure the atomicity, serializability and isolation of the concurrent transactions. The Concurrency control protocols can be broadly classified into the following categories: Lock Based ProtocolTimestamp protocol Lock Based Protocol In this protocol,...

5 minutes read.

ACID Properties in DBMS

A transaction in a database has the following four properties, known as ACID properties. These properties are used to maintain the consistency of the database in the case of system failure and concurrent...

4 minutes read.

File Organization in DBMS

File Organization in DBMS: A database contains a huge amount of data, which is stored is in the physical memory in the form of files. A file is a set...

9 minutes read.

Constraints in Database Management System

In a database management system, certain constraints are applied while performing any operation on the database to ensure that the integrity and quality of the data are maintained throughout the...

4 minutes read.

ER Model: Entity Relationship Diagram (ERD) with Examples

ER model stands for Entity-Relationship Model. It is a high-level data model diagram which defines the conceptual view of the database. It is a blueprint or design of a database that will...

2 minutes read.

Three Schema Architecture of DBMS

The three schema architecture describes how the data is represented or viewed by the user in the database. This architecture is also known as three-level architecture and is sometimes called...

3 minutes read.

Schedule in DBMS

Schedule A sequence of statements that specify the sequential order in which the statements of concurrent transactions are executed. The transaction will get committed when it executes the instructions successfully without any...

4 minutes read.

Domain Constraints in DBMS

Introduction about DBMS:- DBMS stands for DataBase Management System. DBMS(DataBase Management System) is a type of software by which we can save and retrieve the user's data with the security process....

3 minutes read.

Foreign key in DBMS

Foreign key in DBMS: The Foreign key is a field or the set of fields in the relational database table, which points to the existing field in another table. It...

2 minutes read.

Primary Key in DBMS

Primary Key in DBMS: A primary key is the minimal set of columns in the database table, which uniquely identifies each row or tuple in that table. Any table in...

2 minutes read.

DBMS Tutorial | Database Management System

Database Management System (DBMS) tutorial is all about managing and maintaining the data effectively. Our DBMS tutorial is designed for beginners as well as professionals. What is Data? Data is a real-world...

5 minutes read.

Relational Algebra in DBMS

Relational Algebra is a widely used procedural query language, which takes instances of one or more relation as an input and generates a new relation as an output. It uses a different...

6 minutes read.

B-Tree Insertion in DBMS

A B-tree is a special type of m-way tree, commonly used for disk access. A B-tree of order m can have at most m-1 keys and m descendants. B-trees are...

4 minutes read.

DBMS Architecture

Architecture of Database Management System DBMS architecture helps in development, implementation, design, and maintenance of a database that store and organize information for agencies, businesses, and institutions. It is the base of any database...

2 minutes read.

Deadlock in DBMS

Deadlock in DBMS A deadlock is an unwanted condition in which two or more transaction are waiting indefinitely for one another to give up locks. It is said to be one of the...

3 minutes read.

Entity and Entity set in DBMS

What is DBMS? A database management system (DBMS) is a software application that interacts with end-users, other applications, and the database itself to capture and analyze the data. A DBMS allows...

3 minutes read.

Conflict Serializability in DBMS

Conflict Serializability A schedule is said to be conflict serializable if it can transform into a serial schedule after swapping of non-conflicting operations. It is a type of serializability that can be used...

3 minutes read.