Primary Key: -
- The Primary key is used to identify rows or data uniquely from the table hence table cannot have duplicate data.
- A table can have only one primary key.
- Automatically apply Cluster index to the column which has a primary key.
- It is not accepting null values.
Foreign Key :
- A table Primary key becomes a Foreign key in another table. It helps to maintain the relationship between two or more tables.
- A table can have multiple Foreign keys.
- Not apply cluster index.
- Accept null value
eg.
For example, We have two tables student and Course.
Student table has STUID which is primary key of Student Table and Foreign key in COURSE table.
STUDENT TABLE
| STUID (PRIMARY KEY) |
Name |
Address |
Phone No |
| 100 |
Kirtesh Shah |
Vadodara |
123456789 |
| 101 |
Nitya Shah |
Surat |
123456789 |
COURSE TABLE
| COURSEID (PRIMARY KEY) |
STUID (FORENIGN KEY) |
COURSE NAME |
| C1 |
100 |
MCA |
| C2 |
101 |
BSC |