CH 8 · DATABASE CONCEPTS
Unit 3 · Database Management

Chapter 8
Relational Database
Concepts

Chapter 4 stored data in a file. This unit stores it in a way that can be questioned, checked and shared — and that starts with getting the vocabulary exactly right.

1.1 Where we have got to

Chapter 4

You stored the five students in marks.csv and read them back with csv.reader. It worked.

marks.csv — the Chapter 4 versionName,Marks,Stream,HOD
Riya,88,Computer Science,Sharma
Aarav,91,Computer Science,Sharma
Kabir,76,Informatics,Iyer
Meera,94,Computer Science,Sharma
Dev,59,Informatics,Iyer
Three problems, and they are all fatal at scale

① Redundancy. "Computer Science, Sharma" is written three times. Ten thousand students means ten thousand copies.

② Inconsistency. When Sharma is replaced, you must find and change every row. Miss one and the file now says two different things.

③ No rules. Nothing stops you typing Marks = 950, or two students with the same roll number, or a stream that does not exist.

What a database adds

Store each fact once, enforce rules about what is allowed, and let many programs and people ask questions of the same data at the same time without corrupting it.

1.2 The vocabulary of a DBMS

Definition

Database — an organised collection of related data stored in a way that allows it to be accessed, managed and updated efficiently.

Definition

DBMS — Database Management System — software that allows users to define, create, store, retrieve and manage the data in a database, and that controls access to it.

Definition

RDBMS — Relational DBMS — a DBMS in which data is organised as a set of tables (relations), and relationships between tables are expressed by common columns. MySQL, Oracle and PostgreSQL are RDBMSs.

Advantages of a DBMS — the standard 3-mark list

  • Controls redundancy — each fact is stored once
  • Ensures consistency — change it in one place
  • Data integrity — rules are enforced by the system, not by hope
  • Data security — different users get different privileges
  • Data sharing — many users, concurrently and safely
  • Backup and recovery — built in
  • Data independence — programs need not change when storage does
Edgar F. Codd

The relational model was proposed by E. F. Codd at IBM in 1970. Its whole idea is that data should be stored in simple tables and queried mathematically, not navigated by following pointers.

1.3 The database we will use all unit

Two tables, ten rows. They appear on every remaining slide of this unit — Chapter 8, Chapter 9 and Chapter 10.

Table: STUDENT

RollNo (PK)NameSCode (FK)MarksCityFee
101RiyaS0188Delhi4500
102AaravS0191Mumbai4500
103KabirS0276Delhi3800
104MeeraS0194Pune4500
105DevS0259Mumbai3800
106IshanS0367Delhi4000
107NishaS02NULLPune3800
Nisha's Marks is NULL — she was absent. That single cell teaches half of §9.

Table: STREAM

SCode (PK)SNameHOD
S01Computer ScienceSharma
S02InformaticsIyer
S03PhysicsRao
Look at what just happened

"Computer Science, Sharma" now appears once, in STREAM. STUDENT holds only the short code S01. Replace Sharma and you change one cell.

That is the entire relational idea: store each fact once, and refer to it by a key.

Same five students

Riya, Aarav, Kabir, Meera and Dev have been with us since Chapter 4. Nisha and Ishan join here so that we have a NULL and a third stream to work with.

2.1 Every word for one table

RollNo Name SCode Marks 101 Riya S01 88 102 Aarav S01 91 103 Kabir S02 76 104 Meera S01 94 the whole table = RELATION one row = TUPLE / record one column = ATTRIBUTE / field its set of permitted values = DOMAIN 4 rows CARDINALITY 4 columns DEGREE
Five terms, one picture. Every question in this chapter uses these words.
The five definitions

Relation — a table of data, with rows and columns.
Tuple — a single row of a relation; one record.
Attribute — a single column of a relation; one field.
Domain — the set of permitted values for an attribute.
Degree — the number of attributes (columns). Cardinality — the number of tuples (rows).

2.2 Degree and cardinality — worked

STUDENT

RollNoNameSCodeMarksCityFee
101RiyaS0188Delhi4500
102AaravS0191Mumbai4500
103KabirS0276Delhi3800
104MeeraS0194Pune4500
105DevS0259Mumbai3800
106IshanS0367Delhi4000
107NishaS02NULLPune3800
Question 1

State the degree and cardinality of STUDENT, and of STREAM. Then: what happens to each if we insert one student? And if we add a Phone column?

Are you ready for the answer? 🤔
Answer
 DegreeCardinality
STUDENT67
STREAM33

Insert one student → cardinality becomes 8; the degree is unchanged at 6.

Add a Phone column → degree becomes 7; the cardinality is unchanged at 7.

Two things that never change the count

A NULL still counts. Nisha's row is a tuple, so the cardinality is 7, not 6.
Duplicate values in a column change nothing — three students live in Delhi, and cardinality is still 7.

How to never mix them up

Degree = Down the top row, counting columns.
Cardinality = the count of records.

2.3 Domain — the values a column is allowed

Definition

Domain — the set of all permissible atomic values that an attribute may take.

