TExcelTAD
Home › MySQL › MySQL CREATE and DROP TABLE

MySQL CREATE and DROP TABLE

CREATE TABLE defines a new table: its name, its columns with their data types, and the rules (constraints) the data must follow.

sql
CREATE TABLE table_name (
    column1 datatype [constraints],
    column2 datatype [constraints],
    ...
    [table constraints]
);

A first table

mysql · try it yourself
CREATE TABLE reviews (
    review_id   INT AUTO_INCREMENT PRIMARY KEY,
    product_id  INT NOT NULL,
    rating      TINYINT NOT NULL,
    title       VARCHAR(100),
    body        TEXT,
    created_on  DATE NOT NULL
);

INSERT INTO reviews (product_id, rating, title, created_on)
VALUES (1, 5, 'Best beans ever', '2025-02-01'),
       (7, 4, NULL, '2025-02-14');

SELECT * FROM reviews;
+-----------+------------+--------+-----------------+------+------------+
| review_id | product_id | rating | title           | body | created_on |
+-----------+------------+--------+-----------------+------+------------+
|         1 |          1 |      5 | Best beans ever | NULL | 2025-02-01 |
|         2 |          7 |      4 | NULL            | NULL | 2025-02-14 |
+-----------+------------+--------+-----------------+------+------------+

Going through the columns:

Column Definition Meaning
review_id INT AUTO_INCREMENT PRIMARY KEY whole number, numbered automatically, identifies the row
product_id INT NOT NULL required whole number
rating TINYINT NOT NULL small integer (-128 to 127)
title VARCHAR(100) text up to 100 characters, optional
body TEXT long text (up to 64 KB), optional
created_on DATE NOT NULL required calendar date

The Data Types lesson lists all types. The constraints (NOT NULL, PRIMARY KEY, …) have lessons of their own.

Inspecting a table

In the mysql client, DESCRIBE reviews; (or SHOW COLUMNS FROM reviews;) shows the columns, and SHOW CREATE TABLE reviews; prints the full definition. Their exact layout differs a little between versions, so here we ask information_schema instead, which works the same everywhere:

mysql · try it yourself
CREATE TABLE reviews (
    review_id   INT AUTO_INCREMENT PRIMARY KEY,
    product_id  INT NOT NULL,
    rating      TINYINT NOT NULL,
    title       VARCHAR(100)
);

SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH AS max_len, IS_NULLABLE, COLUMN_KEY
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'shop' AND TABLE_NAME = 'reviews'
ORDER BY ORDINAL_POSITION;
+-------------+-----------+---------+-------------+------------+
| COLUMN_NAME | DATA_TYPE | max_len | IS_NULLABLE | COLUMN_KEY |
+-------------+-----------+---------+-------------+------------+
| review_id   | int       |    NULL | NO          | PRI        |
| product_id  | int       |    NULL | NO          |            |
| rating      | tinyint   |    NULL | NO          |            |
| title       | varchar   |     100 | YES         |            |
+-------------+-----------+---------+-------------+------------+

SHOW TABLES; lists the tables of the current database:

mysql · try it yourself
SHOW TABLES;
+----------------+
| Tables_in_shop |
+----------------+
| categories     |
| customers      |
| employees      |
| order_details  |
| orders         |
| products       |
| suppliers      |
+----------------+

IF NOT EXISTS

Creating a table that already exists fails. CREATE TABLE IF NOT EXISTS skips it quietly instead. It doesn't compare definitions, though: if an older, different reviews table exists, it is left as it is.

mysql · try it yourself
CREATE TABLE IF NOT EXISTS categories (id INT);
SELECT COUNT(*) AS categories_still_there FROM categories;
CREATE TABLE categories (id INT);
+------------------------+
| categories_still_there |
+------------------------+
|                      6 |
+------------------------+
ERROR 1050 (42S01) at line 3: Table 'categories' already exists

Copying a table's structure

CREATE TABLE new LIKE old copies the column definitions, indexes and primary key, but no data (and no foreign keys). Combined with INSERT ... SELECT it makes a full copy:

mysql · try it yourself
CREATE TABLE products_backup LIKE products;
INSERT INTO products_backup SELECT * FROM products;
SELECT COUNT(*) AS copied FROM products_backup;
+--------+
| copied |
+--------+
|     20 |
+--------+

DROP TABLE

DROP TABLE removes the table and its data for good. IF EXISTS avoids an error when it isn't there; several tables can be dropped at once:

mysql · try it yourself
CREATE TABLE temp_a (x INT);
CREATE TABLE temp_b (x INT);
DROP TABLE temp_a, temp_b;
DROP TABLE IF EXISTS temp_a;
SHOW TABLES LIKE 'temp%';
SELECT 'both gone' AS result;
+-----------+
| result    |
+-----------+
| both gone |
+-----------+

SHOW TABLES LIKE 'temp%' printed nothing: no table matches any more.

MySQL refuses to drop a table that other tables still reference through a foreign key. In the shop database, orders.customer_id points at customers:

sql
DROP TABLE customers;
-- MySQL 8: ERROR 3730 (HY000): Cannot drop table 'customers' referenced by a foreign key
--          constraint 'orders_ibfk_1' on table 'orders'.
-- MariaDB: ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails

Drop the referencing table first (or the foreign key, with ALTER TABLE).

To empty a table but keep it, use TRUNCATE TABLE (see the DELETE lesson).

Test yourself

What does CREATE TABLE t2 LIKE t1; copy?

  1. Structure and data
  2. Only the structure (columns and indexes)
  3. Only the data
  4. Only the primary key

What is the difference between DROP TABLE and TRUNCATE TABLE?

  1. None
  2. DROP removes the table itself; TRUNCATE removes only the rows
  3. TRUNCATE removes the table; DROP only the rows
  4. DROP can be rolled back

Why can't you drop customers in the shop database?

  1. It is a system table
  2. Other tables reference it through a foreign key
  3. It has too many rows
  4. Tables can only be dropped when empty