Wednesday, July 3, 2019

SQL Server - How to create Foreign Key constraints?

Constraints are the rules and restrictions to apply on the type of data in a table. They specify the limit on the type of data that can be stored in a particular column in a table using constraints and also maintain the data integrity and accuracy in the table. They also ensure the unwanted data can't be inserted into tables.
The basic fundamental is that A Not null constraint restrict the insertion of null values into a column. 
Foreign Key Constraint is another feature of SQL which is also known as referential integrity constraint and responsible to maintain the integrity relationship between two tables. Hence, a Foreign Key is a field in a data table that is a Primary key in another table. A table can have more than one foreign key and can have multiple referential relationships to more than one table also.
They cannot accept null but can have multiple duplicate data as well.

How can we create Foreign Key constraints?
A table can have more than one foreign key and we can create this constraint at two levels -
Column Level Foreign Key Constraints - We can define Foreign Key constraint with CREATE TABLE statement or in time with table definition. 
For example, the below query creates a table Employee with the column fields EmpId with Primary Key constraint, FirstName, LastName and Age as NOT NULL but MobileNo column with Unique constrain, IsActive column with Default constraint and DeptId with Foreign Key Constraint references Department table at DeptId column . Thus, Age column has some specific check constraint to validate the data before inserting into the data table.
---- Column Level Primary Key Constraint
CREATE TABLE TBL_DEPARTMENT
(
DeptId int NOT NULL Primary Key,
DeptName varchar(25) NOT NULL,
IsActive Bit Default(1)
)
---- Insert values into table
Insert Into TBL_DEPARTMENT
(DeptId, DeptName) Values(101,’Sales & Marketing’)

---- Column Level Foreign Key Constraint
CREATE TABLE TBL_EMPLOYEE
(
EmpId int NOT NULL Primary Key,
FirstName varchar(25) NOT NULL,
LastName varchar(25) NOT NULL,
Age int NOT NULL CHECK(Age>=18),
MobileNo Bigint Unique,
IsActive Bit Default(1),
DeptId int Foreign Key References Tbl_Department(DeptId)
)

---- Insert values into table
INSERT INTO TBL_EMPLOYEE
(EmpId, FirstName, LastName, Age, MobileNo,DeptId)
VALUE
(101,’Ryan’,’Arjun’,22,7856124580,101)

Keep in your mind that if you are not going to assign a name for primary key constraint, Foreign Key Constraint, unique constraint, check constraint and default constraint then system/SQL server will generate system defined constraint names for the primary key, Foreign Key, unique, check and default constraint column.

Table Level Foreign Key Constraints -
You can create a Foreign Key constraint after creating the table with the help of Alter Table command as given below.
---- Table Level - Foreign Key being added to another column
ALTER TABLE TBL_EMPLOYEE
ADD Constraint  FK_Emp_DeptId Foreign Key (DeptId)
References Tbl_Department(DeptId)

---- drop Foreign Key Constraint
ALTER TABLE TBL_EMPLOYEE
Drop Constraint  FK_Emp_DeptId



What happen, if you have some data into your table?
If you have some data into your tables, for example employee table is in relation with department table with the help of deptId foreign key –
  1. If you want to delete some records from department table and these records are referenced to employee table then SQL Server will not allow this operation and will through errors.
  2. If you want to delete some records from the department table and these records are referenced to employee table then you should remove all the referenced data from the employee table. After that, you can remove these records from department table
  3. If you want to drop department table containing referenced to employee table based on deptId foreign key, then you have to forcefully remove this relationship from employee table by dropping the foreign key deptId.
  4. You cannot insert null value for a foreign key column because it would be primary key in the another table and primary key never support null values.
To watch a live demo, please visit our YouTube Channel -

No comments:

Post a Comment