Flash cards
What are a table, a row and a column in a relational database?
Show the answer
A table holds one kind of thing, such as walks. A row is one of those things, one walk. A column is one kind of fact every row has, with a name and one data type, such as price_zar.
What is a primary key, and what is a foreign key?
Show the answer
A primary key identifies each row: unique and never NULL, in the Walk Office always id. A foreign key is a column holding another table's primary key, such as a departure's walk_id, and the database guarantees that row exists.
What do -1 and -v ON_ERROR_STOP=1 do when psql runs a file with -f?
Show the answer
ON_ERROR_STOP stops at the first error and makes psql exit with status 3. -1 runs all the files as one transaction, so if anything fails, nothing is changed.
What does psql's autocommit mode mean?
Show the answer
Each statement is its own transaction and is saved the moment it succeeds, with no undo. Start a transaction with BEGIN to group statements and keep the choice of COMMIT or ROLLBACK.
What does an exclamation mark in the psql prompt, just before the final arrow, tell you?
Show the answer
You are in a transaction that has failed. PostgreSQL ignores every statement until you type ROLLBACK.
Why must money never be stored as real or double precision?
Show the answer
Floating point cannot hold most decimal fractions exactly: 0.1 plus 0.2 gives 0.30000000000000004. Use whole rand in an integer, or numeric, which is exact.
What does timestamptz store, and how is it shown?
Show the answer
An exact moment in time, kept in UTC internally, and shown in the session's time zone. In Africa/Johannesburg it shows with +02.
Why use GENERATED ALWAYS AS IDENTITY rather than serial for ids?
Show the answer
Identity columns are standard SQL and own their sequence, which keeps permissions and design changes simple. GENERATED ALWAYS also refuses hand-typed ids, so they can never collide with generated ones.
Name the five constraints you can put on a column in CREATE TABLE, apart from a foreign key.
Show the answer
PRIMARY KEY, NOT NULL, UNIQUE, CHECK (condition) and DEFAULT value (which supplies a value rather than enforcing a rule).
How does PostgreSQL name a check constraint on walks.price_zar that you did not name?
Show the answer
walks_price_zar_check: table, column, kind. Similarly walks_pkey for the primary key and walks_slug_key for a unique slug.
What is the difference between ON DELETE RESTRICT and ON DELETE CASCADE?
Show the answer
RESTRICT refuses to delete a row that other rows still refer to. CASCADE deletes the referring rows too, silently. Business records such as bookings use RESTRICT.
What does RETURNING do on an INSERT, UPDATE or DELETE?
Show the answer
Makes the statement answer like a query, with the rows it changed, such as the ids PostgreSQL handed out to new rows.
In what order are a query's clauses written?
Show the answer
SELECT, FROM, WHERE, ORDER BY, LIMIT, OFFSET. Only SELECT is required.
What is the difference between LIKE and ILIKE, and what do % and _ mean?
Show the answer
LIKE is case-sensitive, ILIKE ignores capitals. In a pattern, % stands for any run of characters, including none, and _ for exactly one.
Why does WHERE phone = NULL never find anything?
Show the answer
Any comparison with NULL is unknown, and WHERE keeps only rows where the condition is true. Use IS NULL or IS NOT NULL.
What does COALESCE(phone, '-') return?
Show the answer
The phone number when there is one, and a hyphen when phone is NULL: the first of its arguments that is not NULL.
What happens when you run UPDATE walks SET price_zar = 500 with no WHERE clause?
Show the answer
Every walk gets the price R500, and the command tag says UPDATE 6. In autocommit mode it is saved at once, so make changes inside BEGIN and read the count first.
What are the parts of a PostgreSQL error message?
Show the answer
ERROR says what went wrong, DETAIL gives the values, HINT suggests a fix, and LINE with a caret shows where. \errverbose adds the SQLSTATE code, such as 23505 for a unique violation.