skip to content

questions

3

Explain the difference between a relation schema and a relation instance - the terms intension and extension - and give a concrete example of each.

level: juniorimportance: must knowfreq 52%

answer

  1. intension = schema = definition
  2. extension = instance = the tuples now
  3. one schema, many legal instances
  4. class vs object, type vs value
  5. schema in migrations, instance in backups

basics

~20 s

The schema (intension) is the definition: the relation's name, its attributes and their domains, plus the constraints declared on it. The instance (extension) is the actual set of tuples present at one moment. One schema, many possible instances over time.

solid answer

~50 s

A **relation schema** is the definition - the relation's name, the set of attributes with their domains, and the constraints declared on it. It is also called the **intension**: what the relation means. `EMPLOYEE(emp_id: INTEGER, name: TEXT, dept_id: INTEGER)` with `emp_id` as key is a schema. A **relation instance** is the set of tuples that satisfy that schema at a particular moment - the **extension**, the data actually there. The three employee rows in the table right now are one instance; after tonight's hiring batch there is a different instance of the same schema. The distinction matters because the two have completely different lifecycles. The schema changes rarely and deliberately, through reviewed, versioned migrations, and it is what queries and application code are written against. The instance changes on every write, potentially thousands of times a second, and is what backups and monitoring track. A useful shorthand is class versus object, or type versus value: the schema is the shape, the instance is one filling of it.

code

text · 6 lines
text
Schema (intension), unchanged all week:
  EMPLOYEE(emp_id: INTEGER, name: TEXT, dept_id: INTEGER)  key {emp_id}

Instance (extension) on Monday:        Instance on Friday:
  (1, 'Ada',   10)                       (1, 'Ada',  10)
  (2, 'Grace', 10)                       (3, 'Alan', 20)

go deeper

for a junior

Give the two definitions and one concrete example. Naming the pair intension and extension is a bonus, not a requirement.

for a middle

Add that one schema admits many legal instances and that constraints are declared at the schema level so they hold for all of them.

for a senior

Draw the operational consequence: schema changes are versioned, reviewed migrations with compatibility risk, while instance changes are ordinary transactions with durability and volume concerns.

for a principal

Use the split to organise change management - contract evolution and coordination on the intension side, capacity, correctness and recovery on the extension side.

## The two levels Every relational database exists on two levels at once, and mixing them up is a reliable source of confused reasoning. The **schema**, also called the **intension**, is the description. For a single relation it consists of the relation's name, its heading - the set of attributes, each with a domain that fixes the values it may take - and the constraints declared over it, such as which attribute or combination of attributes is the key and which values are permitted. Written compactly: `EMPLOYEE(emp_id: INTEGER, name: TEXT, dept_id: INTEGER, hired_on: DATE)`, key `{emp_id}`. The **instance**, also called the **extension**, is the actual body of tuples that exists at one moment in time. It is one specific value of the type the schema describes. Right now `EMPLOYEE` may hold four tuples; after the payroll batch runs it holds four hundred. Those are two different instances of the same, unchanged schema. ## The relationship between them One schema admits many instances - in principle every set of tuples that conforms to the heading and satisfies every declared constraint. Such an instance is called a **legal** or **valid** instance of the schema. Instances that would break a constraint are not merely undesirable; they are excluded by definition, and the engine's job at runtime is to ensure the database never lands in one. The empty instance is worth calling out: a relation with zero tuples still has its full schema, and queries against it succeed and return nothing rather than failing. Emptiness is a property of the extension, never of the intension. ## Different rates and mechanisms of change This is the practical heart of the distinction. The schema changes rarely and by deliberate act. Changes to it are expressed as definition changes, reviewed like code, versioned in source control, and applied through migrations. A schema change alters the *type* of the data and can therefore invalidate queries, application objects, reports and integrations. The blast radius is large and the change is coordinated. The instance changes constantly and automatically. Every insert, update and delete moves the database from one instance to another. No review, no migration, no coordination - just transactions. What you protect here is durability and correctness, not compatibility. The two even live in different places: the schema lives in the system catalog (the database's own metadata) and, in a healthy project, in migration files under version control; the instance lives in data pages and in backups. ## Why interviewers ask this The distinction quietly underlies a lot of later reasoning, and answers that blur it go wrong in predictable ways: - **Constraints belong to the schema.** Saying 'this attribute has no duplicates' is a statement about today's extension; declaring a uniqueness constraint is a statement about every future instance. Only the second is a guarantee. - **Queries are written against the schema but evaluated against an instance.** A query that returns the right answer today has been checked against one extension, not against every extension the schema permits - which is why sample data proves much less than people assume. - **Testing and deployment split along the same line.** Schema changes need migration and compatibility thinking; data changes need volume, correctness and recovery thinking. - **The vocabulary recurs.** 'Intension versus extension' is the textbook phrasing you will meet in academic material, and the same idea reappears as type versus value, class versus object, description versus state. ## Saying it well One sentence of definition, one concrete example, one sentence of consequence: 'The schema is the definition - attributes, domains, constraints; the instance is the set of tuples present at a moment. `EMPLOYEE(emp_id, name, dept_id)` is the schema, the four rows in it right now are one instance. The schema changes by migration and is what code is written against; the instance changes with every transaction.'

  • A table currently has no duplicate values in one of its columns. Is that a fact about the schema or about the instance?
    About the instance - it describes the tuples present right now. It becomes a fact about the schema only when a uniqueness constraint is declared, at which point it holds for every future legal instance. Until then it is an accident of today's data that the next insert may destroy, and no code should rely on it.
  • Can a schema have zero instances, or an instance with zero tuples?
    An empty instance - a relation with no tuples - is entirely normal and still conforms to its schema; queries against it return empty results rather than errors. A schema without any instance at all is not a normal runtime state, because defining a relation immediately gives it an instance, initially the empty one. Emptiness is always a property of the extension.

The schema is the blank form printed for a survey - its questions and the answers each will accept. Each stack of completed forms collected on a given day is an instance. Reprinting the form is a rare, coordinated act; collecting more responses is not.

saying these in an interview costs you the question

  • Using schema and instance interchangeably, or calling the data 'the schema'
  • Thinking an empty table has no schema or an incomplete one
  • Believing a property observed in current data is guaranteed by the schema
  • Assuming each schema has exactly one instance rather than many possible ones over time

context

open as a page

A database schema is often described as a set of relation schemas plus the constraints over them. What exactly belongs to that database schema, what belongs to the database instance at a given moment, and how does that differ from what SQL calls a schema?

level: middleimportance: should knowfreq 42%

basics

~20 s

The database schema is every relation schema - names, attributes, domains - plus all constraints, including ones spanning relations such as referential constraints. The database instance is one instance of each relation, all satisfying those constraints together. In SQL, 'schema' also means a namespace grouping objects, which is a different idea.

open as a page

Why is an integrity constraint described as a property of the schema rather than of the data, and what follows from saying that only legal instances are permitted - including for the intermediate states a transaction passes through?

level: seniorimportance: should knowfreq 38%

basics

~20 s

A constraint is a predicate declared on the schema, so it must hold for every instance the database is ever allowed to reach, not just the current one. A property that merely happens to be true of today's data guarantees nothing. Enforcement therefore checks state transitions, and some constraints must be checked at transaction end rather than per statement.

open as a page