Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🗄️ Unit 3 – Database & SQL  ·  Lesson 20

DBMS, Relational Model & Keys

File system and its problems

Earlier, data was kept in separate files – one for fees, one for library, one for exam. This caused:

📑
Data redundancySame student details stored in many files.
⚠️
InconsistencyAddress changed in one file but not in another.
🔍
Difficult accessA new program is needed for every new question.
🔓
Poor securityHard to control who sees what.
🔗
Data dependenceChanging file format breaks programs.
Limitations of a file system

What is a DBMS?

A database is an organised collection of related data. A DBMS (Database Management System) is software to create, store, update and query it – e.g. MySQL, Oracle, PostgreSQL, SQLite. It reduces redundancy, keeps data consistent, allows sharing and provides security and backup.

Users / appsDBMS (MySQL)Database files
The DBMS sits between users and data

Relational model terms

In the relational model (E. F. Codd, 1970) data is stored in tables called relations.

Relation EMP
EmpIDNameAadhaarSalaryDeptID
1Riya111142000D01
2Arjun222256000D03
3Meena333338000D02
4Kunal444461000D03
Highlighted row = one tuple · highlighted column = one attribute
TermMeaningIn EMP
RelationA tableEMP
AttributeA columnName, Salary…
TupleA row (record)(2, Arjun, 2222, 56000, D03)
DomainSet of allowed values for an attributeDeptID ∈ {D01, D02, D03}
DegreeNumber of attributes5
CardinalityNumber of tuples4

Properties of a relation

  • Each attribute has a unique name; order of columns does not matter.
  • No two tuples are identical; order of rows does not matter.
  • Each cell holds a single (atomic) value.
  • Values of an attribute come from the same domain; a missing value is NULL.

Keys in a relation

🗝️
Candidate keyAttribute(s) that can uniquely identify each tuple. EMP: EmpID, Aadhaar.
🔑
Primary keyThe candidate key chosen to identify tuples; unique and NOT NULL. EmpID.
🔁
Alternate keyCandidate keys not chosen as primary. Aadhaar.
🔗
Foreign keyAttribute that refers to the primary key of another table. EMP.DeptID.
🧩
Composite keyA primary key made of two or more attributes, e.g. (RollNo, Subject).
Types of keys
DEPT🔑 DeptID CHAR(3)DName VARCHAR(20) UNIQUECity VARCHAR(20)EMP🔑 EmpID INTName VARCHAR(25)Aadhaar CHAR(4) UNIQUESalary INT🔗 DeptID CHAR(3)
EMP.DeptID refers to DEPT.DeptID
SQL
SELECT * FROM DEPT;
Output
+--------+----------+--------+
| DeptID | DName    | City   |
+--------+----------+--------+
| D01    | Sales    | Delhi  |
| D02    | Accounts | Mumbai |
| D03    | IT       | Pune   |
+--------+----------+--------+
SQL
SELECT * FROM EMP;
Output
+-------+-------+---------+--------+--------+
| EmpID | Name  | Aadhaar | Salary | DeptID |
+-------+-------+---------+--------+--------+
|     1 | Riya  | 1111    |  42000 | D01    |
|     2 | Arjun | 2222    |  56000 | D03    |
|     3 | Meena | 3333    |  38000 | D02    |
|     4 | Kunal | 4444    |  61000 | D03    |
+-------+-------+---------+--------+--------+

Foreign key in action

MySQL enforces the keys. A duplicate primary key or a DeptID that does not exist in DEPT is rejected; this keeps data correct (referential integrity).

SQL
INSERT INTO EMP VALUES (2, 'Tara', '5555', 40000, 'D01');
-- ERROR 1062 (23000): Duplicate entry '2' for key 'PRIMARY'
SQL
INSERT INTO EMP VALUES (5, 'Tara', '5555', 40000, 'D09');
-- ERROR 1452 (23000): Cannot add or update a child row:
--   a foreign key constraint fails (DeptID 'D09' does not exist in DEPT)
SQL
INSERT INTO EMP VALUES (5, 'Tara', '5555', 40000, 'D01');
-- Query OK, 1 row affected
SQL
SELECT EmpID, Name, DeptID FROM EMP WHERE DeptID = 'D03';
Output
+-------+-------+--------+
| EmpID | Name  | DeptID |
+-------+-------+--------+
|     2 | Arjun | D03    |
|     4 | Kunal | D03    |
+-------+-------+--------+

Practice

  1. A table has 6 columns and 20 rows. Degree = 6, cardinality = 20. After adding 2 rows and 1 column? Degree 7, cardinality 22.
  2. STUDENT(AdmNo, RollNo, Class, Name, Email): identify candidate, primary and alternate keys. Candidate: AdmNo, Email (and RollNo+Class); primary: AdmNo; alternate: Email.

Frequently Asked Questions

What is the difference between degree and cardinality?
Degree is the number of attributes (columns); cardinality is the number of tuples (rows) in a relation.
What is an alternate key?
A candidate key that is not chosen as the primary key. In EMP, if EmpID is the primary key, Aadhaar is an alternate key.
Can a foreign key have NULL or repeated values?
Yes. A foreign key may repeat and may be NULL, but any non-NULL value must exist in the referenced primary key.
← Class 12 Computer Science
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicSQL: DDL, DML & SELECT Your progress: 20 / 26 (77%) · Lesson 21 / 26 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free