SQL Essentials Training & Certification
- 11k Enrolled Learners
- Weekend/Weekday
- Self Paced
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:
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:
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.
The Rules of Foreign Key are as follows:
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:
CustomerID | CustomerName | PhoneNumber |
1 | Rohan | 9876543210 |
2 | Sonali | 9876567864 |
3 | Ajay | 9966448811 |
4 | Geeta | 9765432786 |
5 | Shubham | 9944888756 |
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:
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) );
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.
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
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.
edureka.co