Guided Notes

Constraints & AUTO_INCREMENT

NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT constraints, plus AUTO_INCREMENT keys and referential actions.

Why constraints

Slide 1 / 5

Constraints are rules the database enforces on every INSERT and UPDATE — so bad data can't get in, no matter which app or person writes it.

CREATE TABLE members ( member_id INT AUTO_INCREMENT PRIMARY KEY, email VARCHAR(100) NOT NULL UNIQUE, age INT CHECK (age >= 16), joined_on DATE DEFAULT (CURRENT_DATE) );

A statement that would break a rule fails with an error, and nothing is changed.

← Creating Databases & Tables