Skip to content
Skip to page content
soylai.com

Database Concepts — Notes

6 sections 6 min readBeta

1. Why databases?

Keeping records in separate files (registers, spreadsheets) causes five problems:

  1. Data redundancy — the same data is stored in many files.
  2. Data inconsistency — copies disagree after an update is made in only one place.
  3. Data unavailability — the data exists but is hard to retrieve in the needed form.
  4. Unauthorised access — anyone who can open a file sees all of it.
  5. Data dependence — programs depend on the file layout, so changing the layout breaks them.

A database is an organised collection of related data. A DBMS (database management system) is software, such as MySQL, that creates and manages databases: it stores data once and links it, applies rules (keys, constraints), answers queries, controls who can see or change what, and supports backup and sharing.

Table of five file-system problems: redundancy, inconsistency, unavailability, unauthorised access and data dependence, with what goes wrong and an example from an imagined school
ProblemWhat goes wrongIn the imagined school
RedundancySame data stored many timesName typed in three files
InconsistencyCopies disagree after an updateNew phone number in one file only
UnavailabilityData exists but is hard to getBus users with unpaid fees takes days
Unauthorised accessAnyone with the file sees everythingFee clerk can read health notes
Data dependencePrograms break when the file layout changesA new column breaks the fee program
The DBMS sits between users and data

Key terms

Data redundancy

Unnecessary repetition of the same data in several places.

Data inconsistency

Copies of the same data that disagree.

DBMS

Software to create, store, query and control access to a database.

2. The relational data model

In the relational model data is stored in relations (tables). Terms:

  • Tuple (record): one row.
  • Attribute (field): one named column.
  • Domain: the set of values an attribute may take (for example, Age: whole numbers from 0 upwards).
  • Degree: the number of attributes.
  • Cardinality: the number of tuples (the heading row is not counted).

Properties of a relation: attribute names are unique; each value is atomic (single); no two tuples are identical; the order of tuples and of attributes does not matter.

Imagined data. The heading row is not counted in the cardinality.
Table of relational terms with meanings and examples from the imagined STUDENT relation: relation, tuple, attribute, domain, degree 4 and cardinality 5
TermMeaningIn STUDENT
RelationA table of dataSTUDENT
Tuple (record)One row(2203, Meera Iyer, 12B, LC-107)
Attribute (field)One named columnClass
DomainSet of values an attribute may takeClass: 12A, 12B, 12C, …
DegreeNumber of attributes4
CardinalityNumber of tuples5
Table of five properties of a relation: unique attribute names, atomic values, no identical tuples, order of tuples does not matter, order of attributes does not matter
Property of a relationWhy
Each attribute has a unique nameSo that every value can be referred to
Each value is atomic (single)One phone number per cell, not a list
No two tuples are identicalEach row must be distinguishable
Order of tuples does not matterA relation is a set of rows
Order of attributes does not matterColumns are found by name

Key terms

Relation

A table in a relational database.

Tuple

A row (record) of a relation.

Attribute

A named column (field) of a relation.

Domain

The set of values an attribute is allowed to take.

3. Degree, cardinality and the Cartesian product

Degree counts columns and cardinality counts rows. Adding or dropping a column changes the degree; inserting or deleting a row changes the cardinality.

The Cartesian product A × B pairs every tuple of A with every tuple of B:

  • degree(A × B) = degree(A) + degree(B)
  • cardinality(A × B) = cardinality(A) × cardinality(B)

For the imagined STUDENT (4 columns, 5 rows) and FEES (4 columns, 4 rows), the product has degree 8 and cardinality 20 — measured by running the query on the two tables. Most product rows pair unrelated tuples; keeping only rows whose linking values match gives the meaningful ones.

Measured by running the tables in SQLite.
Relation (imagined)DegreeCardinality
STUDENT45
FEES44
ITEM56
SUPPLIER43
DOCTOR43
PATIENT45
A column changes the degree; a row changes the cardinality.
StepDegreeCardinality
ITEM as given56
Add column Shelf66
Insert two items68
Degrees add, cardinalities multiply (measured by running SELECT * FROM STUDENT, FEES).
STUDENTFEESSTUDENT × FEES
Degree444 + 4 = 8
Cardinality545 × 4 = 20

Key terms

Degree

Number of attributes (columns) of a relation.

Cardinality

Number of tuples (rows) of a relation.

Cartesian product

All combinations of the tuples of two relations.

4. Candidate, primary and alternate keys

A key lets the DBMS identify exactly one tuple.

  • Candidate key: an attribute or set of attributes that is unique for every tuple and never NULL. A relation may have several.
  • Primary key: the one candidate key chosen to identify tuples. Exactly one per relation; unique and NOT NULL.
  • Alternate key: a candidate key not chosen as the primary key.
  • Composite key: a key made of two or more attributes, used when no single attribute is unique, e.g. (AdmNo, Subject) in an exam-result relation.

