skip to content

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%

answer

  1. db schema = all relation schemas + all constraints
  2. inter-relational constraints: referential integrity
  3. db instance legal only if ALL constraints hold together
  4. SQL schema object = namespace, different concept
  5. schema in version control, instance in backups

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.

solid answer

~60 s

The **database schema** is the union of the individual relation schemas plus every constraint declared over them. Constraints are the part people forget: some are local to one relation (domains, keys, check conditions), and some are **inter-relational**, above all referential constraints saying values of one attribute must appear as key values in another relation. The **database instance** is a corresponding set - one instance per relation - and it is legal only if all relations *jointly* satisfy every constraint. Legality is a property of the whole database state, not of one table at a time, which is precisely why referential integrity has to be checked across relations. The overloaded word is worth flagging: in SQL, a schema is also a **namespace** - a container that groups tables, views and other objects, qualifies their names, and acts as a unit of access control. That object is organisation, not the theoretical intension. When someone says 'we deploy the schema' they usually mean the migrated definitions; when they say 'the reporting schema' they usually mean the namespace.

go deeper

for a junior

Say the database schema is all the table definitions taken together and the instance is all the data currently in them.

for a middle

Include constraints as part of the schema, distinguish intra- from inter-relational ones, and flag that SQL also uses 'schema' to mean a namespace.

for a senior

Emphasise that legality is a whole-database property because of cross-relation constraints, and apply the version-control-versus-backup test for anything ambiguous.

for a principal

Treat the schema as the shared contract across all consumers - its change process, ownership and namespace layout are governance decisions, while the instance is a capacity and recovery concern.

## Building the database schema out of relation schemas A single **relation schema** gives a relation's name and its heading - the set of attributes, each with a domain. A **database schema** is then the set of all relation schemas in the database, plus the constraints declared over them. The constraint half is what candidates most often omit, and it is where the interesting content lives. Constraints come in two shapes: - **Intra-relational** constraints, which can be evaluated by looking at one relation alone: domain constraints (the attribute's permitted value set), key constraints (some attribute set uniquely identifies a tuple), and check-style conditions over tuples of that relation. - **Inter-relational** constraints, which involve more than one relation. The canonical case is **referential integrity**: an attribute in one relation may only hold values that appear as key values in another relation, or be absent. Theory also permits general assertions spanning several relations, which engines support only partially. Because inter-relational constraints exist, the schema is genuinely a property of the database as a whole. You cannot decide whether a state is legal by inspecting one relation in isolation. ## What the database instance is A **database instance** is a set containing one instance for each relation schema in the database schema - a complete snapshot of the data at one instant. It is **legal** only if every constraint in the database schema holds simultaneously across all of those relation instances. That 'simultaneously' is what makes correctness a whole-database property. A referencing tuple pointing at a key value that is not present anywhere makes the *database* illegal, even though each relation, examined alone, looks fine. ## A checklist of what sits on each side On the **schema** side: relation names; attribute names and their domains; whether an attribute may be absent; key declarations; referential constraints; check conditions; default value definitions; view definitions, which are named derived relations and therefore part of the definition; and the definitions of any stored procedural or trigger logic. On the **instance** side: the tuples themselves, in every relation - and nothing else that carries meaning. The rows in a lookup table are instance data even though they feel definitional, which is why teams have to decide deliberately whether reference data ships with migrations or with data loads. Auxiliary structures such as indexes are a third category: their *definitions* are part of the stored database definition, while their contents are derived from the instance and carry no information not already in it. The practical test is simple: if losing it means the definitions are gone, it belongs to the schema and should live in version control; if losing it means data is gone, it belongs to the instance and needs backup and recovery. ## The word 'schema' is overloaded, and the overload matters SQL uses schema for a **namespace object**: a container inside a database that groups tables, views, routines and so on, gives them a qualified name, and acts as a unit for access control and name resolution. Teams use these namespaces to separate concerns - one for the application's core tables, one for staging, one per module or tenant. That is a completely different concept from the theoretical intension. A single SQL namespace contains many relation schemas; a database's overall schema in the theoretical sense spans all of its namespaces. Both meanings are legitimate and both are in daily use, so the professional move is to notice the ambiguity out loud and say which one you mean. In an interview, defining the theoretical sense first and then noting the namespace sense demonstrates exactly the precision the question probes for. A third informal usage adds to the confusion: 'the schema' is often shorthand for the migration files themselves - the versioned artefacts that produce the definitions. That is fine as long as everyone understands the artefact is not the concept. ## Why the distinction pays off Separating definition from state gives you a clean rule for almost every operational question. Definitions are code: reviewed, versioned, migrated forward, coordinated with the applications that depend on them. State is data: written by transactions, sized by growth, protected by backups, restored by recovery. When a change touches both - adding a constrained attribute and populating it - you can see immediately that it is two kinds of change bundled into one step, which is exactly why such changes need careful sequencing.

  • Why can legality of a database state not always be decided one relation at a time?
    Because some constraints span relations. A referential constraint says values in one relation must appear as key values in another, so evaluating it requires looking at both. Each relation can individually satisfy its own domain and key constraints while the combined state still violates referential integrity, which makes legality a property of the whole database instance.
  • Is the content of a small lookup table part of the schema or the instance?
    Strictly it is instance data - the rows are tuples like any others, no matter how definitional they feel. In practice teams often manage such reference data alongside migrations so that code depending on specific codes finds them present, but that is a delivery convention, not a change of category. Recognising the difference avoids assuming those rows are guaranteed to exist.

saying these in an interview costs you the question

  • Describing the database schema as only the table definitions, omitting constraints
  • Assuming every constraint can be checked within a single relation
  • Conflating the SQL namespace object with the theoretical schema without noticing the ambiguity
  • Claiming index contents are part of the schema, or that they add information beyond the instance
  • Treating lookup-table rows as schema because they change rarely

context