Review Study Aid
The points to fix in memory before the self assessment.
Definitions you must be able to give
- A table holds one kind of thing, a row is one of those things, and a column is one kind of fact with one data type.
- A primary key identifies every row and is unique and never NULL.
- A foreign key holds another table's primary key, and the database guarantees the row it names exists.
- A constraint is a rule the database enforces on every change, from every program.
- A transaction is a group of statements that succeed or fail as one.
- NULL means a missing or unknown value, not zero or empty text.
Commands and syntax to know
psql -h localhost -U walks_app -d mandlas_walksconnects to the Walk Office database.\d walksdescribes a table,\dtlists tables,\xtoggles expanded display and\i file.sqlruns a file.psql -1 -v ON_ERROR_STOP=1 -f sql/schema.sql -f sql/seed.sqlruns files as one transaction and stops at the first error.id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEYis the id column of every Walk Office table.INSERT INTO walks (slug, name, ...) VALUES (...), (...) RETURNING id;adds rows and reports their new ids.- A query's clauses come in the order SELECT, FROM, WHERE, ORDER BY, LIMIT, OFFSET.
COALESCE(phone, '-')shows a hyphen where the phone is NULL.
Rules and limits
- Money is stored as whole rand in an integer or as
numeric, never asrealordouble precision. numeric(4,1)holds at most 999.9, and integer division drops the fraction, so7 / 2is 3.BETWEENincludes both ends, andLIKEis case-sensitive whileILIKEis not.- Without
ORDER BY, rows come back in no guaranteed order. - A CHECK passes when its condition is unknown, so pair checks with NOT NULL.
- Inside a failed transaction, PostgreSQL accepts only ROLLBACK.
- Africa/Johannesburg is two hours ahead of UTC all year, shown as
+02.
Distinctions not to confuse
- Single quotes hold text values, and double quotes hold names.
= NULLis never true, whileIS NULLfinds missing values.timestamptzstores an exact moment, whiletimestampstores a clock time with no zone.- ON DELETE RESTRICT refuses the delete, while ON DELETE CASCADE deletes the referring rows too.
||gives NULL if any part is NULL, whileconcat()treats NULL as empty text.- COMMIT saves a transaction's changes for everyone, while ROLLBACK discards them.