ON DELETE/ON UPDATE actions to control what happens to dependent rows when a parent row changes.
One-to-many relationships (user → videos)
A one-to-many relationship exists when a single row in a parent table (users) can be referenced by many rows in a child table (videos). For example, auser_id is unique in users but may appear multiple times in videos.
Without a foreign key constraint, MySQL will accept any integer in the user_id column of videos, including values that do not exist in users. A foreign key enforces referential integrity so all referenced values in the child table must exist in the parent table.
If you wanted:
- a one-to-one relationship — add
UNIQUEto the foreign key column in the child table; - a many-to-many relationship — create a join (linking) table.
Common foreign key actions (ON DELETE / ON UPDATE)
Use
ON DELETE CASCADE when the child table (e.g., videos) should not exist without the parent (e.g., users). Use ON DELETE SET NULL when you want to keep the child row but remove its parent reference.Create a database and two tables (example with ON DELETE CASCADE)
Run these statements to create the demo database and tables. This example uses ON DELETE CASCADE so deleting a user will delete their videos automatically.
ON DELETE SET NULL and allow the user_id column to be nullable:
ON DELETE SET NULL requires the foreign key column to accept NULL, so user_id should not be declared NOT NULL.
Inspecting table schema with DESCRIBE
UseDESCRIBE (or SHOW COLUMNS FROM) to check field names, types, nullability, keys, and auto-increment behavior.
Example DESCRIBE output (for the CASCADE version):
PRI= Primary KeyMUL= Multiple occurrences allowed (indexed column, typical for foreign keys)YESunderNullmeans the column acceptsNULL, which is required forON DELETE SET NULL.
Insert demo data (Create)
Insert users (auto-increment handlesuser_id):
user_id values that reference existing users:
Read data (SELECT)
Show all videos:Update data (UPDATE)
Update a single row by matching the primary key in theWHERE clause to avoid accidental bulk updates:
Delete data (DELETE)
Delete a specific video:Demonstrating cascade behavior
Ifvideos was created with ON DELETE CASCADE on user_id, deleting users.user_id = 1 will remove all videos referencing user_id = 1 automatically:
CRUD and SQL quick reference
Common misconception quiz (answer below):
A. CREATE TABLE is the SQL command used to create new records.
B. A primary key can appear multiple times in the same table.
C. A foreign key ensures data in one table matches values in another.
D. INSERT is used to read information from a table. Correct answer: C. Why:
- A is incorrect —
CREATE TABLEmakes the table structure;INSERTadds records. - B is incorrect — primary keys must be unique in a table.
- D is incorrect —
SELECTreads or retrieves data;INSERTwrites data.
Recap
- Primary keys uniquely identify rows.
- Foreign keys enforce relationships and referential integrity.
- Choose
ON DELETEactions (CASCADE,SET NULL, or defaultRESTRICT) based on application logic. - Use
DESCRIBEto inspect schema and ensure columns match the intended constraints. - Practice Create, Read, Update, Delete operations to solidify understanding.
Next steps
Try creating acomments table and perform CRUD operations on it. Experiment with different ON DELETE actions and observe how DESCRIBE changes. If you want to learn alternatives, explore NoSQL document stores such as MongoDB for non-relational approaches.
Links and references
- MySQL Reference Manual - Foreign Key Constraints
- SQL Language Reference (SELECT, INSERT, UPDATE, DELETE)