How to Create a Table in SQL
What you’ll build or solve
You’ll create a table that stores structured data and enforces rules such as uniqueness and required fields.
When this approach works best
This approach works best when:
Learn SQL on Mimo
- You are designing a new database schema.
- You are adding a new feature that requires structured storage.
- You want to enforce rules like required fields or unique values at the database level.
This is not the right approach if the table already exists and only needs modification. In that case, use ALTER TABLE.
Prerequisites
- Access to an SQL database
- Permission to create tables
- A selected database
If needed:
SQL
USE database_name;
Step-by-step instructions
Step 1: Write a CREATE TABLE statement with columns, types, and constraints
All table creation happens inside one statement:
SQL
CREATE TABLE table_name (
column definitions
);
You build complexity inside the parentheses.
Start with basic columns and data types
SQL
CREATE TABLE users (
id INT,
name VARCHAR(100),
email VARCHAR(150)
);
This defines three columns. At this stage, there are no rules beyond data types.
Add a primary key
A primary key uniquely identifies each row.
SQL
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(150)
);
Many systems support auto-generated IDs.
MySQL:
SQL
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(150)
);
PostgreSQL:
SQL
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(150)
);
Primary keys cannot contain duplicates or null values.
Add constraints for data integrity
Constraints enforce rules on your data.
SQL
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) UNIQUE,
active BOOLEAN DEFAULT TRUE
);
Here:
NOT NULLprevents empty values.UNIQUEprevents duplicates.DEFAULTsets automatic values.
All of these are part of the same CREATE TABLE statement.
Add foreign keys to link tables
Foreign keys connect tables together.
First create the parent table:
SQL
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(100)
);
Then create the related table:
SQL
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT,
total DECIMAL(10,2),
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
The referenced table must already exist.
Prevent errors with IF NOT EXISTS
If you might run the statement more than once, use:
SQL
CREATE TABLE IF NOT EXISTS users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(150)
);
This avoids errors if the table already exists.
What to look for
- Choose appropriate data types such as
INT,VARCHAR,DATE, orDECIMAL. - Primary keys uniquely identify rows.
- Constraints such as
NOT NULL,UNIQUE, andDEFAULTenforce rules. - Create parent tables before defining foreign keys.
- Check for syntax errors like missing commas or parentheses.
Examples you can copy
Example 1: Simple users table (MySQL)
SQL
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) UNIQUE
);
Example 2: Blog posts table
SQL
CREATE TABLE posts (
id INT PRIMARY KEY,
title VARCHAR(200) NOT NULL,
content TEXT,
published_at DATE
);
Example 3: Orders linked to customers
SQL
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT NOT NULL,
total DECIMAL(10,2),
created_at DATE,
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
Example 4: Table with defaults and timestamps
SQL
CREATE TABLE tasks (
id INT PRIMARY KEY,
title VARCHAR(150) NOT NULL,
completed BOOLEAN DEFAULT FALSE,
created_at DATE DEFAULT CURRENT_DATE
);
Common mistakes and how to fix them
Mistake 1: Missing comma between columns
You might write:
SQL
CREATE TABLE users (
id INT PRIMARY KEY
name VARCHAR(100)
);
Why it breaks:
SQL requires commas between column definitions.
Correct version:
SQL
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100)
);
Mistake 2: Creating a foreign key before the parent table exists
You might try:
SQL
CREATE TABLE orders (
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
Why it breaks:
The referenced table does not exist yet.
Correct approach:
Create the parent table first.
Mistake 3: Using the wrong data type
You might define prices as VARCHAR.
Why it breaks:
Numeric operations such as sorting or summing will not behave correctly.
Correct approach:
SQL
price DECIMAL(10,2)
Troubleshooting
If you see “permission denied,” confirm your user has CREATE privileges.
If you see “table already exists,” use IF NOT EXISTS or drop the table first.
If foreign key creation fails, confirm the referenced column is a primary key or unique.
If syntax errors appear, check commas, parentheses, and constraint placement.
Quick recap
- Use one
CREATE TABLEstatement. - Define columns and data types inside parentheses.
- Add primary keys and constraints within the same statement.
- Create parent tables before adding foreign keys.
- Use
IF NOT EXISTSto avoid duplicate errors.
Join 40M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot