Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Basic Syntax

Connect, choose a database, and create the table:

sudo mysql
USE appdb;

CREATE TABLE customers (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(255) NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_customers_email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

If the database does not exist yet, create it first as shown in MySQL create database. You can also name the database in the statement, as in CREATE TABLE appdb.customers, without USE.

Common Column Types

TypeUse for
INT, BIGINTWhole numbers and IDs
DECIMAL(10,2)Exact amounts
VARCHAR(n)Short text, up to n characters
TEXTLong text
DATEA day
DATETIMEDate and time
BOOLEANTrue or false
JSONJSON documents

Write INT, not INT(11): the number in brackets is a display width that MySQL 8.0 deprecated for integer types, and it never limited the value.

Keys, Defaults, and Constraints

  • PRIMARY KEY: one per table, unique and never NULL. InnoDB stores rows in primary key order, so give every table one.
  • AUTO_INCREMENT: the server assigns the next number when an insert leaves the column out. One per table, and the column must be indexed.
  • NOT NULL: the column must always have a value.
  • DEFAULT: the value used when an insert does not name the column.
  • UNIQUE: no two rows may share a value. NULL may still appear more than once.

Create Only If Missing

CREATE TABLE IF NOT EXISTS customers (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL
);

If the table exists, MySQL gives a warning instead of an error and leaves the table as it is, even when its columns differ. Use it in setup scripts that may run twice. To change an existing table, use ALTER TABLE.

Foreign Keys and Indexes

CREATE TABLE orders (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  customer_id INT UNSIGNED NOT NULL,
  total DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  status VARCHAR(20) NOT NULL DEFAULT 'new',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_orders_status_created (status, created_at),
  CONSTRAINT fk_orders_customer
    FOREIGN KEY (customer_id) REFERENCES customers (id)
    ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  • Matching types: the foreign key column must have the same type and sign as the column it points to, and both tables must use InnoDB.
  • ON DELETE: RESTRICT refuses to delete a customer who still has orders; CASCADE deletes the orders too. Choose on purpose.
  • Indexes: KEY adds an index. Index the columns you filter or sort by. To add one later, run CREATE INDEX idx_orders_created ON orders (created_at);.

Default Engine and Character Set

On MySQL 8.0 and 8.4, a table created without options uses the InnoDB engine and the utf8mb4 character set, which stores every Unicode character. The older utf8 (utf8mb3) does not. A table without a character set of its own inherits the one of its database, which is why naming ENGINE and DEFAULT CHARSET in the statement, as in the examples, keeps results the same everywhere.

Check What You Created

DESCRIBE orders;
SHOW CREATE TABLE orders\G

DESCRIBE lists columns, types, keys, and defaults. SHOW CREATE TABLE prints the full statement as the server stores it, including engine, character set, and constraint names. To list all tables, see MySQL show tables.

Copy a Table

CREATE TABLE orders_archive LIKE orders;
CREATE TABLE orders_2026 AS SELECT * FROM orders WHERE created_at >= '2026-01-01';

LIKE copies the structure with its indexes but no rows and no foreign keys. AS SELECT copies the rows and column types but no primary key, no indexes, and no AUTO_INCREMENT attribute; add them afterward. On a large, busy table, run such a copy at a quiet time.

MariaDB Notes

  • Same syntax: every statement above works on MariaDB.
  • JSON: an alias for LONGTEXT with a validity check, not a binary type.
  • CREATE OR REPLACE TABLE: replaces an existing table with the new, empty one in one statement, so its rows are gone. Use it only on tables you can rebuild.
  • Character set: defaults differ between versions and packages, so name utf8mb4 explicitly. More differences are in MariaDB vs MySQL.
Was this answer helpful?
Loading...