Junior — Middle
What is the purpose of the UNIQUE keyword in SQL and in what cases is it used?
sobes.tech AI
Answer from AI
The UNIQUE keyword in SQL is used to constrain a column or set of columns so that all values in them are unique, meaning they do not repeat.
This helps ensure data integrity, for example, to prevent duplicates in a table for a specific field.
Example of usage:
CREATE TABLE users (
id INT PRIMARY KEY,
email VARCHAR(255) UNIQUE
);
In this example, the email column cannot contain duplicate values.
UNIQUE is often used for fields that must be unique but are not primary keys, such as phone numbers, logins, serial numbers, etc.