In SQL, unique constraint keep in check that all values of a column or columns are unique. Unique constraints make sure no duplicate values are present in the database table.
CREATE TABLE test_table ( employee_id INT PRIMARY KEY, email VARCHAR(100) UNIQUE, employee_name VARCHAR(50) );
Here are some characteristics of unique constraint-
- Uniqueness – It makes sure all values are unique.
- Only one NULL value – One NULL value is allowed per UNIQUE column
- Performance benefits – Unique constraints often create an index on the column(s) to improve query performance.
- Multiple Unique Columns – There are no restrictions on the number of columns with unique constraints.
Primary key vs Unique Constraint
Both Primary key and unique constraint are used in DBMS to ensure data integrity.
Here are the differences between Primary key and unique constraints-
Uniqueness and Non-NULL Values :
Both Primary key and unique constraints are used to make sure all values are unique. While a column with unique constraint allow a single NULL value, primary key column does not allow any NULL value(s).
Number Of Constraints:
A table can have multiple columns with unique constraints but it can only have one primary key column.
Thanks For Reading!
Read More : Learn SQL Fast