A good primary key is minimal, simple and stable. Attributes such as names or cities can repeat, so they are not keys even if they look unique in a small sample.

Checked by counting matching rows in SQLite.
Attribute of STUDENTUnique for every row?Never empty?Candidate key?
AdmNoYesYesYes
NameNo (Kabir Das twice)YesNo
ClassNo (12A twice)YesNo
LibCardYesYesYes
From candidate keys to primary and alternate keys
Worked example table for the imagined SUPPLIER relation: SupCode is the primary key, LicenceNo the alternate key, and SName and City are not keys
Attribute of SUPPLIERUnique?Role
SupCodeYesPrimary key (chosen)
LicenceNoYesAlternate key
SNameCould repeatNot a key
CityCould repeatNot a key
Key: the pair (AdmNo, Subject), a composite key.
AdmNoSubjectMarks
2201Physics78
2201English84
2203Physics91
2203English72

Key terms

Candidate key

Attribute(s) unique for every tuple and never empty.

Primary key

The candidate key chosen to identify tuples of a relation.

Alternate key

A candidate key that was not chosen as the primary key.

Composite key

A key made of two or more attributes.

5. Foreign keys and referential integrity

A foreign key is an attribute (or set) in one relation whose values must match the primary key of another relation. It links the two: AdmNo in FEES refers to AdmNo in STUDENT; SupCode in ITEM refers to SUPPLIER; DocID in PATIENT refers to DOCTOR.

Referential integrity means every foreign key value must exist in the referenced relation (or be NULL when there is no link yet). The DBMS therefore refuses:

  • a child row whose foreign key matches no parent;
  • deleting a parent that child rows still refer to (unless the table is set to cascade).

A foreign key value can repeat (one supplier, many items), and its name need not match the referenced key — only its values and domain must.

Imagined Nilgiri Vidyalaya tables.
Responses produced by running the changes in SQLite with foreign keys on; MySQL words its messages differently.
Attempted changeDBMS responseRule
New student with AdmNo 2203Rejected (UNIQUE constraint failed: STUDENT.AdmNo)Primary key must be unique
New student with LibCard LC-107Rejected (UNIQUE constraint failed: STUDENT.LibCard)Alternate key must be unique
Receipt for AdmNo 2209Rejected (FOREIGN KEY constraint failed)Foreign key must match a parent
Delete student 2201Rejected (FOREIGN KEY constraint failed)Parent still referred to by receipts
Worked example table of keys in the imagined Seva Hospital database: DOCTOR has primary key DocID and alternate key RegNo; PATIENT has primary key PID and foreign key DocID referring to DOCTOR
RelationPrimary keyAlternate keyForeign key
DOCTORDocIDRegNo—
PATIENTPID—DocID (refers to DOCTOR)

Key terms

Foreign key

Attribute(s) whose values must match the primary key of another relation.

Referential integrity

The rule that every foreign key value refers to an existing tuple (or is NULL).

6. Keys at a glance

Use this table to check answers. In exam answers, name the table with every key, and justify it in a few words: "unique and never empty" for candidate keys, "matches the primary key of …" for foreign keys.

Table comparing keys: candidate, primary and alternate keys are unique and not null; a table has exactly one primary key; a foreign key may repeat and may be null
KeyUnique?NULL allowed?How many per table?
Candidate keyYesNoOne or more
Primary keyYesNoExactly one
Alternate keyYesNo (declare NOT NULL)Zero or more
Foreign keyNo, may repeatYes, if not yet linkedZero or more
Summary table of Part 2: candidate, primary, alternate, foreign and composite keys with one-line rules
KeyOne-line rule
Candidate keyAny attribute set that is unique and never empty
Primary keyThe candidate key chosen to identify tuples
Alternate keyA candidate key not chosen
Foreign keyValues must match the primary key of another table
Composite keyA key made of two or more attributes

Exam tips

Common mistakes

  • Counting the heading row in the cardinality; swapping degree and cardinality.
  • In a Cartesian product, multiplying the degrees (they add) or adding the cardinalities (they multiply).
  • Choosing Name as a key because it looks unique in a small table.
  • Calling an alternate key a foreign key, or saying a foreign key must be unique.
  • Naming a key without its table: write "AdmNo in FEES is a foreign key referring to STUDENT".

How marks are usually awarded

  • 1 mark: the term or the count.
  • 2 marks: definition plus the example from the given table.
  • 3 marks: three points, each tied to the data or scenario.
  • 5 marks: five distinct points, often a comparison or a design with justified keys.