Module lessons (3/4)
Creating Tables and Constraints: DDL
CREATE TABLE with PRIMARY KEY, NOT NULL, UNIQUE, FOREIGN KEY, CHECK, and DEFAULT.
What you'll learn in this lesson
- Define new tables via
CREATE TABLEchoosing the correct types (SERIAL,INTEGER,NUMERIC,TEXT,BOOLEAN,DATE,TIMESTAMPTZ). - Apply essential integrity constraints:
PRIMARY KEY,NOT NULL,UNIQUE,REFERENCES(FOREIGN KEY),CHECK, andDEFAULT. - Evolve or remove existing tables with
ALTER TABLEandDROP TABLE IF EXISTS.
Overview
DDL (Data Definition Language) collects commands that shape the very structure of the database: create tables, impose rules that prevent dirty data from entering and evolve the schema over time with ALTER TABLE.
When you declare REFERENCES customers(id), you enforce the same PK ↔ FK connection at the database engine level that you then query via JOIN: each child row must point to a real parent, otherwise the database rejects the write!
Sections covered in this lesson
- 11. Mental Model: Fundamental Types and Integrity Constraints
- 22. Progressive & Annotated Examples
- 33. Interactive Laboratory: Referential Integrity between Tables
- 44. Diagnostic Verification (Quiz)
- 55. Progressive Practical Laboratory
Unlock "Creating Tables and Constraints: DDL" and the full SQL Course curriculum
CREATE TABLE with PRIMARY KEY, NOT NULL, UNIQUE, FOREIGN KEY, CHECK, and DEFAULT.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in SQL Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.