Objective. Students will create a multi-table database schema using correct DDL statements including CREATE TABLE, data type selection, and column constraints.
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
Data Definition Language (DDL) is the subset of SQL that creates, modifies, and removes the structure of a database rather than its data. The three core DDL statements are CREATE, ALTER, and DROP. In PostgreSQL 16, every object you build—tables, indexes, sequences, schemas—begins with DDL. Understanding DDL before writing a single INSERT statement prevents costly redesigns later.
A schema is the blueprint of your database: which tables exist, what columns each table holds, what data types those columns use, and how tables relate to one another. Before opening psql or pgAdmin, sketch the entities (nouns in your domain), their attributes (columns), and their relationships (foreign keys). For a bookstore you might identify three entities: authors, books, and orders. Mapping these on paper or in a simple diagram catches problems before bad DDL reaches a production server.
PostgreSQL offers a rich type system. Picking the wrong type wastes storage, breaks calculations, or silently corrupts data. Use these as starting defaults:
Avoid using TEXT for everything. It eliminates the type system's ability to catch errors at insertion time.
A `CREATE TABLE` statement names the table, lists columns with their types, and declares constraints inline or as table-level clauses.
```sql
CREATE TABLE authors (
author_id SERIAL PRIMARY KEY,
full_name VARCHAR(120) NOT NULL,
birth_date DATE,
UNIQUE (full_name)
);
```
Key points:
| Constraint | Purpose |
|---|---|
| NOT NULL | Prevents NULL from being stored in the column |
| UNIQUE | Guarantees no two rows share the same value |
| DEFAULT | Supplies a value when none is provided on INSERT |
| CHECK | Rejects rows where a Boolean expression is false |
| PRIMARY KEY | Uniquely identifies each row; combines UNIQUE + NOT NULL |
| FOREIGN KEY | Enforces referential integrity between tables |
```sql
CREATE TABLE books (
book_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title VARCHAR(255) NOT NULL,
price NUMERIC(10,2) NOT NULL CHECK (price >= 0),
published DATE NOT NULL,
author_id INTEGER NOT NULL
REFERENCES authors(author_id)
ON DELETE RESTRICT
);
```
`ON DELETE RESTRICT` prevents deleting an author who still has books, protecting referential integrity.
`ALTER TABLE` changes an existing table's structure without dropping it:
```sql
ALTER TABLE books ADD COLUMN in_stock BOOLEAN NOT NULL DEFAULT TRUE;
ALTER TABLE books ALTER COLUMN title TYPE TEXT;
ALTER TABLE books DROP COLUMN in_stock;
```
`DROP TABLE` permanently removes a table and all its data:
```sql
DROP TABLE books; -- fails if another table references it
DROP TABLE books CASCADE; -- also drops dependent objects
```
Use `CASCADE` with caution. In a shared or production environment, dropping a table is irreversible unless a backup exists. Always verify you are connected to the correct database before executing DROP.
In psql, `\d tablename` displays columns, types, and constraints. You can also query the information schema, which is portable across SQL databases:
```sql
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'books';
```
Compare this output against your specification before submitting any DDL script. Automated grading in this course checks `\d` output directly.
DDL statements execute immediately and cannot be rolled back by default in many workflows. Follow these practices:
1. Work in a development or test database until the schema is verified. Never build new schemas directly against production data.
2. Use transactions where supported: wrap DDL in `BEGIN; … COMMIT;` so you can `ROLLBACK` if something goes wrong within the same session.
3. Keep DDL scripts in version control (Git). Treat schema changes like code changes—review before applying.
4. Back up before DROP or destructive ALTER. In a professional environment, confirm a current backup exists before any destructive DDL.
These habits form the professional baseline expected in every workplace that handles database infrastructure.
Answer each question, then check it. Nothing is recorded; this is your own practice.
A free tech program — complimentary, as our commitment to get America working. Help us build VoTechWorks. Tell us what works, what doesn’t, and what you want to learn.