AWS Global Infrastructure

Databases

Topics Covered
  • Cassandra (13 Blogs)
  • MongoDB Dev and Admin (15 Blogs)
  • MySQL (30 Blogs)
  • SQL Essentials Training and Certification (4 Blogs)
SEE MORE

Foreign Key SQL : Everything You Need To Know About Foreign Key Operations

Last updated on Mar 18,2024 6.7K Views

25 / 37 Blog from Introduction to SQL

In today’s market where a lot of multinational companies use relational databases to handle data, it is very important to understand how each and every table can be related to each other. So, in this article on Foreign Key SQL, I will discuss the foreign key in tables to make you understand the relationship between tables.

The following topics will be covered in this article:

  1. What is Foreign key Constraint?
  2. Rules for Foreign Key
  3. Foreign Key Operations:

 What is Foreign Key constraint?

A foreign key is a type of key used to link two tables in a database. So, a foreign key is an attribute or a collection of attributes in one table that refers to the primary key in another table.

For Example, if Table A and Table B are related to each other, then if Table A consists of the primary key, this table would be called the referenced table or parent table. Similarly, if Table B consists of a foreign key, then that table is known as the referencing table or child table. Refer to the below image:

Foreign Key - Foreign Key SQL - Edureka

Now that you know what is foreign key, next in this article on Foreign key SQL, let us understand the rules of the foreign key.

Rules for Foreign key

The Rules of Foreign Key are as follows:

  1. The table with the foreign key is called the child table and the table being referenced by the foreign key is called the parent table.
  2. Null values are allowed in a foreign key
  3. Foreign keys can be duplicated
  4. There can be more than a single foreign key in a table
  5. The relationship established between the tables is known as referential integrity 

Now that you know what are the rules of a foreign key, next in this article on Foreign key SQL, let us see the operations of the foreign key.

Foreign Key Operations:

To understand the various operations present on Foreign key, consider the following two tables:

Customer Table:

CustomerIDCustomerNamePhoneNumber
1Rohan9876543210
2Sonali9876567864
3Ajay9966448811
4Geeta9765432786
5Shubham9944888756

Courses Table:

CourseID

CourseName

CustomerID

c01

DevOps

2

c02

Machine Learning

4

c03

RPA

1

c04

Tableau

3

c05

AWS

2

Now, if you observe, the customerID column in the courses table refers to the customerID column in the customers’ table. The customerID column from the customers’ table is the Primary Key and the customerID column from the courses table is the Foreign Key of that table.  

Starting with the first operation:

Foreign Key on Create Table

You can use the following syntax to create a foreign key on the “customerID” column when you create “courses” table:

#For SQL Server/ MS Access/ Oracle
CREATE TABLE courses (
courseID varchar NOT NULL PRIMARY KEY,
courseName varchar NOT NULL,
customerID int FOREIGN KEY REFERENCES customers(customerID)
);
#For MySQL
CREATE TABLE courses (
courseID varchar NOT NULL PRIMARY KEY,
courseName varchar NOT NULL,
customerID int
PRIMARY KEY (courseID),
FOREIGN KEY (customerID) REFERENCES customers(customerID)
);

Apply Foreign Key on Multiple Columns

To apply foreign key on multiple columns while creating a table, refer to the following example:

CREATE TABLE courses (
courseID varchar NOT NULL,
courseName varchar NOT NULL,
customerID int, PRIMARY KEY (courseID),
CONSTRAINT FK_CustomerCourse FOREIGN KEY (customerID)
REFERENCES customers(customerID)
);

Next, in this article on Foreign Key SQL, let us see how to use the foreign key on Alter Table.

For a detailed, You can even check out the details of relational databases, functions, queries, variables, etc with the Microsoft SQL Course.

Foreign Key on Alter Table

You can use the following syntax to create a foreign key on the “customerID” column when the “courses” table is already created and you just want to alter the table:

ALTER TABLE courses
ADD FOREIGN KEY (customerID) REFERENCES customers(customerID);

If you wish to add a name to the Foreign Key constraint and define it on multiple columns, use the following SQL syntax:

ALTER TABLE courses
ADD CONSTRAINT FK_CustomerCourse
FOREIGN KEY (customerID) REFERENCES Customers(customerID);

Next, in this article on Foreign Key SQL, let us understand how to drop a foreign key

Drop Foreign Key

To drop the foreign key, you can refer to the following example:

#For SQL Server/ MS Access/ Oracle
ALTER TABLE courses
DROP CONSTRAINT FK_CustomerCourse;
For MYSQL
ALTER TABLE courses
DROP FOREIGN KEY FK_CustomerCourse;

With this, we come to an end to this article. I hope you understood how to use Foreign Key in SQL. If you wish to learn more about MySQL and get to know this open-source relational database, then check out our MySQL DBA Certification Training which comes with instructor-led live training and real-life project experience. This training will help you understand MySQL in-depth and help you achieve mastery over the subject.

Got a question for us? Please mention it in the comments section of this article on “Foreign Key SQL” and I will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Foreign Key SQL : Everything You Need To Know About Foreign Key Operations

edureka.co