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.
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.
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.
Key Vocabulary
The CREATE TABLE Syntax
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
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
Example 1: students Table
Example 2: products Table
Example 3: orders with UNIQUE
Anatomy of a Column Definition
Common Mistakes
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?
CREATE TABLE books (
book_id INT PRIMARY KEY,
title VARCHAR(200) NOT NULL,
pages INT,
);
Question 5 of 5
If a column is defined as BOOLEAN DEFAULT TRUE, what happens when you insert a row without specifying that column?