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.
CREATE TABLE table_name (
column1 datatype [constraints],
column2 datatype [constraints],
...
[table constraints]
);A first table
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:
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:
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.
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:
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:
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:
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 failsDrop 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?
- Structure and data
- Only the structure (columns and indexes)
- Only the data
- Only the primary key
What is the difference between DROP TABLE and TRUNCATE TABLE?
- None
- DROP removes the table itself; TRUNCATE removes only the rows
- TRUNCATE removes the table; DROP only the rows
- DROP can be rolled back
Why can't you drop customers in the shop database?
- It is a system table
- Other tables reference it through a foreign key
- It has too many rows
- Tables can only be dropped when empty