⭐ How to Create a Table in SQL (Beginner-Friendly Guide)

Creating a table is one of the first skills every data analyst, engineer, or backend developer must learn. Tables are the foundation of all databases - every piece of data you’ll ever query lives inside one.

This guide walks you through everything a beginner needs to know about CREATE TABLE in SQL, using simple language, no fluff, and plenty of examples.

Quick summary
  • Command: CREATE TABLE
  • Focus: structure, keys, constraints
  • Perfect for: first-week SQL learners
Jump to practice →

🧱 What Does “Create a Table” Mean?

If you’ve used Excel or Google Sheets before, you already understand the idea behind a table.

  • Rows hold individual records (for example, one customer per row).
  • Columns hold attributes about each record (for example, name, email, country).

When you create a table, you’re telling the database: “Here’s the structure of the data I want to store.”

The basic SQL syntax looks like this:

CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype
);

🏗️ A Simple Example - Creating a customers Table

Here’s a small but realistic beginner table:

CREATE TABLE customers (
    id     SERIAL PRIMARY KEY,
    name   TEXT,
    email  TEXT
);

🔍 What this does

  • Creates a new table named customers.
  • Adds three columns: id, name, and email.
  • Makes id an auto-incrementing number using SERIAL.
  • Marks id as the PRIMARY KEY so each row is uniquely identified.

This is a perfect structure for beginners: small, readable, and close to a real-world example.

🧠 Essential Data Types Beginners Should Know

Different databases have slightly different data types, but these cover 95% of beginner SQL use cases:

Data TypeWhat it StoresExample
SERIALAuto-incrementing ID1, 2, 3…
INTEGERWhole numbers25
NUMERIC(10,2)Decimal numbers (exact)29.95
TEXTFree text of any length"Canada"
VARCHAR(50)Text with a length limit"John"
DATECalendar date2024-01-01
TIMESTAMPDate + time2024-02-01 12:30
BOOLEANTrue/falseTRUE, FALSE
Beginner rule

If you’re not sure which type to use, TEXT is usually safe for words, and INTEGER / NUMERIC / DATE / TIMESTAMP are for numbers and dates.

🔑 Keys Every Beginner Should Know

Keys define how rows are identified and how tables connect to each other in a relational database.

1️⃣ Primary Key

A primary key is a column (or group of columns) that uniquely identifies each row in a table.

id SERIAL PRIMARY KEY

Every table should have one primary key, even if you’re just practising.

2️⃣ Foreign Key

A foreign key is a column that points to the primary key of another table. This is how databases “connect the dots” across tables.

customer_id INTEGER REFERENCES customers(id)

In this example, customer_id must match an existing id in the customers table.

3️⃣ Composite Key (Advanced Beginner)

Sometimes no single column is unique, but a combination of columns is. That combination can be a primary key.

PRIMARY KEY (order_id, product_id)

Composite keys are common in “link” tables that connect many-to-many relationships (for example, orders and products).

⚙️ Constraints Beginners Should Know

Constraints are rules that the database uses to protect your data from bad values.

✔ NOT NULL

Column must not be empty (no missing values allowed).

name TEXT NOT NULL

✔ UNIQUE

No duplicate values allowed in that column.

email TEXT UNIQUE

✔ DEFAULT

If no value is provided, the database uses a default.

created_at TIMESTAMP DEFAULT NOW()

✔ CHECK

The value must satisfy a condition you define.

price INTEGER CHECK (price > 0)

🏛️ Good Table Design Practice (Beginner Level)

Here are foundational habits that make your tables look professional:

1️⃣ Use clear, lowercase table names

  • ✔ customers
  • ✔ orders
  • ✔ product_sales
  • ❌ Tbl1
  • ❌ CustomerTable2024

2️⃣ Use snake_case for columns

  • ✔ first_name
  • ✔ email_address
  • ❌ FirstName
  • ❌ Email Address

3️⃣ Every table needs a primary key

This is how you safely update or delete one specific row later without affecting others.

4️⃣ Use appropriate data types

Don’t store everything as TEXT if you can avoid it. Use numbers for quantities, dates for dates, and booleans for true/false.

5️⃣ Add foreign keys when tables relate

If orders belong to customers, or products belong to categories, reflect that relationship with foreign keys. This keeps your data consistent.

🧩 Complete Beginner Template

You can use this template whenever you create a brand-new table:

CREATE TABLE table_name (
    id        SERIAL PRIMARY KEY,
    column1   TEXT,
    column2   INTEGER,
    column3   DATE
);

🧪 Your Practice Task

Try this to turn the theory into practice. Imagine you’re designing a simple store database.

1️⃣ Create a products table

  • id – SERIAL PRIMARY KEY
  • product_name – TEXT
  • price – INTEGER
  • created_at – TIMESTAMP with a default value of NOW()
CREATE TABLE products (
    id           SERIAL PRIMARY KEY,
    product_name TEXT,
    price        INTEGER,
    created_at   TIMESTAMP DEFAULT NOW()
);

2️⃣ Create an orders table

Each order will reference a product:

  • id – SERIAL PRIMARY KEY
  • product_id – FOREIGN KEY referencing products(id)
  • quantity – INTEGER
  • order_date – DATE
CREATE TABLE orders (
    id          SERIAL PRIMARY KEY,
    product_id  INTEGER REFERENCES products(id),
    quantity    INTEGER,
    order_date  DATE
);

This small pair of tables already mirrors real-world database design used in shops, e-commerce, and inventory systems.

🎉 Final Takeaway

Creating tables is the foundation of SQL. Once you understand how to define:

  • table structure (columns and types)
  • data types (TEXT, INTEGER, DATE, etc.)
  • keys (PRIMARY and FOREIGN)
  • constraints (NOT NULL, UNIQUE, CHECK, DEFAULT)
  • relationships between tables

…you can start building real-world databases with confidence. From here, the next steps are inserting data, querying it with SELECT, and gradually joining tables together.

Try it yourself

CREATE TABLE is Level 43, in Chapter 7: Creating and changing tables.

The first 5 levels are free · no sign-up needed