Junior — Middle
Why is data uniqueness constraint used in databases?
sobes.tech AI
Answer from AI
The uniqueness constraint in a database is used to ensure that the values in a specific column or set of columns are unique across all rows in the table. This helps to:
- Prevent data duplication, for example, so that two users do not have the same email.
- Ensure data integrity and correctness of business logic.
- Simplify search and record identification.
Example in SQL:
CREATE TABLE users (
id INT PRIMARY KEY,
email VARCHAR(255) UNIQUE
);
Here, the email column cannot contain duplicate values.