1
Keys in DBMS ★★★★★
Database
| Key | Meaning |
|---|---|
| Super key | Any set of columns that uniquely identifies a row |
| Candidate key | Minimal super key (no extra column) |
| Primary key | The chosen candidate key: unique and NOT NULL, one per table |
| Alternate key | Candidate keys that were not chosen as primary |
| Foreign key | Column that refers to the primary key of another table |
| Composite key | Primary key made of two or more columns |
| Unique key | Unique values but NULL is allowed |
CREATE TABLE departments (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL UNIQUE
);
CREATE TABLE students (
roll INT PRIMARY KEY,
email VARCHAR(100) UNIQUE,
name VARCHAR(100) NOT NULL,
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES departments(id)
);
CREATE TABLE enrollments (
roll INT,
course_id INT,
grade CHAR(2),
PRIMARY KEY (roll, course_id) -- composite key
);