How a database keeps data correct and private

9 min 6 sections 5 practice questions 11 sources cited

A database is an organised store of records with its own software in charge of every read and every change. Most business data lives in one. That software can refuse a change that would leave the data wrong, keep two users from overwriting each other, and show each user only part of what is held. No experience of databases is assumed.

By the end of this unit you will be able to:

  • Explain table, row, column, primary key and foreign key, with their formal names
  • Explain each of the four ACID properties using one transaction
  • Explain how locks prevent a lost update and how a deadlock arises
  • Tell apart aggregation and inference, and choose a countermeasure for each
  • Explain what a view and polyinstantiation each hide

One spreadsheet, three kinds of trouble

An illustrative example

A village tennis club keeps its members and court bookings in one large spreadsheet. Each booking row repeats the member's name, address and phone number.

Mrs Adeyemi moves house. The secretary corrects her address on some rows and misses others, so the club now holds two addresses for her. On Saturday two volunteers open the sheet at once to take bookings, and the second to save wipes out the first one's work. A new volunteer, who only needs to confirm court times, can scroll across to every member's date of birth and medical notes.

Three different things have gone wrong. What would a properly built database do about each of them?

A relational database is a set of linked tables

A database management system (DBMS) is the software that holds a database and carries out every request to read or change it. Users and programs never touch the stored data directly. They ask the .

Most business databases follow the relational model: all data is held in tables. Older designs held records in a tree, each with one parent (the hierarchical model), or in a web where a record may have many parents (the network model). An object-oriented database stores objects with their methods. A table holds one kind of thing, such as members. Each row is one member. Each column is one fact about every member, such as phone number. The model has formal names for these, and both sets are used.

Everyday and formal names in a relational database
Everyday nameFormal nameWhat it is
TableRelationAll the records of one kind
Row or recordTupleOne record: one member, one booking
Column or fieldAttributeOne fact held about every record
Number of columnsDegreeA table with four columns has degree 4
Number of rowsCardinalityA table of 300 members has cardinality 300

The schema is the description of the database's structure: its tables, their columns, and the rules the data must obey. SQL, the structured query language, is the language in which programs and people put requests to the DBMS.

Not every database is relational. Stores often called NoSQL hold documents or simple pairs of name and value instead of tables. They scale across many servers and typically give weaker guarantees than the ones described below.

Keys identify each row and link the tables

Two members can share a name. The database needs something that never repeats. A candidate key is any column, or set of columns, whose value is different in every row. The primary key is the candidate key chosen to identify the rows. It can never be empty and never repeat. In the Members table it is the member number.

A foreign key is a column in one table that holds the primary key of a row in another. Each row of the Bookings table carries a member number. That number is all the booking needs to hold about the member, because the DBMS can follow it to the right row of Members.

A foreign key links each booking to one member

A foreign key links each booking to one memberTwo tables from a tennis club. Bookings holds booking number, court and time, and member number. Members holds member number and the member's name, address and phone. Member number in Bookings points to member number in Members, and referential integrity guards it.Bookings tableBooking numberprimary keyCourt, timeof the bookingMember numberforeign key: 512Members tableMember numberprimary key: 512Name, address, phonestored onceReferentialintegritymust matcha memberpoints toA foreign key links each booking to one memberTwo tables from a tennis club. Bookings holds booking number, court and time, and member number. Members holds member number and the member's name, address and phone. Member number in Bookings points to member number in Members, and referential integrity guards it.Bookings tableBooking numberprimary keyCourt, timeof the bookingMember numberforeign key: 512Members tableMember numberprimary key: 512Name, address, phonestored onceReferentialintegritymust matcha memberpoints to
Read this figure as text

A tennis club keeps its members in one table and its court bookings in another. A key links them, so that each member's details are stored once and reached from every booking.

  1. Bookings has a booking number, the court and time, and a member number. Its primary key is the booking number, which can never be empty and never repeat.
  2. Members has a member number and the member's name, address and phone. Its primary key is the member number.
  3. The member number in Bookings is a foreign key: a column that holds the primary key of a row in another table. It points to the matching row in Members, so Mrs Adeyemi's address is stored once and reached from all her bookings.
  4. Referential integrity is the rule that a foreign key must point at a row that exists. The database refuses a booking for member 512 if there is no member 512, and refuses to delete a member who still has bookings.

Referential integrity is the rule that a foreign key must point at a row that exists. The DBMS refuses a booking for member 512 if there is no member 512, and refuses to delete a member who still has bookings.

