πŸ“š Complete Notes on Relational Model (DBMS)

πŸ”Ά 1. What is the Relational Model?

Definition

The Relational Model is a way to store and manage data in tabular form. It was introduced by E.F. Codd in 1970.

Relational Model Structure

πŸ“Œ Real-World Analogy: College Record System

Roll_No Name(only allowed String this is one type of domain) Age(only allowed Integer this is one type of domain) Course_ID
101 Ravi 20 C101
102 Meena 21 C102
Table Structure in DBMS

πŸ”Ά 2. Keys (Uniqueness & Linking)

Types of Keys in DBMS

βœ… Primary Key

Uniquely identifies each row in a table.

⚠️ Cannot be NULL or duplicate.

Example: Roll_No in Student table

βœ… Candidate Key

All possible unique keys.

Example: Roll_No, Email

βœ… Alternate Key

Candidate keys that are NOT chosen as Primary Key.

Example: Email (when Roll_No is Primary)

βœ… Super Key

Any set of attributes that uniquely identifies rows.

Example: {Roll_No, Name}

βœ… Foreign Key

Links to a Primary Key from another table.

Example: Student.Course_ID β†’ Course.ID

βœ… Composite Key

Combination of 2+ columns used as a Primary Key.

Example: {Student_ID, Subject_ID} in Marks table

Foreign Key Relationship

πŸ” Table Relationships

Relational Model Example

πŸ”„ Referential Integrity: INSERT, ON DELETE, and ON UPDATE

🎯 Purpose:

🧩 Example Tables:


CREATE TABLE Course (
    Course_ID VARCHAR(10) PRIMARY KEY
);

CREATE TABLE Student (
    Student_ID INT PRIMARY KEY,
    Course_ID VARCHAR(10),
    FOREIGN KEY (Course_ID) REFERENCES Course(Course_ID)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);
    

βž• ON INSERT Rules

πŸ› οΈ ON DELETE Actions

✏️ ON UPDATE Actions

βœ… Summary Table:

Action Effect on Child
ON INSERT (invalid FK) Insert fails if referenced key is missing in parent
ON DELETE CASCADE Deletes matching child rows
ON DELETE SET NULL Sets foreign key to NULL
ON DELETE NO ACTION Prevents deletion if child exists
ON UPDATE CASCADE Updates foreign key in child
ON UPDATE NO ACTION Prevents update if child exists

πŸ“Œ Quick Summary (Easy Revision)

πŸ”Ά 3. Constraints (Rules for Clean Data)

Database Constraints Database Constraints
Constraint Type Description Example
Domain Limits value type Age should be 18–25
Entity Integrity Primary Key can't be NULL Roll_No always required
Referential Integrity Foreign Key must match valid Primary Key Course_ID must exist in Course table
Unique No duplicates allowed Email
Not Null Must have value Name, Age

πŸ“Œ Tip to Remember Constraints:

πŸ’‘ D-E-R = Domain, Entity, Referential

πŸ”Ά 4. Relational Algebra (Operations on Tables)

It's a mathematical way to express queries on tables.

Relational Algebra Operations

πŸ”Ή Select (Οƒ): Filter Rows

Οƒ_Age>21(Student)

Selects all students with age greater than 21

πŸ”Ή Project (Ο€): Select Columns

Ο€_Name, Age(Student)

Shows only Name and Age columns

πŸ”Ή Union (βˆͺ): Combine Rows

Student βˆͺ Alumni

βœ… Both tables must have same attributes

πŸ”Ή Set Difference (βˆ’): Subtract

Student βˆ’ Alumni

Students who are not alumni

πŸ”Ή Cartesian Product (Γ—): Cross Join

Student Γ— Course

Every student paired with every course

πŸ”Ή Join (⨝): Match Rows

Student ⨝ Student.Course_ID = Course.ID

Combines related records from two tables

Relational Algebra Examples

πŸ’‘ Trick to Remember Operations:

πŸ“Œ SPU-JCD = Select, Project, Union, Join, Cartesian, Division

πŸ”Ά 5. Extended Example (Student + Course)

Student Table

Roll_No Name Age Course_ID
101 Ravi 20 C101
102 Meena 21 C102

Course Table

Course_ID Course_Name
C101 DBMS
C102 OS

Query: Show names of students who have DBMS course

Ο€_Name(Οƒ_Course_Name='DBMS'(Student ⨝ Course))
Join Operation Example

πŸ”Ά 7. Interview Tips βœ…

Database Interview Tips
Tip No. Tip
1 Always explain with Student/Course/Employee example
2 Emphasize why keys are important: uniqueness and relation
3 Prepare 2–3 relational algebra queries
4 Be able to explain JOIN vs PRODUCT
5 Practice how constraints keep data clean
6 Draw mini tables during interviews to explain answers

πŸ”Ά 8. Common Interview Questions & Solutions

Q1: Difference between Primary Key and Unique Key?

Answer:

Q2: Write query to get names of students aged 18–21

Ο€_Name(Οƒ_Age>=18 AND Age<=21(Student))< /div>

Q3: Can Foreign Key be NULL?

Answer: Yes, it can be NULL if the relation is optional.

Q4: What is the result of Cartesian Product of 2 tables of 3 and 4 rows?

Answer: 3 Γ— 4 = 12 rows

πŸ”Ά 9. Cheat Sheet Summary ✨

Topic Shortcut
Table Relation
Row Tuple
Column Attribute
Unique column Primary Key
Rule on column Constraint
Filter rows Select (Οƒ)
Select columns Project (Ο€)
Combine tables Join (⨝)
Merge rows Union (βˆͺ)
Cross-pairing Cartesian (Γ—)

πŸ”Ά 10. Mnemonics & Memory Aids

Memory Techniques

Keys: PCSF

Primary
Candidate
Super
Foreign

Constraints: DER

Domain
Entity
Referential

Operations: SPU-JCD

Select, Project, Union
Join, Cartesian, Division

🎯 Quick Reference Card

Essential Formula

Relation = Table Tuple = Row Attribute = Column Domain = Allowed Values

Remember: The 3 Golden Rules

  1. Every table must have a Primary Key
  2. Foreign Keys link tables together
  3. Constraints keep data clean and consistent