Concept Flow - Why constraints matter
Start: Insert or Update Data
Check Constraints
Pass
Data Saved
When data is added or changed, constraints check if it follows rules. If yes, data saves; if no, error stops it.
CREATE TABLE Users ( ID INT PRIMARY KEY, Email VARCHAR(100) UNIQUE NOT NULL ); INSERT INTO Users VALUES (1, 'a@example.com'); INSERT INTO Users VALUES (2, NULL);
| Step | Action | Data Attempted | Constraint Checked | Result |
|---|---|---|---|---|
| 1 | Create Table | Users with ID PK, Email UNIQUE NOT NULL | N/A | Table created |
| 2 | Insert | (1, 'a@example.com') | Primary Key, Unique, Not Null | Success: Data saved |
| 3 | Insert | (2, NULL) | Not Null on Email | Fail: NULL not allowed, insert rejected |
| Variable | Start | After Step 2 | After Step 3 |
|---|---|---|---|
| Users Table Rows | Empty | (1, 'a@example.com') | (1, 'a@example.com') |
Constraints are rules on table columns. They prevent bad or duplicate data. Common constraints: PRIMARY KEY, UNIQUE, NOT NULL. If data breaks rules, database rejects it. This keeps data clean and reliable.