Data Types and Constraints

Lesson 3 of 20

Give every column a type and every table rules — then watch four guards reject the rows that break them.

The big picture

Types and rules, declared up front

A column type says what fits inside; a constraint says what is allowed. Declare both when the table is born.

Types guard values

INT holds whole numbers, VARCHAR(20) text up to 20 characters, DATE calendar days, DECIMAL(5,2) exact money.

PRIMARY KEY

The column that uniquely identifies each row — duplicates and NULLs are impossible.

NOT NULL & UNIQUE

NOT NULL forbids empty; UNIQUE forbids repeats. Two different guards, one column each.

DEFAULT & CHECK

DEFAULT fills in a value when the insert skips one; CHECK is a rule every row must satisfy.