SQL UNIQUE
What is SQL UNIQUE?
A UNIQUE constraint in SQL is a database constraint that ensures the uniqueness of values in one or more columns within a table. It guarantees that all values in the specified column(s) are unique, meaning no two rows can have the same values in the constrained column(s). Attempting to insert or update a record with a duplicate value in the constrained column(s) will result in an error, preventing the duplication of data.
When you would use it
You would use a UNIQUE constraint in SQL when you need to:
-
Enforce Data Integrity: Ensure that specific columns or combinations of columns contain unique values, preventing data duplication.
-
Implement Business Rules: Enforce business rules that require uniqueness, such as ensuring that each email address or employee ID is unique in a database.
-
Improve Query Performance: Optimize query performance by using unique constraints on columns that are frequently used as search criteria.
-
Define Primary Keys: UNIQUE constraints are often used to define primary keys, which uniquely identify each row in a table.
Syntax
The syntax for creating a UNIQUE constraint in SQL varies slightly between database systems, but here's a general representation:
CREATE TABLE table_name (
column1 data_type,
column2 data_type,
-- ...
CONSTRAINT constraint_name UNIQUE (column1, column2)
);
table_name
: The name of the table for which you're creating the constraint.column1, column2, ...
: The column(s) for which you want to enforce uniqueness.constraint_name
: An optional name for the UNIQUE constraint.
Parameter values
table_name
: The name of the table where the UNIQUE constraint is being applied.column1, column2, ...
: The column(s) for which you want to ensure uniqueness. You can apply a UNIQUE constraint to a single column or a combination of columns.constraint_name
: An optional name for the UNIQUE constraint, which can be used to reference the constraint later.
Example query
Here's an example SQL query that creates a table "employees" with a UNIQUE constraint on the "employee_id" column:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100) UNIQUE,
-- Other columns
);
Example table response
The "employees" table is created with a UNIQUE constraint on the "email" column, ensuring that each email address is unique. The table might contain data like this:
employee_id | first_name | last_name | |
---|---|---|---|
1 | John | Doe | john.doe@example.com |
2 | Jane | Smith | jane.smith@example.com |
3 | Bob | Johnson | bob.j@example.com |
Use cases
-
Ensuring Data Integrity: To prevent duplicate data in columns that require uniqueness, such as email addresses, usernames, or product codes.
-
Defining Primary Keys: UNIQUE constraints are often used to define primary keys, which uniquely identify each row in a table.
-
Enforcing Business Rules: To enforce specific business rules, such as ensuring that customer IDs or invoice numbers are unique.
-
Optimizing Query Performance: UNIQUE constraints on columns used for searching and filtering data can improve query performance.
SQL Languages Availability
The concept of UNIQUE constraints is available in most SQL-based relational database management systems (RDBMS), including but not limited to:
- MySQL
- PostgreSQL
- Microsoft SQL Server
- Oracle Database
- IBM Db2
- SQLite
However, the exact implementation and syntax may vary slightly between different database systems. Always consult your specific RDBMS documentation for the most accurate and detailed information on using UNIQUE constraints.