Tables, Keys, and Missing Values
Read a relational table and distinguish a key from a missing value.
database, sql, postgresql, table, primary key, nullA table stores facts in rows
A database organises data so it can be stored, retrieved, and changed. A relational database represents data in tables. Each row describes a record; each column describes an attribute shared by those records. PostgreSQL is one relational database system, and SQL is a language for working with its data.
Imagine a small fictional library: books has an integer id, a text
title, and an integer pages. The primary key id uniquely identifies
each row and cannot be NULL. Titles are not necessarily unique. The
pages column allows NULL, meaning the value is missing or unknown here;
it does not mean zero. The choice of types and constraints is part of the
table's design.
SQL illustration
The following PostgreSQL example creates temporary, fictional data. A temporary table belongs to the current session and is removed when that session ends. Intuvo shows prepared examples and does not execute SQL. Use a fresh session with no transaction already open.
BEGIN;
CREATE TEMP TABLE books (
id integer PRIMARY KEY,
title text NOT NULL,
pages integer
);
INSERT INTO books (id, title, pages) VALUES
(1, 'Kite', 120),
(2, 'Oak', NULL),
(3, 'Tide', 200),
(4, 'Fern', 120);
COMMIT;
SELECT id, title, pages FROM books ORDER BY id;
Prepared result:
| id | title | pages |
|---|---|---|
| 1 | Kite | 120 |
| 2 | Oak | NULL |
| 3 | Tide | 200 |
| 4 | Fern | 120 |
id = 2 identifies Oak's row; pages identifies the column containing its
unknown page count. ORDER BY id makes the displayed row order explicit:
without an ORDER BY, SQL does not promise a result order.
See the reasoning
Explore a table
Identify the parts of the fictional books table. This is prepared; Intuvo does not run SQL.
Find the table
books
| id | title | pages |
|---|---|---|
| 1 | Kite | 120 |
| 2 | Oak | NULL |
| 3 | Tide | 200 |
| 4 | Fern | 120 |
The books table groups four fictional records under the same column names.
Check for missing values
NULL is not an ordinary value. pages = NULL does not test whether a page
count is missing: the comparison is unknown rather than true. Use
pages IS NULL or pages IS NOT NULL. A WHERE condition retains rows
where the condition is true, not rows where it is false or unknown.
Constraints can protect basic rules, but a unique ID does not prove that the
rest of a record is correct.
Reflect on the concept
Four fixed questions to reinforce the reasoning. Answers are not saved or scored.
