Refer: w3schools-SQL
It states:
The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. Can someone give an example of an action that foreign key would prevent ?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Lets say we have the two tables Employees and Departments. Each department has a unique number, and for each employee we list the number of the department that he or she works on. We have also declared this as a foreign key.
In that case the foreign key would prevent a number of actions:
All this is to ensure that when the database says that an employee works on a certain department, then that department will actually exist in the database.