What are the difference between Primary Keys and Foreign Keys?
Primary key:
Primary keys are the unique identifiers for each row. They must
contain unique values and cannot be null. Due to their importance in relational
databases, Primary keys are the most fundamental aspect of all keys and
constraints. A table can have only one primary key.
- The PRIMARY KEY constraint uniquely identifies each record in a database table.
- Primary keys must contain unique values.
- A primary key column cannot contain NULL values.
- Each table should have a primary key, and each table can have only ONE primary key.
Foreign key:
Foreign keys are a method of ensuring data integrity and
manifestation of the relationship between tables. A FOREIGN KEY in one table
points to a PRIMARY KEY in another table.
Difference between
Primary Key & Foreign Key:
Primary Key
|
Foreign Key
|
|
Foreign key is a field
in the table that is primary key in another table.
|
|
Foreign key can accept
multiple null value.
|
|
Foreign key do not
automatically create an index, clustered or non-clustered. You can manually
create an index on foreign key.
|
|
We can have more than
one foreign key in a table.
|
No comments:
Post a Comment