Interview Questions and Answers
Difference between Primary Key and Foreign Key
Primary Key
Foreign Key
Primary key uniquely identify a record in the table.
Foreign key is a field in the table that is primary key in another table.
Primary Key can't accept null values.
Foreign key can accept multiple null value.
By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index.
Foreign key do not automatically create an index, clustered or non-clustered. You can manually create an index on foreign key.
We can have only one Primary key in a table.
We can have more than one foreign key in a table.
Unlike primary keys, foreign keys can contain duplicate values. Also, it is OK for them contain NULL values.
Defining Primary key and Foreign key
--Create Parent Table CREATE TABLE Department ( DeptID int PRIMARY KEY, --define primary key Name varchar (50) NOT NULL, Address varchar(100) NULL ) GO --Create Child Table CREATE TABLE Employee ( EmpID int PRIMARY KEY, --define primary key Name varchar (50) NOT NULL, Salary int NULL, --define foreign key DeptID int FOREIGN KEY REFERENCES Department(DeptID) )
Important Note
- Foreign keys do not automatically create an index, clustered or non-clustered. You must manually create an index on foreign keys.
- There are actual advantages to having a foreign key be supported with a clustered index, but you get only one per table. What's the advantage? If you are selecting the parent plus all child records, you want the child records next to each other. This is easy to accomplish using a clustered index.
- Having a null foreign key is usually a bad idea. In the example below, the record in [dbo].[child] is what would be referred to as an "orphan record". Think long and hard before doing this.
Post a Comment
0 Comments