Middle
Tell about the keys in relational databases.
sobes.tech AI
Answer from AI
In relational databases, keys are used to identify and link data:
- Primary Key — a unique identifier for a record in a table. It cannot contain NULL and must be unique.
- Foreign Key — a field or set of fields that references the primary key of another table, establishing a relationship between the tables.
- Unique Key — ensures the uniqueness of values in a column or set of columns but may allow NULLs (depending on the DBMS).
- Composite Key — a key consisting of multiple columns that together uniquely identify a record.
- Alternate Key — any unique key that is not the primary key.
Example:
CREATE TABLE Users (
user_id INT PRIMARY KEY,
email VARCHAR(255) UNIQUE,
username VARCHAR(100),
CONSTRAINT fk_role FOREIGN KEY (role_id) REFERENCES Roles(role_id)
);