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, alter, rename, and drop databases and tables in MySQL, with practical patterns for primary keys, unique keys, and foreign keys.
Core DDL for databases, tables, columns, constraints, and defaults.
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 table with an auto-increment primary key and timestamps.
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.
ALTER TABLE users ADD COLUMN phone VARCHAR(50) NULL AFTER email;`AFTER` controls physical column position in MySQL table metadata.
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 while specifying its full definition.
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.
ALTER TABLE users DROP COLUMN phone;Be careful in production because this is destructive.
RENAME TABLE users TO app_users;Useful during refactors or when aligning schema names with application conventions.
DROP TABLE IF EXISTS old_logs;Always double-check the target table before dropping in shared environments.
Primary keys, unique keys, foreign keys, and check-style patterns.
ALTER TABLE users ADD CONSTRAINT uq_users_email UNIQUE (email);Unique constraints are commonly used for logins, slugs, and external identifiers.
Reference a parent table and enforce relational integrity.
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.
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.
Use multiple columns as the logical primary key.
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 DATABASE IF EXISTS sandbox_db;This is intentionally destructive and should be reserved for controlled environments.