Objective. Students create a multi-schema database with tables, sequences, views, and constraints using both psql DDL commands and the pgAdmin GUI.
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.
A PostgreSQL server can host many databases. Each database is an isolated container: objects inside one database cannot directly reference objects in another. When you connect with psql or pgAdmin, you connect to exactly one database at a time. For this lab you will create `inventory_db` and work entirely within it.
Inside a database, schemas are namespaces that organize objects. Every database starts with the `public` schema. You can add schemas to separate concerns — for example, `warehouse` for stock-related tables and `sales` for order-related tables. Object names are qualified with the schema name: `warehouse.product` refers to the `product` table in the `warehouse` schema. The search path (`search_path`) controls which schemas PostgreSQL checks when an unqualified name is used.
```sql
CREATE SCHEMA warehouse;
CREATE SCHEMA sales;
```
Tables store data in rows and columns. When you define a table you also declare constraints that enforce data integrity:
```sql
CREATE TABLE warehouse.product (
product_id INT PRIMARY KEY,
sku VARCHAR(50) NOT NULL UNIQUE,
unit_price NUMERIC(10,2) NOT NULL CHECK (unit_price >= 0)
);
```
Foreign keys that span schemas work the same way — just use the fully qualified table name in the REFERENCES clause.
A sequence is a database object that generates a series of integers. You can create one explicitly:
```sql
CREATE SEQUENCE warehouse.product_id_seq START 1 INCREMENT 1;
```
Associating it with a column using `OWNED BY` means the sequence is dropped automatically when the column or table is dropped — keeping the database tidy.
PostgreSQL offers three shorthand approaches for auto-incrementing integer columns:
| Approach | Introduced | Notes |
|---|---|---|
| `SERIAL` / `BIGSERIAL` | Early PostgreSQL | Creates a sequence implicitly; NOT SQL-standard |
| `GENERATED ALWAYS AS IDENTITY` | PostgreSQL 10 | SQL-standard; preferred for new designs |
`BIGSERIAL` uses an 8-byte integer and is appropriate when a table may exceed roughly two billion rows. `GENERATED ALWAYS AS IDENTITY` is the current best practice because it is part of the SQL standard and gives the database tighter ownership of the sequence.
A view is a saved SELECT statement stored as a named object. It does not duplicate data; each time you query the view, PostgreSQL runs the underlying SELECT. Views simplify complex queries, control what columns a user can see, and can join tables across schemas.
```sql
CREATE VIEW sales.inventory_summary AS
SELECT s.order_id, w.sku, s.quantity
FROM sales.order_line s
JOIN warehouse.product w ON s.product_id = w.product_id;
```
PostgreSQL 12 introduced generated columns — columns whose value is always computed from other columns in the same row. The keyword `GENERATED ALWAYS AS (expression) STORED` writes the computed value to disk.
```sql
ALTER TABLE warehouse.product
ADD COLUMN price_with_tax NUMERIC(10,2)
GENERATED ALWAYS AS (unit_price * 1.08) STORED;
```
You cannot insert or update a generated column directly; PostgreSQL manages it.
The `pg_dump --schema-only` flag extracts the Data Definition Language (DDL) for all objects without exporting any row data. This is useful for version control, peer review, and migration.
```bash
pg_dump --schema-only -d inventory_db -f inventory_db_schema.sql
```
Review the output file to confirm every table, constraint, sequence, and view appears as expected.
Although this is a software environment, safe data-management habits begin here:
pgAdmin 4 displays the object tree in the left panel. Expand Databases → inventory_db → Schemas to browse tables, views, sequences, and constraints visually. Right-click any object for scripted DDL. Use Tools → Query Tool to run SQL and see results side by side. Annotate your screenshots with the built-in snipping tool or any image editor before pasting them into the lab report template.
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.