Junior — Middle
Explain what is meant by the concept of a unique key in a database?
sobes.tech AI
Answer from AI
A unique key in a database is a column or a set of columns whose values uniquely identify each record in a table. It ensures that there are no two rows with the same unique key value in the table. Unlike a primary key, a unique key can contain NULLs (depending on the DBMS), and a table can have multiple unique keys.
Example in SQL:
CREATE TABLE Users (
id INT PRIMARY KEY,
email VARCHAR(255) UNIQUE
);
Here, email is a unique key that prevents adding two users with the same email.