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
Stage 1 / 7
Empty schema. CREATE TABLE will define each column with a type + rule.
--example
query.sql
1CREATETABLEemployees(
2idINTEGERPRIMARYKEY,
3nameVARCHAR(50)NOTNULL,
4departmentVARCHAR(30),
5salaryDECIMAL(10,2),
6ageINTEGER
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
1CREATETABLEnotes(
2bodyTEXT,
3createdDATE
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
1CREATETABLEemployees(
2idINTEGERPRIMARYKEY,
3nameVARCHAR(50)NOTNULL
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
1CREATETABLEorders(
2idINTEGERPRIMARYKEY,
3qtyINTEGERNOTNULLCHECK(qty>0),
4statusVARCHAR(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
1ALTERTABLEemployeesADDCOLUMNbonusDECIMAL(10,2);
2DROPTABLEnotes;
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
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 →