Here we look at Foreign Keys in SQL Server and how to find them within SQL Server Management Studio, the video covers:
How To Create Foreign Key Constraints
We look at how to create Foreign keys along with why they are needed and what we need to check prior to creating them. Foreign keys can be created when we create the table, or alternatively they can be created afterward. In this tutorial I show you both. I find its very useful to know how to create Foreign Keys using code personally and as a DBA or a Data Analyst you should understand this.
Foreign Key Error
Very frequently we may find that we are getting Foreign Key errors in SQL Server, errors such as 'Violation of Foreign Key Constraint'. This is usually raised when inserting into a table and the the value the Foreign Key is referencing, isn't there - so the constraint is doing its job. Remember a Foreign Key is there to enforce integrity in a database. In this tutorial we recreate that error and explain why it happens and more importantly - how to fix it!
Primary Key vs Foreign Key
Although this tutorial isn't solely on Primary Key vs Foreign Key (I will probably do one shortly), I do reference the usage of Primary Keys in this video and explain the comparison between the two. Knowing what a Primary Key is this tutorial shows you everything about a Foreign Key so the difference is explained to you here too.
How To Find Foreign keys in SQL Server Management Studio
There are times when we may want to know how to find foreign keys within SQL Server management studio. In this video we cover how to locate these within tables and I run through scripting them out and re-adding them. The biggest problem that I found when creating Foreign keys are that the data may need to be 'cleansed' first because you may be trying to create a Foreign Key on a table but the existing data within it contradicts what you are trying to do - so you need to prep the table first.
To explain the above example we look at how to remove a foreign key and then re-add it.
As always, if there are any questions then let me know.
Thanks
Alan
Lesson
00:00 Tutorial Overview
02:14 Inserting into table with Foreign key
03:05 Violation of Foreign Key Constraint
03:30 Remove Foreign Key Constraint
04:10 What is a foreign key
04:30 Create Foreign Key
06:54 How to Find Foreign Key Constraint
References.
The below links may also help you understand Foreign Keys
Foreign Keys
[ Ссылка ]
Difference Between Foreign Key and Primary Key
[ Ссылка ]
Foreign Key Check Contstaint
[ Ссылка ]
SQL Foreign Key Constraint - A Complete Tutorial
Теги
SQL Serversql foreign keysql server tutorialforeign key vs primary keyprimary keyprimary key and foreign key in sqlforeign key in sqlforeign key sql server management studioviolation of foreign key constraintforeign key sql server on deletesql foreign key insertdisable foreign key sql servercreate foreign keyadd foreign key constraintremove foreign key constraintbeginners tutorialdatabase administrationdbasqlforeign keyforeign key in dbms