CREATE TABLE Statement

SQL · Working with Tables

Theory

CREATE TABLE defines a new, empty table: its name, each column's name and data type, and the rules (constraints) the data must follow. Common types are INT for whole numbers, DECIMAL(10,2) for money with exact decimals, VARCHAR(n) for text up to n characters, DATE and TIMESTAMP for time values, and BOOLEAN. Constraints such as PRIMARY KEY, NOT NULL, UNIQUE, CHECK, DEFAULT and FOREIGN KEY can be written next to a column or as separate lines at the end.

The table holds no rows until you INSERT them. Running CREATE TABLE for a name that already exists fails, so many engines support CREATE TABLE IF NOT EXISTS. You can also create a table from a query with CREATE TABLE new_t AS SELECT ..., which copies the data and column types but usually not the constraints. Type names and limits differ by engine, so check your database's documentation.

Why we use it

A database needs to know the shape of your data before it can store it. CREATE TABLE fixes the columns, types and rules up front, so bad data is rejected and every query can rely on a known structure.

When to use it

Use CREATE TABLE when designing a new entity (customers, orders) or a staging table for loads. Use CREATE TABLE AS SELECT for quick copies, and ALTER TABLE to change a table that already exists.

How it works

  1. Choose a table name
  2. List each column with a data type
  3. Add column rules: NOT NULL, DEFAULT, UNIQUE, CHECK
  4. Add table rules: PRIMARY KEY, FOREIGN KEY
  5. Name already exists? Yes: Error (or skip with IF NOT EXISTS). No: Create the empty table.
  6. Table is ready for INSERT

Syntax

CREATE TABLE table_name (
  column1 datatype,
  column2 datatype
);

Example

CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2)
);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic