Stop 10 · Schema Design & ConstraintsTopics · 1/11

CREATE TABLE

Part of the SQL Roadmap

In short

Before a single row of data exists, someone decides what a table holds and what it refuses. CREATE TABLE is that decision, and a good one saves months of cleaning bad data later.

CREATE TABLE defines a new table with its columns, data types and constraints.

The jaadu leans over a big drafting table, drawing neat empty boxes of different sizes on a large blank sheet of paper with a ruler and pencil.

Think of it like this

Designing the admission form

Before admissions open, a college in Nagpur prints its form: a box for name (required), date of birth in DD-MM-YYYY, a six-digit pin code, a fee amount that can't be negative. Every form that comes back follows those rules. CREATE TABLE is the blank form, and constraints are the 'required' stars.

Watch it happen

Anatomy of a CREATE TABLE

students
student_id INTEGER
PRIMARY KEY, IDENTITY
full_name VARCHAR(100)
NOT NULL
email VARCHAR(150)
UNIQUE
fees_paid NUMERIC(10,2)
DEFAULT 0, CHECK >= 0

1/5A table has a name and a list of columns.

The big ideas

What you need to know

Name and type

Every column needs a name and a data type. Use lower_snake_case names like date_of_birth.

Always a primary key

Give every table a primary key, usually a generated ID, so each row can be found and referenced.

Constraints guard the door

NOT NULL, UNIQUE, CHECK and DEFAULT apply to every insert, whichever app or person sends the data.

Foreign keys come next

Columns like department_id can point to another table with REFERENCES. You'll do this in later topics.

See it in code

Walk through the code

sql
1CREATE TABLE students (2  student_id    INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,3  full_name     VARCHAR(100)  NOT NULL,4  email         VARCHAR(150)  UNIQUE,5  city          VARCHAR(60)   DEFAULT 'Nagpur',6  pin_code      CHAR(6),7  date_of_birth DATE,8  fees_paid     NUMERIC(10,2) NOT NULL DEFAULT 0 CHECK (fees_paid >= 0),9  created_at    TIMESTAMPTZ   NOT NULL DEFAULT NOW()10);

1/5The table name, then columns inside brackets, separated by commas.

A students table with types and constraints, in PostgreSQL. In MySQL use INT AUTO_INCREMENT PRIMARY KEY and DATETIME. In SQL Server use INT IDENTITY(1,1) PRIMARY KEY and DATETIME2 with DEFAULT SYSDATETIME().

Save yourself the pain

Common mistakes

Oops

Making every column VARCHAR(255) because it 'just works'.

Do this instead

Pick real types: DATE for dates, NUMERIC for money, INTEGER for counts. Wrong types let bad data in and sort wrongly.

Oops

Creating a table without a primary key.

Do this instead

Without one, duplicate rows creep in and you can't safely update or reference a single row.

Your mission

Your first real table

~20 min

Quick check

Which constraint stops two students from registering with the same email?

Go deeper

  1. 1Create a students table with sensible types for each column.
  2. 2Add a primary key and at least one NOT NULL column.
  3. 3Save your CREATE scripts so you can rebuild the database any time.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge