A Table with Rules
Result:
| id | salary | status | |
|---|---|---|---|
| 1 | a@corp.com | 50000.00 | active |
Naming constraints (uq_..., ck_...) makes error messages and later changes easier.
What Happens When a Rule Breaks
ERROR 1062: Duplicate entry 'a@corp.com' for key 'employees.uq_employees_email'
ERROR 3819: Check constraint 'ck_employees_salary' is violated.
ERROR 1364: Field 'email' doesn't have a default value
The NOT NULL error says "doesn't have a default value" because the column was left out of the insert. Naming the column with an explicit NULL gives error 1048 instead. Either way the row is refused in strict mode.
CHECK syntax and silently ignored it. This is a common surprise when moving a database between versions.
FOREIGN KEY Actions
Foreign keys need InnoDB. Choose what happens to child rows when the parent changes:
| Option | On DELETE or UPDATE of parent |
|---|---|
RESTRICT / NO ACTION (default) | Refuse the change |
CASCADE | Apply the same change to the children |
SET NULL | Set the child column to NULL (the column must allow NULL) |
SET DEFAULT | Not supported by InnoDB |
Result:
| id | title | author_id |
|---|---|---|
| 1 | 1984 | NULL |
| 2 | Emma | 20 |
Deleting Orwell set the book's author_id to NULL. Changing Austen's id to 20 updated the book automatically.
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails ('shop'.'orders', CONSTRAINT 'orders_ibfk_1' FOREIGN KEY ('customer_id') REFERENCES 'customers' ('id'))
Disabling Foreign Key Checks
During imports MySQL files often start with SET FOREIGN_KEY_CHECKS = 0;. It lets you load tables in any order, but it also lets bad data in. Turn it back on and verify the data after the import.
Result:
| orphan_order |
|---|
| 50 |
ALTER TABLE: Changing the Structure
| Task | Statement |
|---|---|
| Add a column | ALTER TABLE t ADD COLUMN phone VARCHAR(20) AFTER email; |
| Change a type | ALTER TABLE t MODIFY COLUMN phone VARCHAR(30) NOT NULL; |
| Rename a column | ALTER TABLE t RENAME COLUMN phone TO mobile; |
| Drop a column | ALTER TABLE t DROP COLUMN mobile; |
| Rename a table | RENAME TABLE t TO t_new; |
| Add an index | ALTER TABLE t ADD INDEX idx_city (city); |
| Add a foreign key | ALTER TABLE t ADD CONSTRAINT fk_x FOREIGN KEY (c) REFERENCES p(id); |
| Drop a foreign key | ALTER TABLE t DROP FOREIGN KEY fk_x; |
| Drop a check | ALTER TABLE t DROP CHECK ck_x; |
Result:
| id | name | sku | units_in_stock |
|---|---|---|---|
| 1 | Laptop | NULL | 15 |
| 2 | Phone | NULL | 30 |
Look at the structure afterwards:
Result:
| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| id | int | NO | PRI | NULL | (empty) |
| name | varchar(100) | NO | (empty) | NULL | (empty) |
| sku | varchar(20) | YES | (empty) | NULL | (empty) |
| category | varchar(50) | NO | (empty) | NULL | (empty) |
| price | decimal(10,2) | NO | (empty) | NULL | (empty) |
| stock | int | NO | (empty) | NULL | (empty) |
ERROR 3819: Check constraint 'ck_products_price' is violated.
ERROR 1060: Duplicate column name 'price'
Big Tables: Online DDL
ALTER TABLE on a table with millions of rows can take a long time and block writes. InnoDB supports online DDL for many changes. State your wish explicitly, and MySQL refuses if it cannot comply instead of silently locking:
INSTANT adds a column in 8.0 without rebuilding the table. For changes that need a full rebuild, tools such as gh-ost or pt-online-schema-change copy the table in the background. Always test on a copy of production data first (these statements were not run on a big table here).
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Unnamed constraints | Hard to drop later | Name them fk_, uq_, ck_ |
| Using MyISAM tables with foreign keys | Keys are ignored | Use InnoDB (the default) |
Leaving FOREIGN_KEY_CHECKS=0 | Orphan rows appear | Turn it back on and verify |
ALTER TABLE on a huge table during peak hours | Long locks | Online DDL or a migration tool, off-peak |
| Expecting CHECK to work on old versions | Rules silently ignored | Use MySQL 8.0.16+ |
Forgetting NOT NULL when using MODIFY | Column becomes nullable again | Repeat the full definition |
Practice
- Add a
CHECKthatstock >= 0on products and test it. - Create
coursesandenrollmentswith a cascading foreign key and test a delete. - Add a nullable
phonecolumn, then make itNOT NULLwith a default. - Find orphan rows after loading with
FOREIGN_KEY_CHECKS = 0.
Conclusion
Constraints keep MySQL data correct: NOT NULL, UNIQUE, CHECK (8.0.16+), FOREIGN KEY with RESTRICT, CASCADE or SET NULL. Name them, use InnoDB, change structure with ALTER TABLE, and treat large alterations with online DDL.

