Your school stores the details of thousands of students β names, classes, fees, marks, attendance. π« Your bank stores every rupee you deposit, and a railway website stores lakhs of bookings every day. π Where does all this data live, and how is it kept correct, safe and quick to find? The answer is a database! This chapter answers the BIG questions: What problems happen when data is kept in ordinary files? What is a database and a DBMS? What is a relation, a tuple, an attribute and a domain? And what are primary, candidate, alternate and foreign keys? Let's get organised! ποΈ
The most tested topics are limitations of the file system, advantages of a DBMS, relational model terms (relation, attribute, tuple, domain, degree, cardinality), and keys β especially "identify the primary, candidate and alternate keys in this table". Practise finding degree and cardinality on every table you see!
8.1 π Introduction β Why Do We Need a Database?
Before computers, schools kept records in registers. π Later, people started keeping data in computer files β a separate file for every job. This is called a file system.
Imagine a school where the office, the library and the exam cell each keep their own file of students:
π Office file π Library file π Exam file
βββββββββββββββ ββββββββββββββββ βββββββββββββ
Roll, Name, Address Roll, Name, Address Roll, Name, Marks
101, Riya, Salt Lake 101, Riya, Salt Lake 101, Riya, 92
102, Aman, Howrah 102, Aman, Park St β 102, Amann β, 85
Can you spot the problems? Aman's address is different in two files, and his name is misspelt in the third! π¬
8.1.1 Limitations of the File System β οΈ
Data Redundancy
The SAME data is stored again and again in many files β wasting space
DuplicationData Inconsistency
Copies of the same data don't match β which one is correct?
MismatchData Isolation
Data is scattered in different files and formats β hard to bring together
ScatteredDifficult Access
A new question (like "students with fees due") needs a NEW program to be written
No easy searchPoor Sharing
It's hard for many people to use the same file at the same time
No sharingWeak Security
Anyone who opens the file can see or change everything
Not safeRedundancy, Inconsistency, Data isolation, Difficult access, Security and sharing problems β the file system gives you RIDDS (riddles) to solve!
"What is data redundancy? How does it lead to data inconsistency?" β 2 marks Answer: Data redundancy means the same data is stored in more than one place. When the data is changed in one place but not in the others, the copies no longer match β this is data inconsistency. For example, if a student's address is updated only in the office file and not in the library file, the two files show different addresses.
8.2 ποΈ Database and DBMS
A database is an organised collection of related data, stored so that it can be easily accessed, managed and updated.
A DBMS (Database Management System) is the software that lets us create, store, update, search and protect the data in a database.
graph LR
U1["π§βπΌ Office"]
U2["π Library"]
U3["π Exam Cell"]
DBMS["βοΈ DBMS\n(MySQL, Oracleβ¦)"]
DB[("ποΈ ONE shared\nDatabase")]
U1 --> DBMS
U2 --> DBMS
U3 --> DBMS
DBMS <--> DB
style DBMS fill:#FF9800,color:#fff
style DB fill:#2196F3,color:#fff
Now everyone uses the same single copy of the data β so there is no duplication and no mismatch!
Analogy: A database is like a big library full of books (data). π The DBMS is the librarian π©βπΌ β she keeps the books in order, finds any book you ask for in seconds, lets many people borrow books, and makes sure no one takes a book without permission!
Popular DBMS software:
MySQL
Free and open source β used in this course and by many websites
Open sourceOracle
Used by big companies and banks
CommercialMicrosoft SQL Server
Popular in offices using Microsoft products
CommercialPostgreSQL
Free and powerful
Open sourceSQLite
Tiny DBMS built into mobile apps and browsers
Lightweight8.2.1 Advantages of a DBMS β
| Advantage | Meaning |
|---|---|
| Less redundancy | Data is stored once in one place, so duplication is reduced |
| Consistency | With one copy, there is no mismatch between copies |
| Easy sharing | Many users and programs can use the data at the same time |
| Security | Passwords and permissions decide who can see or change what |
| Integrity | Rules (constraints) keep wrong data out β e.g. marks can't be negative |
| Easy access | Any question can be answered using simple queries (SQL) |
| Backup and recovery | Data can be backed up and restored after a failure |
"Write any three advantages of a DBMS over a file system." β 3 marks Answer: (1) Reduced redundancy β data is stored only once, avoiding duplication. (2) Data consistency β since there is a single copy, all users see the same correct data. (3) Data security β access to data is controlled using passwords and permissions. (Data sharing, integrity and backup are also correct.)
8.2.2 Where are Databases Used? π
Banking
Accounts, deposits, loans and transactions
Bank recordsRailway / Airline
Seat booking, PNR status and schedules
ReservationsSchool
Admissions, fees, attendance and results
Student recordsOnline Shopping
Products, orders, payments and deliveries
E-commerceHospital
Patient details, doctors and medicines
Health records8.3 π The Relational Data Model
The most popular way to organise a database is the relational data model. In it, data is stored in tables, and a table is called a relation.
Here is a relation named STUDENT:
βββββ Attributes (columns) βββββ
βΌ βΌ βΌ βΌ
βββββββββββ¬βββββββββββββ¬βββββββββ¬βββββββββ
Relation name β β RollNo β Name β Class β Marks β β heading
STUDENT βββββββββββΌβββββββββββββΌβββββββββΌβββββββββ€
β 101 β Riya β 11 β 92 β βββ
β 102 β Aman β 11 β 85 β βββ€ Tuples
β 103 β Zoya β 12 β 97 β βββ€ (rows)
β 104 β Kabir β 12 β 78 β βββ
βββββββββββ΄βββββββββββββ΄βββββββββ΄βββββββββ
Degree = number of attributes (columns) = 4
Cardinality = number of tuples (rows) = 4
8.3.1 Important Terms π
| Term | Meaning | In the STUDENT table |
|---|---|---|
| Relation | A table with rows and columns | STUDENT |
| Attribute | A column β one property of the data | RollNo, Name, Class, Marks |
| Tuple | A row β one complete record | 102, Aman, 11, 85 |
| Domain | The set of all allowed values for an attribute | Class β {11, 12}; Marks β 0 to 100 |
| Degree | The number of attributes (columns) | 4 |
| Cardinality | The number of tuples (rows) | 4 |
Degree = columns (count across the top). Cardinality = count the rows (going down). Or remember: "C for Count of rows".
Analogy: A relation is like your class attendance register π β the column headings (Roll No, Name, Date) are the attributes, and each student's line is a tuple. The domain of the "Present?" column is just {P, A}!
Many students swap them. Degree = columns, Cardinality = rows. Adding a new student changes the cardinality; adding a new column like "Phone" changes the degree.
8.3.2 Properties of a Relation π
Unique Column Names
No two attributes in a table can have the same name
Unique namesNo Duplicate Rows
Two tuples can never be exactly the same
Each row uniqueOrder Doesn't Matter
Rows and columns can be in any order β the data means the same
Any orderAtomic Values
Each cell holds ONE single value β not a list
One value per cellNULL for Unknown
A missing or unknown value is stored as NULL
NULL"A table ITEM has 5 columns and 20 rows. 3 more rows are added and 1 column is deleted. What are its degree and cardinality now?" β 2 marks Answer: Degree = 5 β 1 = 4. Cardinality = 20 + 3 = 23.
8.4 π Keys in a Relation
A key is an attribute (or a group of attributes) used to identify a tuple uniquely or to link two tables.
Let's use this table:
EMPLOYEE
βββββββββ¬ββββββββββ¬βββββββββββββββ¬ββββββββββββββββββββββ¬βββββββββ
β EmpID β Name β AadhaarNo β Email β DeptNo β
βββββββββΌββββββββββΌβββββββββββββββΌββββββββββββββββββββββΌβββββββββ€
β E01 β Ravi β 2345 6789 01 β ravi@mail.com β D1 β
β E02 β Sneha β 3456 7890 12 β sneha@mail.com β D2 β
β E03 β Ravi β 4567 8901 23 β ravi.k@mail.com β D1 β
βββββββββ΄ββββββββββ΄βββββββββββββββ΄ββββββββββββββββββββββ΄βββββββββ
(Name can't identify an employee β there are two Ravis! π)
graph TD
CK["ποΈ Candidate Keys\n(all columns that can identify\na row uniquely)\nEmpID, AadhaarNo, Email"]
PK["π Primary Key\n(the ONE chosen)\nEmpID"]
AK["ποΈ Alternate Keys\n(candidates NOT chosen)\nAadhaarNo, Email"]
CK --> PK
CK --> AK
style CK fill:#9C27B0,color:#fff
style PK fill:#4CAF50,color:#fff
style AK fill:#FF9800,color:#fff
8.4.1 Candidate Key ποΈ
A candidate key is any attribute (or set of attributes) that can uniquely identify every tuple. It has no duplicate values and no NULL values. In EMPLOYEE: EmpID, AadhaarNo and Email are candidate keys.
8.4.2 Primary Key π
The primary key is the one candidate key chosen by the database designer to identify tuples. It must be unique and NOT NULL. A table has only one primary key. In EMPLOYEE: EmpID is chosen as the primary key.
8.4.3 Alternate Key π
The candidate keys that are not chosen as the primary key are called alternate keys. In EMPLOYEE: AadhaarNo and Email are alternate keys.
Candidate keys = Primary key + Alternate keys So, Alternate keys = Candidate keys β Primary key.
Analogy: A class wants to elect a monitor π§βπ. All students who are eligible are the candidates (candidate keys). The one who wins becomes the monitor (primary key). Those who lost are the alternates (alternate keys) β still good, just not chosen!
8.4.4 Foreign Key π
A foreign key is an attribute in one table that refers to the primary key of another table. It is used to link (relate) two tables.
EMPLOYEE (child table) DEPARTMENT (parent table)
βββββββββ¬ββββββββ¬βββββββββ ββββββββββ¬ββββββββββββββ
β EmpID β Name β DeptNo β βββββ refers to βββΊβ DeptNo β DeptName β
βββββββββΌββββββββΌβββββββββ€ ββββββββββΌββββββββββββββ€
β E01 β Ravi β D1 β β D1 β Sales β
β E02 β Sneha β D2 β β D2 β Accounts β
β E03 β Ravi β D1 β β D3 β HR β
βββββββββ΄ββββββββ΄βββββββββ ββββββββββ΄ββββββββββββββ
DeptNo = FOREIGN KEY DeptNo = PRIMARY KEY
DeptNois the primary key of DEPARTMENT.DeptNoin EMPLOYEE is a foreign key β it can only have values that exist in DEPARTMENT (D1, D2, D3), or NULL.- A foreign key can repeat (two employees can be in D1).
Many employees can belong to the same department, so D1 can appear many times in EMPLOYEE. Only the primary key it refers to (in DEPARTMENT) must be unique.
A foreign key is a "foreigner" π§³ β it lives in one table but actually belongs to (comes from) the primary key of another table!
| Key | Unique? | NULL allowed? | How many per table? |
|---|---|---|---|
| Candidate key | Yes | No | One or more |
| Primary key | Yes | No | Exactly one |
| Alternate key | Yes | No | Zero or more |
| Foreign key | Not needed | Yes | Zero or more |
"In the table STUDENT(AdmNo, RollNo, Name, Phone), AdmNo and Phone are unique for every student, and AdmNo is used to identify students. Identify the candidate, primary and alternate keys." β 3 marks Answer: Candidate keys: AdmNo, Phone. Primary key: AdmNo. Alternate key: Phone. (RollNo is not a candidate key if two students in different classes can have the same roll number.)
8.5 π§Ύ A Complete Example
Let's put everything together with two related tables.
BOOK MEMBER
ββββββββββ¬βββββββββββββββββββββ¬βββββββββββ ββββββββββββ¬βββββββββ¬ββββββββββββββ
β BookID β Title β MemberID β β MemberID β Name β Phone β
ββββββββββΌβββββββββββββββββββββΌβββββββββββ€ ββββββββββββΌβββββββββΌββββββββββββββ€
β B1 β Wings of Fire β M2 β β M1 β Asha β 98300 11111 β
β B2 β Python Basics β NULL β β M2 β Rohit β 98300 22222 β
β B3 β Malgudi Days β M2 β ββββββββββββ΄βββββββββ΄ββββββββββββββ
ββββββββββ΄βββββββββββββββββββββ΄βββββββββββ
| Question | Answer |
|---|---|
| Degree of BOOK | 3 |
| Cardinality of BOOK | 3 |
| Degree and cardinality of MEMBER | 3 and 2 |
| Primary key of BOOK | BookID |
| Candidate keys of MEMBER | MemberID, Phone |
| Primary and alternate key of MEMBER | MemberID (primary), Phone (alternate) |
| Foreign key | MemberID in BOOK β refers to MEMBER |
| Why is NULL allowed in BOOK.MemberID? | Book B2 is not issued to anyone yet |
β οΈ Common Errors and Misconceptions
| Mistake | What's Wrong | Correct Understanding |
|---|---|---|
| "Database and DBMS are the same" | One is data, the other is software | Database = the data; DBMS = software that manages it |
| "Degree = number of rows" | Swapped | Degree = columns, Cardinality = rows |
| "Redundancy and inconsistency are the same" | One causes the other | Redundancy (duplicates) leads to inconsistency (mismatch) |
| "A table can have two primary keys" | Only one is chosen | Exactly one primary key (it may have several columns) |
| "Primary key can be NULL if unknown" | Must always have a value | Primary key is unique and NOT NULL |
| "Foreign key must be unique" | It can repeat | Foreign key can have duplicates and NULLs |
| "Name is a good primary key" | Two people can share a name | Use something unique, like an ID |
| "Alternate key is a backup copy of the table" | It's a key, not a copy | Alternate key = candidate key not chosen as primary |
| "Domain is the table name" | Domain is about allowed values | Domain = set of permitted values of an attribute |
| "Adding a row changes the degree" | Rows change cardinality | New row β cardinality changes; new column β degree changes |
π Quick Revision β Exam Ready!
- File system problems β Redundancy, Inconsistency, Data isolation, Difficult access, Security and sharing issues (RIDDS)
- Database β organised collection of related data
- DBMS β software to create, manage, share and protect a database (MySQL, Oracle, SQL Server, PostgreSQL, SQLite)
- DBMS advantages β less redundancy, consistency, sharing, security, integrity, easy access, backup
- Relational model β data stored in tables called relations
- Attribute = column; Tuple = row; Domain = set of allowed values
- Degree = number of columns; Cardinality = number of rows
- Relation properties β unique column names, no duplicate rows, order doesn't matter, atomic values, NULL for unknown
- Candidate key β every attribute set that can uniquely identify a row (unique, not NULL)
- Primary key β the ONE candidate key chosen (unique, not NULL, one per table)
- Alternate key β candidate keys not chosen as primary
- Candidate = Primary + Alternate
- Foreign key β attribute that refers to the primary key of another table; links tables; can repeat and can be NULL
π― Sample Exam Questions
Q1: Very Short Answer [1 mark each]
a) What is a row of a relation called? Answer: Tuple
b) What is the number of columns in a relation called? Answer: Degree
c) Name any two DBMS software. Answer: MySQL and Oracle
d) What is the set of permitted values of an attribute called? Answer: Domain
e) Which key is used to link two tables? Answer: Foreign key
Q2: Short Answer [2 marks]
Differentiate between a database and a DBMS.
Answer: A database is an organised collection of related data, such as the records of all students of a school. A DBMS is the software used to create, store, update, search and protect that data β e.g. MySQL.
Q3: Short Answer [2 marks]
Define degree and cardinality. Find them for a table with columns (Code, Item, Price, Qty) containing 12 records.
Answer: Degree is the number of attributes (columns) of a relation, and cardinality is the number of tuples (rows). Here, Degree = 4 and Cardinality = 12.
Q4: Short Answer [3 marks]
Consider the table CUSTOMER:
ββββββββββ¬βββββββββ¬βββββββββββββββββββ¬ββββββββββββββ
β CustID β Name β Email β City β
ββββββββββΌβββββββββΌβββββββββββββββββββΌββββββββββββββ€
β C1 β Meena β meena@mail.com β Pune β
β C2 β Arjun β arjun@mail.com β Delhi β
β C3 β Meena β m.j@mail.com β Pune β
ββββββββββ΄βββββββββ΄βββββββββββββββββββ΄ββββββββββββββ
(a) Identify the candidate keys. (b) If CustID is the primary key, which is the alternate key? (c) Why can't Name be a key?
Answer: (a) Candidate keys: CustID and Email (both are unique for every row). (b) Alternate key: Email. (c) Name has duplicate values (two customers named Meena), so it cannot identify a row uniquely.
Q5: Short Answer [3 marks]
What is a foreign key? Explain with an example of two tables.
Answer: A foreign key is an attribute of one table that refers to the primary key of another table; it is used to link the two tables. For example, in STUDENT(RollNo, Name, ClassID) and CLASS(ClassID, ClassTeacher), ClassID is the primary key of CLASS and a foreign key in STUDENT. Every ClassID in STUDENT must exist in CLASS (or be NULL), and it can repeat, since many students belong to the same class.
βοΈ Practice Problems
- List any four limitations of keeping data in ordinary files.
- What is data inconsistency? Give an example from your school.
- Explain the role of a DBMS using the "librarian" analogy.
- Draw a table BOOK with 4 attributes and 5 tuples. Write its degree and cardinality.
- A relation has degree 6 and cardinality 30. Two columns are added and 5 rows are deleted. What are the new degree and cardinality?
- What is a domain? Give the domain of the attributes
Gender,Class(for a senior secondary school) andMarks(out of 100). - Why must a primary key be unique and NOT NULL?
- In PATIENT(PatientID, Name, Phone, BedNo, DoctorID), identify a suitable primary key, the alternate keys, and a possible foreign key.
- Can a table have more than one candidate key? More than one primary key? Explain.
- Differentiate between a primary key and a foreign key (any three points).
- Why is "Name" usually a poor choice for a primary key?
- Give two examples each of how databases are used in a bank and in a hospital.