4.4.11 · D1Databases

Foundations — Views — creating, updatable views

3,415 words16 min readBack to topic

Before you can reason about views, you need to be fluent in the pieces the parent note assumes you already know. This page builds every one of them from zero. If a word below already feels obvious, good — read it anyway, because the picture attached to it is what makes updatable views click later.


1. A table = a grid of rows and columns

Picture the grid literally. The whole topic is about the difference between a grid that holds data and a name that only points at data.

Figure — Views — creating, updatable views

What the figure shows: the grid employees with a header row (blue) naming three columns — emp_id, name, salary. The yellow arrow points along a column (all salaries stacked vertically); the green arrow points along a row (one person's full record read across). Fix this one picture in your mind: columns run down, rows run across.

Related deeper reading once you know the basics: Normalization explains why data is split across several such grids in the first place.


2. Keys = the column that names each row uniquely

Before SELECT and JOIN, you need the idea of a key — it decides everything about updatability later.

Figure — Views — creating, updatable views

What the figure shows: two grids. In employees, the emp_id cells are outlined yellow — each value appears once (unique = primary key). The dept_id column has arrows crossing to the departments grid, landing on its yellow dept_id primary key. Notice two employees point at the same department — that is the "many-to-one" shape that will haunt joins.


3. SELECT = "give me a slice of the grid"

SELECT name, salary       -- which columns
FROM   employees          -- from which table
WHERE  salary > 50000;    -- keep only these rows

Read it as three questions in order:

  • WHAT columns?name, salary (chops the grid vertically). This chosen column list is called the projection.
  • FROM where?employees (which grid).
  • WHICH rows?salary > 50000 (chops the grid horizontally).
Figure — Views — creating, updatable views

What the figure shows: the same employees grid. The blue box highlights the two chosen columns (name, salary) — that is the vertical cut. The red box highlights the surviving rows where salary > 50000 — that is the horizontal cut. What's left after both cuts is the view's result. Notice the projection here dropped emp_id — remember from §2 that dropping the primary key can cost you updatability.

The > symbol here is just comparison — "greater than". Below is the full toolkit of comparison symbols you'll meet.

More on how SELECT combines grids lives in SQL SELECT and JOINs.


4. WHERE = the row filter (and why it only filters reads)

That last sentence is the seed of one of the parent's big "mistakes". Hold onto it.

Figure — Views — creating, updatable views

What the figure shows: on the left, the full base table holds four prices, including a red $150. The dashed yellow box on the right is the view built with WHERE price < 100 — only the three cheap green rows pass through the "read" arrow. The $150 row is still in the base table; the view simply never displays it. That single picture is the whole "WHERE filters reading, not existence" lesson.


5. JOIN = stitching two grids by a shared column

SELECT e.name, d.dept_name        -- columns from BOTH tables
FROM   employees e                -- alias e
JOIN   departments d              -- alias d
       ON e.dept_id = d.dept_id;  -- match rule
Figure — Views — creating, updatable views

What the figure shows: the two grids on the left, joined by arrows on dept_id, produce the wide grid on the right. Look carefully: department "Sales" (dept_id = 1) appears in two output rows because two employees belong to it. The green highlight marks that duplicated department cell — one base department row, printed twice in the view.


6. Aggregate functions = "squash many rows into one number"

Figure — Views — creating, updatable views

What the figure shows: four separate salary boxes on the left flow through the blue "AVG (squash)" arrow into a single yellow box 50000 on the right. The dashed red arrow going backwards is crossed by the question "which numbers?" — because countless different four-number sets average to 50000, the reverse arrow has no unique answer. That is irreversibility, drawn.


7. DISTINCT and set operations = "remove or merge rows"


8. The vocabulary of "editing": INSERT, UPDATE, DELETE


How these feed the topic

Base table = grid of rows and cols

Key = unique row name tag

SELECT slices the grid

WHERE filters rows on read

NULL = unknown, filtered out

JOIN stitches two grids on a key

Outer JOIN pads with NULL

Aggregate squashes many rows to one

GROUP BY and HAVING

DISTINCT UNION and set ops merge rows

INSERT UPDATE DELETE = writes

Updatable only if one to one mapping and key survive

VIEW = a named SELECT

Read the map top-down: base tables (with their keys) feed SELECT; SELECT gains filters (which drop NULL/unknown rows), joins, outer joins that pad with NULL, aggregates, and set operations; a view is just a named SELECT; and whether that view is updatable is decided by whether the one-to-one row mapping and the primary key survived all of that.


Equipment checklist

Test yourself — you're ready for the main note when you can answer each aloud.

What is a base table, in one picture?
A named grid on disk where each row is one record and each column is one attribute; it actually holds data.
What is a primary key?
A column whose value is unique and non-empty for every row, so it names exactly one row.
What is a foreign key, and must it be unique?
A column pointing at another table's primary key; it is not required to be unique, so many rows may share the same value.
Why must a view carry the base table's key to stay updatable?
Because the engine needs a unique handle to locate the single base row behind each view row; drop the key and there is no unique target.
What are the two "cuts" a SELECT makes on a grid?
A vertical cut (choose columns, called projection) and a horizontal cut (WHERE chooses rows).
What do <> and != mean in SQL?
Both mean "not equal"; <> is the standard spelling, != is accepted by most engines.
What is NULL, and what does a comparison with NULL return?
NULL means an unknown/missing value; any comparison involving it returns UNKNOWN, not true or false.
Which rows does a WHERE clause keep under three-valued logic?
Only rows where the condition is exactly TRUE; both FALSE and UNKNOWN rows are filtered out.
What does a table alias like employees e do, and how do you use it?
It nicknames the table e; you then write e.column to name a column of that table, resolving ambiguity between tables.
What does the ON clause in a JOIN specify?
The match condition — which columns must be equal for two rows to be stitched together, e.g. e.dept_id = d.dept_id.
How does an outer (LEFT/RIGHT/FULL) join affect updatability?
It can create rows padded with NULL on one side, with no real base row there, making the view ambiguous to write and usually read-only.
What is the difference between UNION and UNION ALL?
UNION removes duplicate rows (implicit DISTINCT); UNION ALL keeps duplicates and is faster.
Why is SELECT * risky in a view?
New base columns get exposed silently (possible leak) and it hides whether the primary key is present, both threatening security and updatability.
Does WHERE control what you can write, or only what you can read?
Only what you read — it hides rows from view but does not stop rows existing in the base table.
Why is AVG irreversible?
Many different sets of numbers share the same average, so "set the average" has infinitely many possible base changes — no unique one.
What single question decides if a view is updatable?
Does each view row map back to exactly one keyed row of one base table (with real columns you can change)?
Name the three write commands that must be translated to the base table.
INSERT, UPDATE, DELETE.