SQL Query to Add Foreign Key Constraints Using ALTER Command
Last Updated :
21 Aug, 2024
In relational databases, a Foreign Key Constraint is a fundamental concept that defines a relationship between two tables. It ensures that the value of one or more columns in a child table corresponds to the value in a primary key or unique key in a parent table.
In this article, we will look into how we can add a foreign key constraint using the ALTER command in SQL.
Foreign Key Constraint
- A Foreign Key Constraint in a relational database is a rule that establishes a relationship between two tables.
- It ensures that the values in a column (or a group of columns) of one table (called the child table) must match the values in a column (or a group of columns) in another table (called the parent table).
- The column(s) in the parent table is referred to as the primary key or unique key, and the column(s) in the child table that references this primary key is the foreign key.
Create a Database:
We can create a Database using the command:
Syntax:
CREATE DATABASE DATABASE_NAME;
So let’s create a geeks database as shown below:
CREATE DATABASE geeks;
Output:

Using Database:
Use the below command to use the geeks database:
use geeks;
Output:

Adding table into Database:-
To add a table into the database we use the below command:
Syntax:-
CREATE TABLE table_name (Attribute_name datatype...);
So, let’s create a geeks table within the geeks database as shown below:
CREATE TABLE geeks(id int , name varchar(20));
Output:

Here Table Added Successfully.
Inserting values into Tables:
For inserting records into the table we can use the below command:
Syntax:
INSERT INTO table_name(column1,
column2,
column 3,.....)
VALUES( value1,
value2,
value3,.....);
So let’s add some records in the geeks table:
INSERT INTO geeks(id,name) VALUES (1,"teja");
Output:

Creating Primary Key Element in a Table:
To have a Foreign Key in a Table we must have a Primary Key. To create a Primary we use the below command:
Syntax:
CREATE TABLE table_name (Attribute_name datatype PRIMARY_KEY);
Now let’s create a primary key:
CREATE TABLE emp (id int NOT NULL PRIMARY KEY,name varchar(20))
Output:

Now to add a Foreign Key we have to create a new table by the following:
CREATE TABLE student(
id int ,
name varchar(20),
Emp_id int REFERENCES emp(id));
Output:

Alter a Table and ADD Foreign Key:
So if you already created the table student, and now you wish to add Foreign Key you can use the below command to change that:
ALTER TABLE dbo.student
add constraint Fk_empid foreign key(emp_id)
references dbo.emp(id);
Output:

At this point, we have successfully achieved our goal.
Conclusion
The Foreign Key Constraint is a crucial component in relational database management. It helps ensure the integrity and accuracy of your data by linking related records between different tables. By defining these relationships, you can prevent the entry of invalid data and establish clear connections between tables for more structured data management.
Similar Reads
SQL Query to Drop Foreign Key Constraint Using ALTER Command
A foreign key is included in the structure of a table, to connect multiple tables by referring to the primary key of another table. Sometimes, you might want to remove the primary key constraint from a table for performance issues or other requirements. A foreign key constraint can be removed from a
3 min read
SQL Query to Add Unique key Constraints Using ALTER Command
Here we will see how to add unique key constraint to a column(s) of a MS SQL Server's database with the help of a SQL query using ALTER clause. For the demonstration purpose, we will be creating a demo table in a database called "geeks". Creating the Database : Use the below SQL statement to create
2 min read
SQL Query to Drop Unique Key Constraints Using ALTER Command
Here, we see how to drop unique constraints using alter command. ALTER is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table. Syntax : ALTER TABLE table_name DROP CONSTRAINT unique_constraint; For instance, cons
2 min read
How to add a foreign key using ALTER in MySQL
In this article, we will discuss the overview of foreign keys and will discuss how to add a foreign key using ALTER in MySQL step by step. Let's discuss it one by one. Foreign key : If an attribute is a primary key in one table but was not used as a primary key in another table then the attribute wh
3 min read
SQL Query to Add a New Column After an Existing Column in SQL
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve data from relational databases like MySQL, Oracle, SQL Server, Postgres, etc. In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command
3 min read
MySQL FOREIGN KEY Constraint
A FOREIGN KEY is a field/column(or collection of fields) in a table that refers to a PRIMARY KEY in another table. It is used for linking one or more than one table together. FOREIGN KEY is also called referencing key. A Foreign key creates a link (relation) between two tables thus creating referent
7 min read
SQL FOREIGN KEY Constraint
A FOREIGN KEY constraint is a fundamental concept in relational databases, ensuring data integrity by enforcing relationships between tables. By linking a child table to a parent table, the foreign key establishes referential integrity. This constraint ensures that the values in the foreign key colu
5 min read
How to Delete a Foreign Key Constraint in SQL
Foreign key constraints are important for maintaining referential integrity in a relational database as they define relationships between tables. However, there are times when we may need to identify which foreign key constraints reference a specific table in SQL Server. This could be necessary for
4 min read
How to Use SQL Query to Rename a Constraint?
In SQL, we sometimes need to rename the constraints of a table. The whole process for doing the same is demonstrated below. For this article, we will be using the Microsoft SQL Server as our database. Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks.
2 min read
How to Temporarily Disable a Foreign Key Constraint in MySQL?
MySQL is a popular open-source relational database management system (RDBMS) that is uniquely used to construct expandable and high-productivity databases. MySQL, which was created by MySQL AB and later acquired by its current owner Oracle Corporation, was originally introduced in 1995. MySQL is rep
4 min read