×

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

Secondary Key in DBMS

What are Keys? Keys in DBMS are the attributes or set of attributes that uniquely identify a row in a set of relations. A table (relation) can have a column or...

3 minutes read.

Network model in DBMS

Network model: The many-to-many relationship between the database constraints is represented hierarchically by the Network Model in DBMS. It is a straightforward and straightforward database model. Due to the Network Model...

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.

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.

Aggregate Functions in DBMS

Aggregate Functions in DBMS: Aggregate functions are those functions in the DBMS which takes the values of multiple rows of a single column and then form a single value by...

2 minutes read.

Serializability in DBMS

Serializability in DBMS Serializability is the concept in a transaction that helps to identify which non-serial schedule is correct and will maintain the database consistency. It relates to the isolation property of transaction in...

3 minutes read.

ER Diagram of Company Database in DBMS

Entity Relationship Diagram Entity Relationship Diagrams, or ER Diagrams for short, are diagrams that show the relationships among entity sets that are stored in databases. Alternatively said, ER diagrams assist in...

3 minutes read.

Advantages of Threaded Binary Tree in DBMS

We know that every node in a binary tree contains both its data value and the address pointers for its left and right children. A null pointer is used to...

3 minutes read.

Disavantages of RDBMS

RDBMS offers many features when handling the data, but it also has certain limitations that can be overcome by choosing an alternative data model. The limitations of the Relational Data...

3 minutes read.

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.

Evaluating DBMS

There are various database management systems available in the market. Each type has its features and can be used for varied purposes. The large number of DBMS makes it difficult...

4 minutes read.

Super Key in DBMS

Super Key in DBMS: The super key is a column or a set of columns in the database table, which uniquely identifies the tuple or row of the same table....

2 minutes read.

Inference Rules

Armstrong’s axioms are the complete set of basic inference rules used to infer all the functional dependencies on the relational database. An inference rule is a type of assertion that a user can...

2 minutes read.

Founder of DBMS

The first database management system was built to automate the business of the General Electric Company. It was built by a small group of programmers. The Integrated Data Store IDS...

4 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.

What is a Cloud Database

Cloud computing is the process of commoditizing data storage and processing time using standardized technologies. Cloud databases are databases that are hosted on cloud computing platforms such as Salesforce, GoGrid,...

4 minutes read.

Cardinality in DBMS

Cardinality in DBMS Cardinality is the relationship between two or more entities (table). It shows how all the entities are connected. In DBMS, all the entities and tables are interconnected with...

2 minutes read.

Domain in DBMS

Constraints: Constraints in DBMS are a set of guidelines that guarantee that authorized users who modify the database do not alter the consistency of the data. Constraints are expressed in DDL...

3 minutes read.

DBMS View: Read, Update, Create and Drop

View in DBMS: The View is a logical or virtual table that allows users to view or manipulate parts of the table. View is also a table which consists of...

2 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.