Wednesday, August 14, 2024

Difference between Primary key vs Unique key in a Table

The primary key and unique key are two important concepts in a relational database and are used to uniquely identify a row in a table. Both primary key and unique keys can identify a row uniquely but there is some subtle difference between them which we will see in this article. In fact, primary key vs unique is a popular SQL interview question along with classics like truncate vs delete and  How to manage transactions in a database, mostly asked to fresher and 2 to 3 years experience guys in any programming language. 


By the way, SQL is not just limited to any DBA or PLSQL developer but it's an important skill even for Java programmers and you can expect SQL interview questions even in many Java interviews.

Some time programmer also confuses between a unique key and a foreign key, which is the primary key of other tables in relation, hence questions like the difference between a primary and foreign key or a primary and unique key, or foreign and unique keys are asked to check their understanding.

The key thing to remember is that they help to keep data integrity in check. The unique key ensures that no duplicate values are inserted in the database, while a primary and foreign key is responsible for maintaining referential integrity.

They are also used to link two tables when you are joining them. Btw, there is a slight difference in unique key and unique key constraint, former is a concept while the latter is an implementation that ensures that no duplicate key is inserted into your tables.


Difference between primary key and unique key in a table

As I said both primary and unique key uniquely identifies each row in the table but there is some subtle difference between them. here are some of them :

1. Unique key in a table can be null, at least one but the primary key cannot be null in any table in a relational database like MySQL, Oracle, etc.

2. The unique key is represented using a unique constraint while a primary key is created using a primary key constraint in any table and it's automatically got a unique constraint. You can also see these Database and SQL courses from Udemy and from Pluralsight for more details on how unique key constraint works.

Difference between Primary key and Unique key in SQL



3. The primary key can be a combination of more than one unique key in the same table.

4. There can be only one primary key per table in relation to databases like MySQLOraclePostgreSQLSQLServer, or Sybase but there can be more than one unique key per table.

5. Many database engines automatically put a clustered index on the primary key and since you can only have one clustered index per table, it's not available to any other unique key at the same time. This is true for both Oracle and MySQL Server

Here is a nice summary of the difference between a primary key and a unique key in a table. These differences are valid on most of the relational databases like Oracle, MySQL, and SQL Server:

Difference between primary and unique key in table SQL


These were some of the differences between the primary key and the unique key in SQL or any table. Its' also one of those SQL interview questions which you don't like to miss before going for any programming interview or any database, SQL interview.

No comments:

Post a Comment