Normalisation is the work of arranging tables so that each fact is stored once. It proceeds through numbered rules called normal forms. The first makes every cell hold one value and removes repeating groups of columns. The second requires every column outside the key to depend on the whole primary key, not on part of it. The third requires every column outside the key to depend only on the key, never on another column. A member's town held beside her postcode breaks the third form, because the town depends on the postcode. Mrs Adeyemi's address then exists in one row of one table, and correcting it once corrects it everywhere.

A transaction is all or nothing: the four ACID properties

A transaction is a group of changes that must succeed or fail as one. Moving 200 pounds from a savings account to a current account takes two changes: subtract 200 from one balance, add 200 to the other. If the power fails between them, 200 pounds has vanished. A DBMS prevents this by giving every transaction four properties, known by their initials as ACID.

Atomicity
All of the transaction's changes happen, or none do. After the power cut the subtraction is undone, as if the transfer had never started.
Consistency
The data obeys every rule of the schema when the transaction ends. If a rule says no balance may fall below zero, a transfer that would break it is refused whole.
Isolation
Nobody else sees a transaction's work until it is finished. A statement printed halfway through shows both old balances or both new ones, never one of each.
Durability
Once the transaction is confirmed, its changes survive a crash or a power failure.

Two commands end a transaction. Commit confirms it: the changes become permanent and visible to others. Rollback abandons it: every change since the transaction began is undone.

ACID: Atomic, all or nothing. Consistent, the rules still hold. Isolated, unfinished work is invisible. Durable, confirmed work survives a crash.

Two users at once: concurrency, locks and deadlock

Concurrency means several users working in the database at the same moment. Without control it produces a lost update. Two shop assistants each read that ten kettles are in stock. Each sells one and writes back nine. Two kettles have gone and the database says nine remain.

A lock lets one transaction hold a row, or a whole table, so that others must wait. The first assistant's transaction locks the kettle row, writes nine and commits. Only then does the second read the row, see nine and write eight.

Locks bring their own failure. In a deadlock two transactions each hold a lock the other needs, so neither can finish. The DBMS detects the circle, rolls one transaction back and lets the other complete. The one rolled back is then tried again.

Locks protect and cost . A transaction that holds its locks for a long time keeps every other user waiting.

Limiting what a user sees: permissions and views

SQL commands fall into three groups, and the third is the .

Three groups of SQL command
GroupPurposeCommands
Data definition language (DDL)Builds and alters the structureCREATE, ALTER, DROP
Data manipulation language (DML)Reads and changes the dataSELECT, INSERT, UPDATE, DELETE
Data control language (DCL)Decides who may do whatGRANT, REVOKE

A view is a stored query that the DBMS presents as if it were a table. It holds no data of its own. The club defines a view showing only booking time, court and member name. The new volunteer is granted the view and not the Members table. Date of birth and medical notes are simply not there for her to scroll to. A view is how , access to no more than the task needs, is applied inside a database.

Aggregation, inference and polyinstantiation

Two leaks can occur even when every permission is set correctly.

Aggregation and inference
PointAggregationInference
What happensMany items, each harmless alone, are collectedA hidden fact is worked out from facts the user may see
ExampleOne minister's flight plan is public. The full set for a month reveals a pattern that is notTotal pay of a team of four, then the total without the manager. The difference is her salary
Countermeasures the collection higher than its items. Limit how much one user can retrieveRefuse answers drawn from very few records. Round or add small errors to totals. Suppress revealing cells

Both grow in a data warehouse, a large store that combines data from many systems for analysis. They grow again with data mining, the search for patterns in large sets of data. Limiting the number and frequency of queries and watching the query logs are controls for both.

Polyinstantiation answers a particular inference in databases that hold data at several classification levels. Two rows are kept with the same key at different levels. A naval database has an unclassified row for the ship Calder: cargo, food. A secret row for the same ship says: cargo, weapons. A user cleared only to unclassified sees the first row, a cover story. Without it, the message access denied would itself reveal that the Calder carries something secret.

A question, worked through

A hospital trust lets its research analysts query the patient database for statistics only. They may ask for counts and averages and may not see any patient's record. A reviewer finds that an analyst asked how many patients on one ward had a rare diagnosis and received the answer 1. The analyst then asked for the average age of that group. The security manager is asked to recommend a control.

Which control BEST addresses what the analyst did?

  1. Refuse any statistic that is drawn from fewer than a set number of records
  2. Encrypt the patient table so that the stored records cannot be read
  3. Give the analysts a view that leaves out the patient name column
  4. Lock each patient row while a statistical query is reading from it