AttributeDomain
RollNopositive integers, 101–999
Namecharacter strings up to 20 characters
SCodeS01, S02, S03 — those in STREAM
Marksintegers 0–100, or NULL
Citycharacter strings up to 15 characters
Why the domain matters

It is what lets the DBMS refuse bad data. A CSV file would happily accept Marks = 950; a database with the domain declared will not. That is the "data integrity" advantage from §1.2, made concrete.

NULL — the value that is not a value

NULL represents a value that is missing, unknown or not applicable. It is not zero, and not an empty string.

Three consequences you will meet in Chapter 9

Marks = NULL is never true — you must write Marks IS NULL.
COUNT(Marks) gives 6, but COUNT(*) gives 7.
AVG(Marks) ignores the NULL row entirely — it divides by 6, not 7.

2.4 The properties of a relation

① Every cell holds one atomic value

No cell may contain a list. A student with two phone numbers needs two rows or a second table — not "9811, 9822" in one cell.

② All values in a column come from one domain

The Marks column cannot hold 88 in one row and "absent" in another. That is what NULL is for.

③ Every attribute name is unique within the table

You cannot have two columns both called Name. Across different tables the same name is fine — both STUDENT and STREAM have SCode, and that is exactly how they are joined.

④ Every tuple is distinct

No two rows may be completely identical. This is why every relation needs a primary key — §3.

⑤ The order of tuples does not matter

A relation is a set of rows. Riya's row being first carries no meaning, and a query may return them in any order unless you write ORDER BY.

⑥ The order of attributes does not matter

You refer to a column by name, never by position. SELECT Name, Marks works regardless of how they are stored.

Properties ⑤ and ⑥ are why SQL looks the way it does

Because order is meaningless, you must name what you want and state how to sort it. There is no "give me the third row" in SQL — and that is deliberate.

3.1 The problem a key solves

Think about it

"Update Riya's marks to 92."

Which row do you change? What if two students are called Riya? What if three live in Delhi and score 88?

Definition

Key — an attribute, or a set of attributes, whose values uniquely identify each tuple in a relation.

The test for any candidate

Ask: "could two different rows ever share this value?"

Name — yes, two students could both be Riya. Not a key.
City — obviously yes. Not a key.
Marks — yes, and one is even NULL. Not a key.
RollNo — no, the school issues each one once. A key.

Judge the design, not the sample data

In our seven rows every Name happens to be unique. That does not make Name a key — the question is whether a duplicate is possible, not whether one is present today. This is the single most common error in this chapter.

3.2 Candidate, primary and alternate

Candidate key

Candidate key — any attribute or minimal set of attributes that can uniquely identify every tuple. A relation may have several.

Primary key

Primary key — the one candidate key chosen by the designer to identify tuples. It can never be NULL and never duplicated.

Alternate key

Alternate key — a candidate key that was not chosen as the primary key.

alternate keys = candidate keys − primary key

Worked example

Suppose STUDENT also had an AadhaarNo column, which is also unique to each person.

AttributeUnique?So it is…
RollNoYescandidate key → chosen as PRIMARY KEY
AadhaarNoYescandidate key → ALTERNATE KEY
NameNonot a key
SCodeNonot a key — it is a foreign key
MarksNonot a key — and it can be NULL
Why RollNo and not AadhaarNo?

Both would work. A designer picks the one that is shorter, stable, always known and controlled by the organisation. The school issues roll numbers, so it can guarantee them; it cannot guarantee anyone has an Aadhaar number.

Composite key

A composite key is a primary key made of two or more attributes together, used when no single column is unique. In a MARKS(RollNo, Subject, Score) table neither RollNo nor Subject alone is unique — but the pair (RollNo, Subject) is.

3.3 Foreign key — the link between tables

STUDENT RollNo Name SCode 101RiyaS01 102AaravS01 103KabirS02 104MeeraS01 105DevS02 106IshanS03 FOREIGN KEY STREAM SCode SName HOD S01Comp SciSharma S02InformaticsIyer S03PhysicsRao PRIMARY KEY Every SCode in STUDENT must exist in STREAM. That rule is referential integrity.
A foreign key in one table points at the primary key of another.
Definition

Foreign key — an attribute in one relation whose values must match the values of the primary key of another relation. It is what establishes a relationship between two tables.
Referential integrity — the rule that a foreign key value must either match an existing primary key value, or be NULL.

3.4 What referential integrity actually prevents

Rejected — an orphan record

INSERT INTO STUDENT VALUES (108, 'Tara', 'S09', 82, 'Delhi', 4000);

Refused. There is no S09 in STREAM, so this student would belong to a stream that does not exist.

Rejected — a dangling reference

DELETE FROM STREAM WHERE SCode = 'S01';

Refused. Riya, Aarav and Meera all point at S01. Deleting it would leave three students referring to nothing.

Allowed

INSERT INTO STUDENT VALUES (108,'Tara','S03',82,'Delhi',4000); — S03 exists.

DELETE FROM STREAM WHERE SCode = 'S03';only if no student references S03.

UPDATE STUDENT SET SCode = 'S02' WHERE RollNo = 101; — S02 exists, so Riya may move stream.

