MySQL DDL and Schema Design Cheatsheet

create, alter, rename, and drop databases and tables in MySQL, with practical patterns for primary keys, unique keys, and foreign keys.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

create and alter tables

Core DDL for databases, tables, columns, constraints, and defaults.

Create a database

Create a schema if it does not already exist.

sqlANYcreatedatabaseutf8mb4
sql
CREATE DATABASE IF NOT EXISTS app_db CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;

Using `utf8mb4` is the standard choice for full Unicode support.

Create a users table

Create a table with an auto-increment primary key and timestamps.

sqlANYcreate-tableusersprimary-key
sql
CREATE TABLE users (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  email VARCHAR(255) NOT NULL,
  full_name VARCHAR(255) NOT NULL,
  is_active TINYINT(1) NOT NULL DEFAULT 1,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_users_email (email)
) ENGINE=InnoDB;

A common baseline table definition for application data.

Add a column

Add a nullable column to an existing table.

sqlANYalter-tableadd-column
sql
ALTER TABLE users ADD COLUMN phone VARCHAR(50) NULL AFTER email;

`AFTER` controls physical column position in MySQL table metadata.

Modify a column definition

Change type, size, or nullability.

sqlANYalter-tablemodify-column
sql
ALTER TABLE users MODIFY COLUMN phone VARCHAR(100) NULL;

Use `MODIFY COLUMN` when you are changing the definition but not renaming the column.

Rename a column

Rename a column while specifying its full definition.

sqlANYalter-tablerename-column
sql
ALTER TABLE users CHANGE COLUMN full_name display_name VARCHAR(255) NOT NULL;

In MySQL, `CHANGE COLUMN` renames a column and requires the new full definition.

Drop a column

Remove a column from a table.

sqlANYalter-tabledrop-column
sql
ALTER TABLE users DROP COLUMN phone;

Be careful in production because this is destructive.

Rename a table

Rename a table without recreating it.

sqlANYrenametable
sql
RENAME TABLE users TO app_users;

Useful during refactors or when aligning schema names with application conventions.

Drop a table if it exists

Delete a table definition and all rows.

sqlANYdrop-tabledestructive
sql
DROP TABLE IF EXISTS old_logs;

Always double-check the target table before dropping in shared environments.

constraints and keys

Primary keys, unique keys, foreign keys, and check-style patterns.

Add a unique key

Prevent duplicate values for a column or column pair.

sqlANYuniqueconstraintindex
sql
ALTER TABLE users ADD CONSTRAINT uq_users_email UNIQUE (email);

Unique constraints are commonly used for logins, slugs, and external identifiers.

Create a table with a foreign key

Reference a parent table and enforce relational integrity.

sqlANYforeign-keyorderscascade
sql
CREATE TABLE orders (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  status VARCHAR(50) NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_orders_user_id (user_id),
  CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES users(id)
    ON DELETE CASCADE
    ON UPDATE RESTRICT
) ENGINE=InnoDB;

The explicit index on the foreign key column helps join and lookup performance.

Drop a foreign key

Remove a foreign key constraint from a table.

sqlANYforeign-keydrop
sql
ALTER TABLE orders DROP FOREIGN KEY fk_orders_user;

You may also need to drop the supporting index separately if you no longer need it.

Create a table with a composite primary key

Use multiple columns as the logical primary key.

sqlANYcomposite-keyjunction-table
sql
CREATE TABLE user_roles (
  user_id BIGINT UNSIGNED NOT NULL,
  role_code VARCHAR(50) NOT NULL,
  granted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (user_id, role_code)
) ENGINE=InnoDB;

Composite keys are common in mapping or junction tables.

Drop a database

Delete an entire schema and all objects inside it.

sqlANYdrop-databasedestructive
sql
DROP DATABASE IF EXISTS sandbox_db;

This is intentionally destructive and should be reserved for controlled environments.

Recommended next

No recommendations yet.