SQL - MySQL

Module 01 - Database Theory
1. Introduction to Databases2. DBMS Theory Concepts3. Types of Keys4. Database Relationships5. DBMS Interview Questions
Module 02 - CRUD Operations
1. Create - INSERT2. Read - SELECT3. Update - UPDATE4. Delete - DELETE5. Alter - ALTER TABLE
Module 03 - Querying
1. Joins2. Filtering and sorting3. Practice - Filtering and Sort...4. Aggregate functions5. Practice - Aggregate Function...
Module 04 - Data Integrity
1. Constraints in Depth2. Transactions
MySQL Playground
ProfileProfile
Akkal DhamiFull Stack Developer

Building modern web experiences with a focus on performance, scalability, and clean architecture.

© 2026 | Akkal Dhami | All rights reserved

Built with
byAkkal Dhami

Navigation

  • Projects
  • Dev Setup
  • Playbook
  • Templates
  • Networking
  • SQL - MySQL
  • SQL Playground
  • System Design
  • DSA
AKKAL DHAMIAKKAL DHAMIAKKAL DHAMI

Module 04 - Data Integrity

1. Constraints

Constraints are rules attached to columns that stop invalid data from ever being saved. We've already used NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY throughout earlier modules.

This lesson covers the ones we've only touched briefly: CHECK, DEFAULT, and what actually happens to related rows when a referenced row is deleted or updated.

example.sql
CREATE TABLE books (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    isbn VARCHAR(13) NOT NULL UNIQUE,
    price DECIMAL(6,2) NOT NULL CHECK (price > 0),
    in_stock BOOLEAN DEFAULT TRUE
);
ConstraintEnforces
CHECK (condition)Value must satisfy a condition before it's saved
DEFAULT valueFallback value used when none is provided
ON DELETE RESTRICTBlocks deleting a parent row with existing children
ON DELETE CASCADEDeletes child rows automatically along with the parent
ON DELETE SET NULLClears the child's foreign key instead of deleting it
ON UPDATE CASCADEUpdates child foreign keys automatically if the parent key changes

2. Transactions

A transaction is a group of SQL operations that execute as a single unit — either all of them succeed, or none of them do. If anything fails partway through, everything gets undone, leaving the database exactly as it was before.

example.sql
START TRANSACTION;
 
UPDATE accounts
SET balance = balance - 1000
WHERE id = 1;
 
UPDATE accounts
SET balance = balance + 1000
WHERE id = 2;
 
COMMIT;
example.sql
START TRANSACTION;
 
UPDATE accounts
SET balance = balance - 1000
WHERE id = 1;
 
UPDATE accounts
SET balance = balance + 1000
WHERE id = 2;
 
ROLLBACK;