Who enforces this?

The DBMS, automatically, once you declare the foreign key. You do not write a single line of checking code — which is precisely the advantage a database has over the CSV file in §1.1.

A foreign key may be NULL, a primary key may not

If a new student has not yet chosen a stream, SCode may be NULL — "no relationship yet" is a legitimate state. But RollNo can never be NULL, because then the row could not be identified at all.

3.5 All the keys, on one screen

KeyDefinitionNULL allowed?In our database
Candidateany attribute set that can uniquely identify a tuplenoRollNo (and AadhaarNo, if it existed)
Primarythe candidate key chosen to identify tuplesNeverSTUDENT.RollNo · STREAM.SCode
Alternatea candidate key not chosen as primarynoAadhaarNo, if it existed
Compositea primary key made of two or more attributesNever(RollNo, Subject) in a marks table
Foreignmatches the primary key of another relationYesSTUDENT.SCode → STREAM.SCode
Only the foreign key may be NULL. That single row answers a common one-mark question.
Question 2

In a table LIBRARY(BookID, ISBN, Title, Author, MemberID)BookID and ISBN are both unique, and MemberID refers to the MEMBER table's primary key. Identify each kind of key.

Are you ready for the answer? 🤔
Answer

Candidate keys: BookID and ISBN — both uniquely identify a book.

Primary key: BookID — chosen by the library, short and under its own control.

Alternate key: ISBN — a candidate key that was not chosen.

Foreign key: MemberID — it refers to the primary key of MEMBER. It may be NULL, which is exactly how the table records a book that is currently on the shelf.

Not keys: Title and Author — two books can share either.

4.1 Everything on one screen

The model

relation = table tuple = row = record attribute = column = field domain = allowed values degree = number of columns cardinality = number of rows NULL ≠ 0 and ≠ ""

Properties of a relation

atomic values only one domain per column unique attribute names no duplicate tuples row order is meaningless column order is meaningless

Keys

candidate — could identify primary — chosen, never NULL alternate — candidate not chosen composite — two columns together foreign — points at another PK, may be NULL referential integrity
The three sentences worth the most marks

Degree is the number of attributes and cardinality is the number of tuples in a relation.
A primary key uniquely identifies each tuple and can neither be NULL nor duplicated.
A foreign key is an attribute whose values must match the primary key of another relation; this rule is called referential integrity.

4.2 Exam question 1 — read the table

4 marks

Consider the table HOSPITAL.

PatIDNameDeptAgeDoctor
P01AnilCardiology54Nair
P02SaraOrtho31Bose
P03AnilOrtho47Bose
P04ZoyaCardiologyNULLNair
  1. State the degree and cardinality.
  2. Identify the primary key and justify your choice.
  3. Can Name be a primary key? Give a reason from the data.
  4. What is the cardinality after inserting two rows and deleting one?
Are you ready for the answer? 🤔
Answer

1. Degree = 5 (PatID, Name, Dept, Age, Doctor). Cardinality = 4. The NULL in Zoya's Age does not reduce the count — the tuple exists.

2. PatID. Its values — P01 to P04 — are unique and never NULL, so it identifies each patient uniquely.

3. No. Anil appears in both P01 and P03, so Name is not unique. Here the data itself proves it, which is the strongest form of justification.

4. 5. Cardinality changes with rows: 4 + 2 − 1 = 5. The degree is still 5, because no column was added or removed.

4.3 Exam question 2 — theory

2 + 2 + 1 marks
  1. Differentiate between a candidate key and an alternate key, with an example.
  2. What is a foreign key? How does it enforce referential integrity?
  3. Can a primary key be NULL? Can a foreign key?
Are you ready for the answer? 🤔
Answer 1

A candidate key is any attribute or minimal set of attributes capable of uniquely identifying every tuple of a relation. A relation may have several.

An alternate key is a candidate key that was not selected as the primary key.

In STUDENT(RollNo, AadhaarNo, Name, …) both RollNo and AadhaarNo are candidate keys. If RollNo is chosen as the primary key, then AadhaarNo is the alternate key.

Answer 2

A foreign key is an attribute in one relation whose values must match the values of the primary key of another relation. It creates the relationship between the two tables.

It enforces referential integrity by ensuring that every foreign key value actually exists in the referenced table. The DBMS therefore refuses an insert that names a non-existent parent row, and refuses a delete of a parent row that children still refer to. This prevents orphan and dangling records.

Answer 3

A primary key can never be NULL — a tuple with no identifying value could not be distinguished from another.

A foreign key may be NULL, which simply means the tuple is not currently related to any row in the other table.

End of Chapter 8

Next: SQL

You now have the vocabulary and the two tables. Chapter 9 is the language for asking them questions — and every single query will be run against the STUDENT and STREAM tables you have just met.

Carry these three forward

NULL in Nisha's Marks — it changes WHERE, COUNT and AVG, and each one is examined.
SCode is the foreign key — it is the column every JOIN in Chapter 9 uses.
Row order is meaningless, so any question about ordering must be answered with ORDER BY.