0 · Recall Warm-up
Answer from memory first. Answers are in the Answer Vault at the bottom.
- Which constraint forbids NULL in a primary key? from Lesson 2.3.1
- What does ON DELETE CASCADE do? from Lesson 2.3.2
- Is SQL relationally complete? from Lesson 3.2.3
1 · The Hook
SQL (Structured Query Language) is what real systems speak: Oracle, PostgreSQL, MySQL, SQL Server, SQLite. It is both a DDL and a DML (Lesson 0.1.2). This lesson is the DDL half: how the college's tables, keys and referential actions are actually declared.
2 · Predict First
In SQL, a column declared UNIQUE: can it hold NULL? Can it hold NULL in two different rows?
Make a guess in your head. The answer opens Section 3.
3 · Core Concept
Predict First — the answer
Yes and yes (in standard SQL). UNIQUE forbids two equal non-NULL values; NULLs are not equal to each other (Lesson 2.1.2), so several NULLs are allowed. A PRIMARY KEY column, by contrast, can never hold NULL.
CREATE TABLE
CREATE TABLE DEPT (
dname VARCHAR(10) PRIMARY KEY,
building VARCHAR(30) NOT NULL
);
CREATE TABLE STUDENT (
roll INT PRIMARY KEY,
name VARCHAR(40) NOT NULL,
dept VARCHAR(10) REFERENCES DEPT(dname) ON DELETE SET NULL,
cgpa DECIMAL(3,1) CHECK (cgpa BETWEEN 0 AND 10),
email VARCHAR(60) UNIQUE
);
CREATE TABLE ENROLL (
roll INT,
cid CHAR(2),
grade CHAR(1) DEFAULT 'P',
PRIMARY KEY (roll, cid),
FOREIGN KEY (roll) REFERENCES STUDENT(roll) ON DELETE CASCADE,
FOREIGN KEY (cid) REFERENCES COURSE(cid)
);
Common data types
INT/INTEGER, SMALLINT, DECIMAL(p, s) / NUMERIC(p, s) (p digits, s after the point), REAL/FLOAT, CHAR(n) (fixed length, padded), VARCHAR(n) (variable length, at most n), DATE, TIME, TIMESTAMP, BOOLEAN.
Constraints
| Constraint | Meaning |
|---|---|
| NOT NULL | column may not be NULL |
| UNIQUE | no two rows with the same non-NULL value (NULLs allowed, several) |
| PRIMARY KEY | UNIQUE + NOT NULL; at most one per table; may be composite (table-level form) |
| FOREIGN KEY … REFERENCES T(col) | referential integrity (Lesson 2.2.2) |
| ON DELETE / ON UPDATE CASCADE | SET NULL | SET DEFAULT | RESTRICT | NO ACTION | referential actions (Lesson 2.3.2); default is NO ACTION (reject) |
| CHECK (condition) | row must not make the condition FALSE (UNKNOWN passes) |
| DEFAULT value | used when an INSERT omits the column |
Column-level form (written after a column) can only constrain that column; table-level form (separate line) is needed for composite keys.
CHECK subtlety: CHECK rejects a row only if the condition is FALSE. A NULL cgpa makes cgpa BETWEEN 0 AND 10 UNKNOWN, so the row is accepted.
Changing and removing tables
ALTER TABLE STUDENT ADD COLUMN phone VARCHAR(15);adds a column (existing rows get NULL or the default).ALTER TABLE STUDENT DROP COLUMN phone;ALTER TABLE … ADD CONSTRAINT …/DROP CONSTRAINT ….DROP TABLE ENROLL;removes the table and its schema. (Compare DELETE, next lesson, which removes rows only.) Dropping a table that others reference is rejected unlessCASCADEis added.
4 · Visual
PRIMARY KEY = UNIQUE + NOT NULL (one per table)
UNIQUE = no equal non-NULL values (many NULLs allowed)
CHECK(c) = reject row only if c is FALSE (UNKNOWN passes)
REFERENCES = value must exist in parent, or be NULL
DROP TABLE = rows + schema gone DELETE FROM = rows gone, schema stays
5 · Worked Examples
Example 1 Easy
Write the table-level constraint for a composite primary key (roll, cid).
Solution
PRIMARY KEY (roll, cid). A column-level PRIMARY KEY cannot cover two columns.
Example 2 Medium
STUDENT has email VARCHAR(60) UNIQUE. Three new students have no email. Can all three be inserted?
Solution
Yes. UNIQUE allows several NULLs; NULL values are not considered equal to each other.
Example 3 GATE
CHECK (cgpa >= 6) on a table. Which inserts succeed: cgpa 5.0, 6.0, NULL?
Solution
5.0 → FALSE → rejected. 6.0 → TRUE → accepted. NULL → UNKNOWN → accepted.
6 · GATE Traps
- At most one PRIMARY KEY per table, but many UNIQUE constraints.
- UNIQUE allows multiple NULLs; PRIMARY KEY allows none.
- CHECK passes UNKNOWN.
- The default referential action is NO ACTION (reject), not CASCADE.
- DROP TABLE removes structure; DELETE removes rows.
7 · Memory Hook
"PK = Unique + Not Null; CHECK only says no to a clear FALSE."
8 · Connect the Dots
9 · Self-Test
GATE format. Try every question before opening the vault.
Q1 · MCQ · 1 mark(s) — Constraints
A PRIMARY KEY constraint is equivalent to:
- (A) UNIQUE
- (B) NOT NULL
- (C) CHECK
- (D) UNIQUE and NOT NULL
Q2 · NAT · 1 mark(s) — UNIQUE
A column is declared UNIQUE (not NOT NULL). What is the maximum number of rows that can hold NULL in it, if the table has 10 rows?
Numerical answer: ________
Q3 · MCQ · 1 mark(s) — CHECK
Table T has CHECK (x > 0). Which insert is rejected?
- (A) x = 1
- (B) x = NULL
- (C) x = 5
- (D) x = 0
Q4 · MCQ · 1 mark(s) — Default action
If a FOREIGN KEY has no ON DELETE clause, deleting a referenced parent row:
- (A) sets children to the default
- (B) is rejected
- (C) sets children to NULL
- (D) cascades
Q5 · MSQ · 2 mark(s) — DDL
Which statements are true?
- (A) ALTER TABLE ADD COLUMN fills existing rows with NULL unless a default is given.
- (B) A table can have several UNIQUE constraints.
- (C) DROP TABLE removes the table's schema.
- (D) A composite primary key must be declared at table level.
10 · Rule Card
- CREATE TABLE with types (INT, DECIMAL(p,s), CHAR(n), VARCHAR(n), DATE …).
- PRIMARY KEY = UNIQUE + NOT NULL, one per table; UNIQUE allows many NULLs.
- FOREIGN KEY … REFERENCES … ON DELETE/UPDATE CASCADE | SET NULL | SET DEFAULT | RESTRICT | NO ACTION (default).
- CHECK rejects only FALSE; DEFAULT fills omitted columns.
- ALTER TABLE ADD/DROP column/constraint; DROP TABLE removes schema + rows.
11 · Concept Ledger
Unlocked in this lesson. From here on, these terms may be used.
SQLCREATE TABLEdata typesNOT NULLUNIQUEPRIMARY KEYFOREIGN KEY … REFERENCESCHECKDEFAULTALTER TABLEDROP TABLEcolumn-level vs table-level constraint
12 · Answer Vault
Reveal answers when you’re ready
Recall Warm-up
- Entity integrity.
- Deletes the child rows that reference a deleted parent, following chains.
- Yes.
Self-Test
Q1 Answer: (D) · 1 mark(s)
Both properties.
- (A) is wrong: UNIQUE allows NULL.
- (B) is wrong: NOT NULL allows duplicates.
- (C) is wrong: Different constraint.
Re-read: Lesson 3.3.1 — SQL DDL: CREATE TABLE, data types, constraints, ON DELETE/ON UPDATE
Q2 Answer: 10 · 1 mark(s)
NULLs never conflict with each other under UNIQUE.
Re-read: Lesson 3.3.1 — SQL DDL: CREATE TABLE, data types, constraints, ON DELETE/ON UPDATE
Q3 Answer: (D) · 1 mark(s)
0 > 0 is FALSE.
- (A) is wrong: TRUE.
- (B) is wrong: UNKNOWN passes.
- (C) is wrong: TRUE.
Re-read: Lesson 3.3.1 — SQL DDL: CREATE TABLE, data types, constraints, ON DELETE/ON UPDATE
Q4 Answer: (B) · 1 mark(s)
Default NO ACTION.
- (A) is wrong: Must be declared.
- (C) is wrong: Must be declared.
- (D) is wrong: Must be declared.
Re-read: Lesson 3.3.1 — SQL DDL: CREATE TABLE, data types, constraints, ON DELETE/ON UPDATE
Q5 Answer: (A) (B) (C) (D) · 2 mark(s)
All four are standard.
Re-read: Lesson 3.3.1 — SQL DDL: CREATE TABLE, data types, constraints, ON DELETE/ON UPDATE