Same name: fine. Same number: refused.
Two students in your class are both called Rahul Sharma. Same name, same branch, sometimes even the same birthday. Yet the exam cell never mixes up their marks, and the library never fines the wrong one.
Every college solved this long ago with one number on your ID card. A database does the same thing, and then goes further: it refuses any row that would break its rules.
See it#
Here is a college's students table:
| roll_no | aadhaar | email (@college.in) | name | age | dept_id |
|---|---|---|---|---|---|
| 101 | …3333 | asha | Asha | 19 | CSE |
| 102 | …6666 | ravi | Ravi | 20 | ECE |
Now try to add four new rows, and watch the database say no to each one:
| The row you try to add | What the database says |
|---|---|
| a student with no roll_no | refused: a student must have a roll number |
| a second student with roll_no 101 | refused: 101 already belongs to Asha |
| a student whose aadhaar is Asha's | refused: that Aadhaar is already in use |
| a 16 year old student | refused: students must be at least 18 |
No app code checked any of this. The table itself carries the rules, so every app that writes to it, the admission portal, the hostel app and the library system alike, gets the same answer.
Name it#
Two ideas did that work. A key is a column, or a set of columns, whose value picks out exactly one row of a table.
A constraint is a rule written into the table that the database checks on every insert and update, refusing any row that breaks it. Keys are one kind of constraint; this lesson covers the others too.
These rules are what What is a database called data integrity: a CHECK is how the bank refuses a balance of minus ₹50,000.
How it works#
1. Candidate keys and super keys#
One candidate becomes the primary key.
Here is the same table with the two Rahul Sharmas added. Which columns could pick out one student on their own?
- roll_no could: no two students share one.
- aadhaar could: every Indian has their own.
- email could: the college gives each student a different one.
- name couldn't: two students can share a name, like the two Rahul Sharmas.
Any column, or set of columns, that identifies a row is a super key. So roll_no is a super key, and so are (roll_no, name) and (aadhaar, age): adding extra columns to something unique keeps it unique. A candidate key is a super key with nothing extra in it, so removing any column would break it. Here the candidate keys are roll_no, aadhaar and email.
2. The primary key, and the alternate keys#
The designer picks one candidate key as the table's main identifier: the primary key. Here it's roll_no, because it's short, never changes, and the college already prints it on every ID card.
The candidate keys that weren't picked, aadhaar and email, are the alternate keys. They still can't repeat, but other tables won't use them to point at a student.
A primary key has two rules that the database enforces:
- It must be unique. A second row with roll_no 101 is refused.
- It can never be NULL. A row with no roll_no is refused, because "unknown" can't identify anybody. This rule is called entity integrity.
3. A composite key#
Two columns, one unique ID.
Sometimes no single column is unique, but two together are. An enrollments table records which student takes which course. Asha takes DBMS and OS, and Ravi takes DBMS, so each column repeats on its own: roll_no 101 appears twice, and so does DBMS. The pair (roll_no, course_id) is unique, though: Asha can't enroll in DBMS twice. A primary key made of more than one column is a composite key.
4. Foreign keys link tables#
Foreign keys point at real rows.
| dept_id | dept_name |
|---|---|
| CSE | Computer Science |
| ECE | Electronics |
The students table has a dept_id column, and every value in it must be a real department in the departments table, where dept_id is the primary key. A column whose values must match the primary key of another table is a foreign key.
It stops a student being put in a department that doesn't exist: a row with dept_id MBA is refused, because there is no MBA in departments. This rule is called referential integrity. Notice that dept_id can repeat in students, since hundreds of students share CSE, so it's a foreign key, not a candidate key.
A foreign key may also be NULL, meaning "no department yet". What it can't hold is a value that isn't in departments.
5. What happens when the row being pointed at is deleted#
Delete a department, its students go.
Now someone deletes a department that students still point to. The foreign key decides what happens, through its ON DELETE rule:
- No rule given (the default, NO ACTION or RESTRICT): the delete is refused with an error. The database won't leave students pointing at a department that's gone.
- ON DELETE CASCADE: the delete carries over. If the ECE department is deleted while 40 students belong to it, those 40 student rows go too. That's what happens to your posts when you delete an Instagram account.
- ON DELETE SET NULL: the students stay, and their dept_id becomes NULL.
6. NOT NULL, UNIQUE and CHECK#
Constraints turn bad rows away.
Three more constraints guard single columns:
- NOT NULL: the column must have a value. A student with no name is refused.
- UNIQUE: no two rows may share the value. A second student with Asha's email is refused. Unlike a primary key, a UNIQUE column can usually hold NULL.
- CHECK: every row must pass a condition you write.
CHECK (age >= 18)refuses a 16 year old. NOT NULL alone would have let 16 through, since 16 isn't missing. One catch: WHERE keeps a row only when its test is true, but CHECK refuses a row only when its test is false. A NULL makes the test unknown, which is neither, so WHERE drops the row and CHECK lets it in. Add NOT NULL too when the value is required.
Why it matters#
Keys are how every app you use keeps people apart and tables linked. Your Instagram user id, your bank account number, your PNR on IRCTC and your Swiggy order id are all primary keys. Your Swiggy order's restaurant_id is a foreign key into the restaurants table. When a site says "this email is already registered", a UNIQUE constraint just refused your row.
Constraints matter because apps have bugs. A rule in the table is checked on every write, from every app, forever, so one buggy screen can't save a negative balance or an order for a customer who doesn't exist.
Interviews ask these as definitions and as "what happens" questions:
| Interviews call it | What you saw |
|---|---|
| Super key | any columns that identify a row, extras allowed |
| Candidate key | a super key with nothing extra: roll_no, aadhaar, email |
| Primary key | the candidate key picked: roll_no |
| Alternate key | the candidate keys not picked: aadhaar, email |
| Composite key | a primary key of two columns: (roll_no, course_id) |
| Foreign key | dept_id, which must match a row in departments |
| Entity integrity | a primary key can't be NULL |
| Referential integrity | a foreign key must point to a real row |
Code#
One CREATE TABLE per table, each constraint written where it applies. (In SQLite, run PRAGMA foreign_keys = ON; first: it checks foreign keys only when asked.)
Each of these is refused, for the reason in its comment:
And with ON DELETE CASCADE, deleting a department removes its students along with it:
What to remember#
- A key picks out one row. Super key: any columns that do it. Candidate key: a super key with nothing extra. Primary key: the candidate you choose. Alternate keys: the others.
- A primary key is unique and never NULL. A composite key is a primary key made of two or more columns.
- A foreign key must match the primary key of another table. Deleting a row it points to is refused by default, and carried over with ON DELETE CASCADE.
- NOT NULL demands a value, UNIQUE forbids repeats, and CHECK demands a condition, like age at least 18.