Decide on your answer, then open the reasoning
  1. First name what happened. The analyst used permitted statistics to learn one patient's age. Working out a hidden fact from allowed answers is inference.
  2. Rule out d. Locks stop two users corrupting each other's changes. They protect integrity and have nothing to do with what a reader can deduce.
  3. Rule out b. Encryption protects stored data from someone who bypasses the DBMS. The analyst went through the DBMS with valid permission, and it decrypted the data to compute the answer.
  4. Between a and c: a view without names sounds right, but the analyst never saw a name. The count of 1 did the damage, because an average over one person is that person's value.
  5. The control aimed at inference is to refuse statistics over very small groups. Option a.

Answer: A. Refuse any statistic that is drawn from fewer than a set number of records

Choosing between good answers

A question describes harmless items becoming sensitive once collected
the answer is aggregation. If a hidden fact is deduced from permitted answers, it is inference.
A question asks which ACID property is at stake
match the symptom. Half a transfer applied is atomicity, a broken rule is consistency, a half-finished change seen by others is isolation, and a confirmed change lost in a crash is durability.
A scenario has two rows with the same key at different classification levels
it is polyinstantiation, used so that a refusal does not reveal that a secret exists.
A user should see only some columns or rows of a table
choose a view and grant access to the view, not to the table.

Check your understanding

5 questions on this unit, easiest first. Confirm an answer to see the reasoning for it, then the right answer, the other options and the rule to reuse. Your answers are kept in this browser and feed readiness and review.

These questions are drafts that no named reviewer has approved yet.

Question 1 of 5

During a transfer between two accounts, a database server loses power after the debit is written and before the credit. On restart the database undoes the debit. Which ACID property is at work?

What to remember

  • A relation is a table, a tuple is a row and an attribute is a column. Cardinality is the number of rows and degree the number of columns. The primary key identifies each row, and a foreign key links a row to a row in another table.
  • Referential integrity means a foreign key always points at a row that exists. Normalisation stores each fact once.
  • ACID: atomicity is all or nothing, consistency keeps the rules, isolation hides unfinished work, durability keeps committed work.
  • Locks prevent lost updates. A deadlock is two transactions each waiting for the other's lock, and the DBMS ends it by rolling one back.
  • A view shows chosen columns and rows as if they were a table, and is the database's tool for least privilege.
  • Aggregation is sensitivity from collection. Inference is deduction from permitted answers. Polyinstantiation keeps a cover row at the lower level.

Say it from memory

  1. What are degree and cardinality, a primary and a foreign key, and what do second and third normal form require?

    Degree is the number of columns and cardinality the number of rows. A primary key identifies each row and never repeats or stays empty. A foreign key holds the primary key of a row in another table. Second normal form: every column outside the key depends on the whole primary key. Third: no such column depends on another column instead of the key.

    Read this part again

  2. What do the four ACID properties mean, and how does a DBMS end a deadlock?

    Atomicity: all or nothing. Consistency: the rules still hold. Isolation: unfinished work is invisible to others. Durability: committed work survives a crash. In a deadlock two transactions each wait for the other's lock, and the DBMS rolls one back.

    Read this part again

  3. What do a view, aggregation, inference and polyinstantiation each mean in a database?

    A view shows chosen rows and columns as if they were a table and applies least privilege. Aggregation is sensitivity that arises from collecting harmless items. Inference is deducing a hidden fact from permitted answers. Polyinstantiation keeps rows with the same key at different levels, so the lower row is a cover story.

    Read this part again

  4. What are the formal names for a table, a row and a column in a relational database, and what is the schema?

    A table is a relation, a row is a tuple and a column is an attribute. The schema is the description of the database's structure: its tables, their columns, and the rules the data must obey.

    Read this part again

  5. What is a lost update, and how does a lock prevent it?

    Two users read the same value at once, each changes it and writes back, so one change is lost, as when two sales of kettles leave the stock at nine. A lock lets one transaction hold the row until it commits, so the other must wait and then reads the new value.

    Read this part again

  6. Name the three groups of SQL command with their commands, and say which group is the access control.

    Data definition language builds and alters the structure: CREATE, ALTER, DROP. Data manipulation language reads and changes the data: SELECT, INSERT, UPDATE, DELETE. Data control language decides who may do what: GRANT, REVOKE. Data control language is the access control.

    Read this part again

Sources