Learn SQL
-- tables.create_table

CREATE TABLE in SQL

Define a table: its columns, types and rules

--concept

CREATE TABLE defines a new table: its name, each column with a data type, and optional rules (constraints) like NOT NULL or PRIMARY KEY. Types keep data honest — numbers stay numbers, dates stay dates — and the primary key gives every row a unique identity the database enforces forever.

--how it works
1

Name the table

Convention: lowercase plural — employees, orders, departments. The name becomes the routing word in every future query.

2

Declare typed columns

INTEGER for counts and ids, DECIMAL(10,2) for money, VARCHAR(n) for short text, TEXT for long text, DATE for calendar values.

3

Attach constraints

PRIMARY KEY = unique + required; NOT NULL forbids blanks; UNIQUE forbids duplicates; DEFAULT supplies a value when none is given; CHECK validates a rule.

4

Run it once

The table springs into existence empty — DDL statements return no rows, just the new structure.

5

Evolve later, deliberately

ALTER TABLE adds or retypes columns; DROP TABLE removes the whole structure. Both are heavyweight, intentional operations.

--visualization
idINTEGERnameVARCHAR(50)NOT NULLdepartmentVARCHAR(30)salaryDECIMAL(10,2)ageINTEGERCREATE TABLE employees ( … );
Stage 1 / 7
Empty schema. CREATE TABLE will define each column with a type + rule.
--example
query.sql
1CREATE TABLE employees (
2 id INTEGER PRIMARY KEY,
3 name VARCHAR(50) NOT NULL,
4 department VARCHAR(30),
5 salary DECIMAL(10,2),
6 age INTEGER
7);

Five typed columns; id is the primary key so two rows can never share an employee number, and name can never be blank. Watch the visualization stamp out the empty grid column by column.

--variants

Minimal table

Two columns, no constraints — legal, but nothing is protected yet.

query.sql
1CREATE TABLE notes (
2 body TEXT,
3 created DATE
4);
1Both columns accept NULL and duplicates.
2Nothing stops two identical notes from existing.
3Fine for scratch tables; risky for anything that matters.

With a primary key

id becomes a unique identity the engine enforces forever.

query.sql
1CREATE TABLE employees (
2 id INTEGER PRIMARY KEY,
3 name VARCHAR(50) NOT NULL
4);
1INSERTing a second id = 1 fails immediately with a constraint error.
2NOT NULL on name means the engine rejects blank names too.
3Every foreign reference elsewhere can now point at id safely.

Defaults & checks

Columns can fill themselves in and validate their own values.

query.sql
1CREATE TABLE orders (
2 id INTEGER PRIMARY KEY,
3 qty INTEGER NOT NULL CHECK (qty > 0),
4 status VARCHAR(10) DEFAULT 'new'
5);
1CHECK (qty > 0) rejects zero and negative quantities at insert time.
2Omitting status in an INSERT stores 'new' automatically.
3The database is now the last line of defence, not the app code alone.

Changing structure later

ALTER adds or modifies columns; DROP removes the table entirely.

query.sql
1ALTER TABLE employees ADD COLUMN bonus DECIMAL(10,2);
2DROP TABLE notes;
1ALTER TABLE keeps all existing rows; the new column is NULL (or its default) everywhere.
2DROP TABLE deletes structure and data together — there is no undo without a backup.
3Both are DDL: instant, logged, and usually restricted to admins in production.
--key facts
Common typesINTEGER · DECIMAL(10,2) · VARCHAR(n) · TEXT · DATE · TIMESTAMP · BOOLEAN
ConstraintsPRIMARY KEY · NOT NULL · UNIQUE · DEFAULT · CHECK · FOREIGN KEY
Naminglowercase snake_case is the most portable convention
--use cases
Designing any new feature's storage — the schema is the spec
Importing a CSV: create the typed table first, then INSERT the rows
Enforcing business rules (qty > 0, unique emails) at the deepest layer
Interview schema-design questions, which almost always start "write the CREATE TABLE"
--under the hood
Types are documentation the engine enforces — DECIMAL(10,2) exists because FLOAT rounds money badly.
Pick VARCHAR(n) for bounded values and TEXT for unbounded ones; the n hint helps storage and validation.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.