SQL CREATE TABLE Get Certified
Learn how to create tables, define columns and data types, apply constraints, and insert data into newly created tables.
What is CREATE TABLE?
The CREATE TABLE statement defines a new table in the database.
You specify the column names, their data types, and any constraints that protect the integrity of the data.
Basic Syntax
CREATE TABLE table_name (
column1 data_type,
column2 data_type,
column3 data_type
);
Creating a Simple Table
What this code does: Creates a students table with four columns, inserts three rows and returns them.
DROP TABLE IF EXISTS students;
CREATE TABLE students (
student_id INTEGER,
student_name TEXT,
age INTEGER,
course TEXT
);
INSERT INTO students VALUES
(1, 'Aarav', 21, 'Physics'),
(2, 'Meera', 22, 'Chemistry'),
(3, 'Kabir', 20, 'Mathematics');
SELECT *
FROM students
ORDER BY student_id;
CREATE TABLE with PRIMARY KEY
A primary key uniquely identifies each row. In SQLite an INTEGER PRIMARY KEY column automatically receives sequential values when none are supplied.
What this code does: Creates a products table whose primary key is generated automatically and inserts three products.
DROP TABLE IF EXISTS products;
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
product_name TEXT,
price REAL
);
INSERT INTO products (product_name, price) VALUES
('Wireless Mouse', 899.00),
('Laptop Stand', 1499.00),
('USB Hub', 799.00);
SELECT *
FROM products
ORDER BY product_id;
CREATE TABLE with NOT NULL
NOT NULL prevents a column from ever containing a NULL value.
What this code does: Requires both employee name and department to be present for every row.
DROP TABLE IF EXISTS employees;
CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
employee_name TEXT NOT NULL,
department TEXT NOT NULL,
salary REAL
);
INSERT INTO employees (employee_name, department, salary) VALUES
('Anaya', 'Engineering', 72000),
('Rohan', 'Design', 68000),
('Tara', 'Marketing', 64000);
SELECT *
FROM employees
ORDER BY employee_id;
CREATE TABLE with UNIQUE
UNIQUE guarantees that no two rows share the same value in the constrained column(s).
What this code does: Ensures every username and every email address is unique.
DROP TABLE IF EXISTS users;
CREATE TABLE users (
user_id INTEGER PRIMARY KEY,
username TEXT UNIQUE,
email TEXT UNIQUE
);
INSERT INTO users (username, email) VALUES
('aarav01', 'aarav@example.com'),
('meera22', 'meera@example.com'),
('kabir88', 'kabir@example.com');
SELECT *
FROM users
ORDER BY user_id;
CREATE TABLE with DEFAULT
A DEFAULT value is used automatically when an INSERT statement does not supply a value for that column.
What this code does: New tasks receive the status “Pending” unless another value is provided.
DROP TABLE IF EXISTS tasks;
CREATE TABLE tasks (
task_id INTEGER PRIMARY KEY,
task_name TEXT NOT NULL,
status TEXT DEFAULT 'Pending'
);
INSERT INTO tasks (task_name) VALUES
('Design homepage'),
('Write documentation'),
('Test search feature');
SELECT *
FROM tasks
ORDER BY task_id;
Multiple Constraints Together
Columns can carry several constraints at once.
What this code does: Creates an accounts table that requires a name, a unique non-null email, and a default balance of zero.
DROP TABLE IF EXISTS accounts;
CREATE TABLE accounts (
account_id INTEGER PRIMARY KEY,
account_name TEXT NOT NULL,
account_email TEXT UNIQUE NOT NULL,
balance REAL DEFAULT 0
);
INSERT INTO accounts (account_name, account_email) VALUES
('Nisha', 'nisha@example.com'),
('Dev', 'dev@example.com');
SELECT *
FROM accounts
ORDER BY account_id;
CREATE TABLE IF NOT EXISTS
The IF NOT EXISTS clause prevents an error when the table is already present – useful for scripts that may be re-run.
What this code does: Creates a departments table only if it does not already exist, then inserts three rows.
DROP TABLE IF EXISTS departments;
CREATE TABLE IF NOT EXISTS departments (
department_id INTEGER PRIMARY KEY,
department_name TEXT NOT NULL
);
INSERT INTO departments (department_name) VALUES
('Engineering'),
('Research'),
('Operations');
SELECT *
FROM departments
ORDER BY department_id;
CREATE TABLE with CHECK
A CHECK constraint rejects any value that does not satisfy the given Boolean expression.
What this code does: Ensures that every course duration is greater than zero.
DROP TABLE IF EXISTS courses;
CREATE TABLE courses (
course_id INTEGER PRIMARY KEY,
course_name TEXT NOT NULL,
duration INTEGER CHECK (duration > 0)
);
INSERT INTO courses (course_name, duration) VALUES
('SQL Basics', 20),
('Web Design', 35),
('Data Analysis', 45);
SELECT *
FROM courses
ORDER BY course_id;
CREATE TABLE with FOREIGN KEY
A foreign key links a column in one table to the primary key of another table.
In SQLite you must enable foreign-key enforcement with PRAGMA foreign_keys = ON.
What this code does: Creates customers and orders tables related by a foreign key, inserts sample data and joins them.
PRAGMA foreign_keys = ON;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS customers;
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
customer_name TEXT NOT NULL
);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
amount REAL,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
INSERT INTO customers VALUES
(1, 'Isha'),
(2, 'Karan');
INSERT INTO orders VALUES
(101, 1, 2400.00),
(102, 2, 3150.00);
SELECT
orders.order_id,
customers.customer_name,
orders.amount
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id
ORDER BY orders.order_id;
CREATE TABLE AS SELECT
You can create a new table whose structure and data come from a query result.
What this code does: Creates an engineering_staff table that contains only the Engineering employees.
DROP TABLE IF EXISTS employees;
DROP TABLE IF EXISTS engineering_staff;
CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
employee_name TEXT,
department TEXT,
salary REAL
);
INSERT INTO employees VALUES
(1, 'Aarav', 'Engineering', 78000),
(2, 'Meera', 'Design', 69000),
(3, 'Kabir', 'Engineering', 82000),
(4, 'Riya', 'Marketing', 65000);
CREATE TABLE engineering_staff AS
SELECT
employee_id,
employee_name,
salary
FROM employees
WHERE department = 'Engineering';
SELECT *
FROM engineering_staff
ORDER BY employee_id;
Inspecting Table Structure
SQLite’s PRAGMA table_info() (or the equivalent table-valued function) shows the columns, types and constraints of a table.
What this code does: Creates a vehicles table and then lists its column definitions.
DROP TABLE IF EXISTS vehicles;
CREATE TABLE vehicles (
vehicle_id INTEGER PRIMARY KEY,
brand TEXT NOT NULL,
model TEXT NOT NULL,
year INTEGER
);
SELECT
cid,
name,
type,
"notnull",
dflt_value,
pk
FROM pragma_table_info('vehicles')
ORDER BY cid;
Common Constraints
Common Data Types (SQLite)
Important Points
CREATE TABLEdefines the structure that will hold your data.- Every column needs a name and an appropriate data type.
- Constraints (PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT, CHECK, FOREIGN KEY) protect data integrity.
- SQLite supports
CREATE TABLE IF NOT EXISTSandCREATE TABLE … AS SELECT. - Foreign-key enforcement must be turned on with
PRAGMA foreign_keys = ON.
Best Practices
- Choose clear, meaningful names for tables and columns.
- Always define a primary key when each row needs a unique identifier.
- Apply the tightest set of constraints that still match the business rules.
- Use
IF NOT EXISTSin setup scripts that may be executed more than once. - Design relationships between tables before loading large amounts of data.
The CREATE TABLE statement is the foundation of every relational database. By combining columns, data types and constraints you create reliable, well-organised tables that protect the integrity of your data. Click Test My Code on any example to run it live, or Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.