Core CS · DBMS
The design holds still while the data moves
A schema is written once and edited rarely. An instance changes every second. Half the DBMS questions you will be asked live on one side of that line or the other.
Watch one schema survive seven writes →01 The idea
The design and the data are two different things
In C you write int marks; and then marks = 78;. The first line is a type: it fixes what values are legal and it does not change while the program runs. The second is a value: it changes as often as you like, and the compiler checks every assignment against the first line. A database works the same way, one size up. The type of the whole database is called the schema. One legal value of that type is called an instance.
Here is the schema this whole lesson uses. Read it once now, because every trace, table and console step below is written against it.
CREATE TABLE student ( roll INT PRIMARY KEY, name VARCHAR(20) NOT NULL, marks SMALLINT CHECK (marks BETWEEN 0 AND 100));
That is five statements. There is a table called student. A row carries a roll, a name and marks. No two rows may share a roll. A name may never be missing. Marks run from 0 to 100. Five statements, and not one of them is about any particular student.
Now put rows in. (1, Asha, 78) arrives, then (2, Ravi, 65), then (3, Meera, 35). Meera’s mark is corrected to 55. Ravi transfers out and his row is deleted. Five changes, five different instances, and the schema was not touched once. That asymmetry is the whole idea. It is also why the two words are never interchangeable: ALTER TABLE changes the schema and UPDATE changes the instance. An interviewer who hears you mix them up will keep pulling on the thread.
One more warning before the vocabulary. A real database has more than one schema at a time, at the internal, conceptual and external levels, and separating them is a topic of its own in this module. Everything in this lesson is true of any of the three.
02 Worked example
One write, checked against a design that does not move
Follow the marks column of the same three rows through four moments. Each line below is one instance, with only the marks shown: the rolls stay 1, 2 and 3 in that order and the names never change, so marks are the only thing left to watch.
Four moments, four lines, and the design was never mentioned. Read the same four lines in the other direction and the point flips: at every one of them the columns were still roll, name and marks, with the same types and the same constraints. Those are the schema.
The last line is the interesting one, so open it up. Somebody ran INSERT INTO student VALUES (4, 'Nikhil', 350); and the database said no. Nothing about that statement is malformed, and 350 fits comfortably inside a SMALLINT, which holds anything from −32768 to 32767. Here is the whole path it took.
The highlighted node is where students think the decision happens. It is not. The write carries no rules of its own; the rule was written into the catalog long before and is read back on every attempt. So the type was fine and the constraint was not, and the DBMS chose refusal over an invalid state. That is the only choice it is allowed to make.
Notice what that costs. The schema is a promise the database keeps on your behalf, and it can only keep promises you actually wrote down. Nothing above stops Nikhil being recorded as 82 when he really scored 28. A valid state is a legal state, never a true one. And the notation all of this is written in — tables, columns, values matching values — has a name. It is the relational model, and it is one of seven worth knowing.
03 Mechanics
Seven data models, and the four things that separate them
A data model is the notation a schema may be written in: which structures you are allowed to build, which constraints you may state, and which operations you get for free. That is a third thing, one step more abstract than a schema, and interviewers test whether you can keep the three apart. The relational model is a data model. student(roll, name, marks) is a schema written in it. The two rows in the table today are an instance of that schema.
Seven models come up. Two are history you are still asked about, one is a drawing tool, one is a road not taken, and three are what you actually use. Ignore the adjectives people attach to them and compare the four columns that genuinely differ.
| Model | How data is structured | How a relationship is represented | Query language | Where you still meet it |
|---|---|---|---|---|
| Hierarchical | Segments in a tree: one root segment type per database, everything else hanging beneath it. IBM, shipped with IMS in 1968 | parent-to-child links; a child has exactly one parent |
Navigational: DL/I calls such as GET UNIQUE and GET NEXT, one segment at a time | IBM IMS on mainframes, still running core banking and insurance. Also every tree you already use: DNS, LDAP, a filesystem, nested JSON |
| Network | Records joined into owner-member sets: a graph, not a tree. Standardised by the CODASYL Data Base Task Group, 1971 — not by IBM, and not by ANSI | set pointers; a record may be a member of many sets, so many-to-many is direct |
Navigational: CODASYL DML, FIND and GET, walking from one current record to the next | IDMS on mainframes. The navigational style resurfaces in graph databases such as Neo4j, which are a separate model of their own |
| Relational | Relations: unordered sets of tuples over named, typed attributes. Proposed by E. F. Codd at IBM Research in 1970 | matching values; a foreign key holds the same value as some candidate key |
Declarative SQL, defined over relational algebra. You state what you want, not how to reach it | PostgreSQL, MySQL, Oracle, SQL Server, SQLite. Most transactional data in the world |
| ER | Entity sets, attributes and relationship sets, drawn rather than stored. Proposed by Peter Chen in 1976 | a relationship is a first-class thing, with a cardinality ratio and a participation constraint |
None. An ER diagram is mapped to a relational schema before anything runs | The design step before CREATE TABLE, and every modelling tool: MySQL Workbench, dbdiagram, ERwin |
| Object-oriented | Objects with their own identity, grouped into classes, with inheritance and methods stored beside the data | direct object references held as object identifiers |
OQL from the ODMG standard, plus ordinary method calls from the host language | ObjectDB, db4o, GemStone. Niche. The idea won anyway, as ORMs sitting on top of relational databases |
| Object-relational | Tables, but a column may hold a user-defined type, an array or a whole document | still foreign keys, plus reference types |
SQL with the object extensions added in SQL:1999 | PostgreSQL composite types, arrays, JSONB and table inheritance; Oracle object types. Most databases you call relational are really this |
| Document (NoSQL) | Self-describing documents in a collection; two documents need not agree on their fields | embed the child inside the parent, or store a reference and join in the application or with $lookup |
A query API rather than a language: MongoDB’s query documents and its aggregation pipeline | MongoDB, Couchbase, Firestore, DocumentDB. Also JSONB columns living inside PostgreSQL |
05 Cheat sheet
The words they will make you define
Each term has a short answer that holds up and a shorter one that invites the follow-up in the third column. The hollow answers are not wrong, which is exactly why they get you into trouble.
| Term | Say this | The answer that invites a follow-up |
|---|---|---|
| Schema | the design: tables, columns, types, keys, constraints | “the structure of the database” — then you are asked which statement changes it, and DDL is the word you left out. |
| Instance | every row in every table at one moment | “the data” — one row is also data. The three words that save you are at one moment. |
| Database state | a synonym for instance: the contents right now | “the state of the DBMS” — that is uptime, sessions and memory, not data. |
| Valid state | an instance satisfying every constraint the schema states | “correct data” — a valid state can be factually wrong. The DBMS enforces legality, not truth. |
| Schema evolution | changing the design of a live database with ALTER | “redesigning it” — evolution is incremental and every existing row has to stay legal. |
| Data model | the notation a schema may be written in, plus its operations | “the diagram” — a diagram is one schema drawn in one model. |
| Relational model | relations of tuples, related by matching values, queried declaratively | “tables” — tables are the picture. Values instead of pointers is the property. |
| ER model | a design notation: entities, attributes, relationships, cardinality | “a kind of database” — no ER database exists. You map the diagram to tables. |
| Object-relational | relational plus user-defined types, arrays and inheritance, per SQL:1999 | “object-oriented” — a different model. Object-relational keeps tables and keeps SQL. |
| Document store | self-describing documents, shape checked on read | “schema-less” — the schema moved into your code. It did not vanish. |
06 Where & why
Where the schema actually lives, and what the word means to each vendor
Schema and instance sound like exam vocabulary until you notice that four databases you have used disagree about what the words mean. Answers that name a real system and a real command land differently from answers that recite a definition.
Column definitions live in pg_catalog and are exposed as information_schema.columns, so “what is the schema” is a SELECT and “what is the instance” is a COUNT. Since PostgreSQL 11, ADD COLUMN with a constant default is a catalog-only change, so a design change on a huge table can finish in milliseconds without touching a single row.
In the SQL standard and in PostgreSQL a schema is a namespace inside a database that holds tables. In MySQL, SCHEMA is a plain synonym for DATABASE. In Oracle a schema is the set of objects owned by one user, so creating the user creates the schema. Worse, Oracle uses instance for the memory and processes that run the database, not for the rows. Say which vocabulary you are in before you answer.
Two documents in one collection may carry different fields, so nothing rejects a stray value at insert time and the reader meets it instead. The design is still there, spread across the documents themselves and the code that reads them. A collection can be given a $jsonSchema validator, available since 3.6, at which point it refuses a bad document much as a CHECK constraint does.
sqlite_master holds one row per table, index, view and trigger, and its sql column keeps the original CREATE statement verbatim. The schema is literally a string. SQLite is also dynamically typed: a column declared INTEGER will accept the text twenty unless the table is declared STRICT, which arrived in 3.37. A declared type is not automatically an enforced one.
07 Interview questions
What they actually ask
This pair of terms opens more DBMS interviews than any other, usually as a warm-up that the interviewer expects to take fifteen seconds. Answer each of these out loud, in about twenty.
What is the difference between a schema and an instance?
Explain that with a programming analogy.
Interviewers sometimes say intension and extension. What do those mean?
What is a database state, and when is it invalid?
Which statements change the schema and which change the instance?
Does an ALTER TABLE change the instance as well?
What is a data model, and how is it different from a schema?
Why did the relational model replace the hierarchical and network models?
ER model versus relational model. Why do we need both?
Is MongoDB schema-less?
Is PostgreSQL relational or object-relational?
In a real project, how often does a schema actually change?
08 Practice problems
Six to work through
For each one, write your answer in two columns before you check anything: what the design says, and what the data says. Most of the mistakes people make on this topic are answers that quietly swap the two.