Database schema
Схема базы данных
A database schema describes how a database is organized: what tables and columns it has, how records relate to one another, and what rules the stored data must follow.
Structure, not contents
A database schema describes what can be stored, not what happens to be stored right now. A library database can define a table for book copies and a table for loans before anyone checks out a book. Checking out a copy adds a row; adding a due-date column changes the schema.
What the database enforces
In a relational database, a schema defines tables, column names and data types, and constraints. A primary key identifies a row uniquely. A foreign key requires a reference to match a row in another table (or sometimes the same table). NOT NULL makes a value mandatory, and CHECK can enforce a rule such as quantity > 0. A schema definition can also include objects such as indexes and views.
Constraints matter because the database enforces them regardless of which application writes the data. An entity-relationship (ER) diagram can show the same tables and relationships, but it's a design and communication tool. Drawing a relationship doesn't create a foreign key.
Example: library copies and loans
CREATE TABLE copies (
id bigint PRIMARY KEY
);
CREATE TABLE loans (
id bigint PRIMARY KEY,
copy_id bigint NOT NULL REFERENCES copies(id),
checked_out_on date NOT NULL,
returned_on date,
CHECK (returned_on >= checked_out_on)
);
Each loan must reference an existing copy, and the same copy can appear in multiple loan records over time. A NULL return date means the copy hasn't been returned yet. The check rejects a return date earlier than checkout but allows a missing return date. Notice what this design does not enforce: it doesn't stop two open loans from referencing the same copy. That business rule needs a separate constraint or mechanism.
How schemas are used and changed
Developers consult the schema to find the right columns to join on and to see which values a query might return as NULL. Teams usually version schema changes as migrations. For example, to add a required due date to a table that already has loans, you might first add a nullable column, update the application to write due dates, fill in existing rows, and then enforce NOT NULL. The order matters if old and new application versions run at the same time.
In PostgreSQL, schema also has a narrower meaning: a named namespace inside a database. In library.loans, library is the schema and loans is the table. library.loans and archive.loans can coexist in the same database.
How it comes up in interviews
You might be asked to design a schema for a library or an online store. Start by clarifying the business rules: does a loan refer to a book title or to a particular physical copy? Can a copy have several historical loans? Then explain which rules are enforced by keys and constraints and which aren't. A table diagram is a useful starting point, but walking through a concrete case, such as a second checkout before the first return, shows whether the design actually meets the requirements.