01 / 20

CREATE TABLE
in SQL

Learn how to define and build database tables from scratch using the SQL CREATE TABLE statement: columns, data types, and constraints.

SQL

What Is a Database Table?

A table is the fundamental storage structure in a relational database. Think of it like a spreadsheet: it organizes data into rows (records) and columns (fields), where each column has a name and a data type.

id name grade 1 Alice 10 2 Bob 11 3 Carol 10 Columns (fields) → Rows →

Why CREATE TABLE Matters

Every application that stores data: a social media app, an online store, a school gradebook: relies on well-designed tables. The CREATE TABLE statement is your blueprint.

Structure
Defines what data you can store and how it is organized into columns.
Integrity
Constraints enforce rules so invalid or inconsistent data cannot enter the table.
Performance
Choosing the right data types helps the database store and retrieve data efficiently.

Key Vocabulary

1
Column (field): A named attribute of the table, e.g. name or email.
2
Data type: The kind of value a column can hold: INT, VARCHAR, DATE, etc.
3
Constraint: A rule enforced on a column, such as NOT NULL or PRIMARY KEY.
4
Row (record): A single entry in the table: one student, one order, one product.

The CREATE TABLE Syntax

CREATE TABLE table_name ( column1 data_type constraints, column2 data_type constraints, column3 data_type constraints );

Common Data Types

Type Stores Example
INT Whole numbers 42, -7
VARCHAR(n) Text up to n characters 'Alice'
TEXT Long, variable-length text 'A long essay...'
DATE Calendar date '2025-01-15'
BOOLEAN TRUE or FALSE TRUE
DECIMAL(p,s) Precise decimal numbers 99.95

Key Constraints

PRIMARY KEY
Uniquely identifies each row. No duplicates, no NULLs allowed.
NOT NULL
The column must always have a value: empty entries are rejected.
UNIQUE
Every value in this column must be different across all rows.
DEFAULT
Provides a fallback value if none is supplied during INSERT.

CREATE vs ALTER TABLE

Aspect CREATE TABLE ALTER TABLE
Purpose Build a brand-new table Modify an existing table
When used Before any data exists After table already has data
Can add columns? Yes (initial set) Yes (additional ones)
Destroys data? No (nothing exists yet) Can if you drop columns

How to Write a CREATE TABLE

1
Choose a table name. Use a clear, lowercase, plural noun: students, orders, products.
2
List your columns. Decide what attributes each record needs: name, age, email, etc.
3
Assign data types. Pick the most appropriate type for each column: INT for numbers, VARCHAR for text.
4
Add constraints. Mark the primary key, decide which columns are NOT NULL, add DEFAULT values.
5
End with a semicolon. Every SQL statement must end with ; to tell the database you are done.

Example 1: students Table

CREATE TABLE students ( id INT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, grade INT NOT NULL );
students id (PK) first_name last_name grade 1 Alice Smith 10 2 Bob Jones 11 3 Carol Lee 10

Example 2: products Table

CREATE TABLE products ( product_id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, price DECIMAL(8,2) NOT NULL, in_stock BOOLEAN DEFAULT TRUE, category VARCHAR(50) );

Example 3: orders with UNIQUE

CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_name VARCHAR(100) NOT NULL, email VARCHAR(255) UNIQUE NOT NULL, order_date DATE NOT NULL, total DECIMAL(10,2) NOT NULL );

Anatomy of a Column Definition

email VARCHAR(255) UNIQUE NOT NULL Column Name Data Type What kind of value? Constraints What rules apply?

Common Mistakes

✕ Missing Semicolon
Forgetting the ; at the end. The database will wait for more input and throw a confusing error.
✕ Trailing Comma
Putting a comma after the last column definition. SQL does not allow a trailing comma before the closing parenthesis.
✕ Wrong Data Type
Using INT for phone numbers or ZIP codes. Leading zeros get lost: use VARCHAR instead.
✕ No Primary Key
Omitting a PRIMARY KEY. Without it, you have no reliable way to identify individual rows.

Quick Reference Summary

Element Purpose Example
CREATE TABLE Defines a new table CREATE TABLE students (...);
INT Stores whole numbers age INT
VARCHAR(n) Stores text up to n chars name VARCHAR(50)
PRIMARY KEY Unique row identifier id INT PRIMARY KEY
NOT NULL Disallows empty values email VARCHAR(255) NOT NULL
DEFAULT Sets a fallback value active BOOLEAN DEFAULT TRUE

Question 1 of 5

Which SQL statement is used to define a brand-new table in a database?

Question 2 of 5

What data type is most appropriate for storing a product price like 49.99?

Question 3 of 5

What does the PRIMARY KEY constraint guarantee about a column?

Question 4 of 5

Which line contains a syntax error?

Question 5 of 5

If a column is defined as BOOLEAN DEFAULT TRUE, what happens when you insert a row without specifying that column?