- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Primary key and Foreign key in SQL Database
In the relationaltonal database key is the most important element to maintain the relationship between two tables or to uniquely identify data from the table. Primary key is used to identify data uniquely therefore two rows can’t have the same primary key. It can’t be null.
On the other hand, foreign key is used to maintain relationship between two tables. Primary of a table act as forgein key in the other table. Foreign key in a table enforce Referential Integrity constraint. It can be more than one in the table.
Sr. No. | Key | Primary Key | Foreign Key |
---|---|---|---|
1 | Basic | It is used to uniquely identify data in the table | It is used to maintain relationship between tables |
2 | Null | It can’t be null | It can accept the null values |
3 | Duplicate | Two or more rows can’t have same primary key | It can carry duplicate value for a foreign key attribute |
4 | Index | Primary has clustered index | By default, It is not clustered index |
5 | Tables | Primary key constraint can be defined on temporary tables | It can’t be defined on temporary tables |