UNIT 2
1. Tuple & Attributes
Tuple: A single row in a table representing a record.
Attribute: A column in a table representing a property of an entity.
Example: Student(ID, Name, Age) → One student row is a tuple, Name is an attribute.
2. Primary Key
Unique identifier for each tuple in a table.
Example: StudentID in Student table.
3. Relational Model
Represents data as tables (relations) with rows (tuples) and columns (attributes).
Uses keys and constraints to maintain integrity.
4. Functional Dependency
Relationship where attribute B is determined by attribute A (A → B).
Example: StudentID → Name
5. Mapping Cardinality
Defines number of entity instances in a relationship.
Types: 1:1, 1:N, M:N
6. Normalization & Its Need
Process of organizing tables to reduce redundancy and avoid anomalies.
Need: Prevents insertion, update, deletion anomalies; ensures data integrity.
7. Join in SQL
Combines data from two or more tables based on a common attribute.
Example:
SELECT [Link], [Link]
FROM Student
JOIN Enrollment ON [Link] = [Link]
JOIN Course ON [Link] = [Link];
. Types of File Organization
Heap/Unordered: Records stored randomly.
Sequential/Ordered: Records stored by key order.
Hashed: Records stored using a hash function.
2. Foreign Key & Referential Integrity
Foreign Key: Attribute in one table referring to primary key of another table.
Referential Integrity: Ensures foreign key value exists in referenced table.
3. Sequential Access
Records are accessed one after another in order.
Example: Reading a file from start to end.
4. Clustered & Non-Clustered Indexes
Clustered: Table records stored in same order as index. Only one per table.
Non-Clustered: Separate structure pointing to records. Multiple indexes allowed.
5. Indexing & Its Purpose
Creates pointers to data to speed up search queries.
6. Difference between Dense & Sparse Index
Feature Dense Index Sparse Index
Entry Every record Only some records
Space More space Less space
Search Faster Slightly slower
7. SQL View
Virtual table created using SELECT query.
Stores query result, not actual data.
Example:
CREATE VIEW StudentView AS
SELECT Name, Age FROM Student;
8. Static & Dynamic Hashing
Static Hashing: Fixed number of buckets; may overflow.
Dynamic Hashing: Buckets grow/shrink dynamically; avoids overflow.
Difference between Primary Key & Foreign Key
Feature Primary Key Foreign Key
Purpose Uniquely identifies a tuple Refers to primary key in another table
Uniqueness Must be unique Can have duplicates
Null Allowed Not allowed Can be null
Table Relationship Exists in its own table Links two tables
2. Hashing & Its Purpose
Hashing: Technique of converting a key into a hash value (address) to store or retrieve records.
Purpose: Fast direct access to data without scanning entire file.
Example: EmployeeID → Hash(EmployeeID) → Bucket location
PART B
1. B-Tree Indexing
Definition:
A B-Tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential
access, insertions, and deletions in logarithmic time.
Characteristics:
1. All leaves are at the same level.
2. Each node can contain multiple keys (not just one).
3. Keys in each node are sorted in ascending order.
4. Internal nodes store keys and child pointers; leaf nodes may store data.
5. The tree remains balanced after insertions and deletions.
Operations:
Search: Follow the appropriate child pointer based on key comparison.
Insertion: Insert key in the correct leaf; split node if full.
Deletion: Remove key; merge or redistribute nodes to maintain balance.
Advantages:
Efficient search, insert, delete in O(log n) time.
Maintains data sorted, useful for databases.
2. B+ Tree Indexing
Definition:
A B+ Tree is an extension of B-Tree where:
Only leaf nodes store actual data.
Internal nodes store only keys for navigation.
Leaf nodes are linked sequentially, forming a linked list for efficient range queries.
Characteristics:
1. All data is at leaf level.
2. Internal nodes guide the search but do not contain data.
3. Leaf nodes are doubly or singly linked, allowing fast range retrieval.
4. Like B-Tree, it is balanced, keeping operations O(log n).
Advantages over B-Tree:
Faster range queries because leaf nodes are linked sequentially.
Less storage in internal nodes.
Easier to implement database indexing.
Key Differences:
Feature B-Tree B+ Tree
Data storage Internal & leaf nodes Only leaf nodes
Leaf linkage Not linked Linked sequentially
Range queries Slower Faster
Internal node size Larger (stores keys + data) Smaller (stores only keys)
Database use Less common Widely used for indexing
1. Data Definition Language (DDL)
Used to define or modify database structure.
Command Purpose Example
CREATE Create table, database, view, etc. CREATE TABLE Student(ID INT, Name VARCHAR(50));
ALTER Modify structure of existing table ALTER TABLE Student ADD Age INT;
DROP Delete table, database, or view DROP TABLE Student;
TRUNCATE Remove all records from a table TRUNCATE TABLE Student;
2. Data Manipulation Language (DML)
Used to manipulate data in tables.
Command Purpose Example
SELECT Retrieve data SELECT Name FROM Student;
INSERT Insert new record INSERT INTO Student VALUES(1, 'Alice', 20);
UPDATE Modify existing record UPDATE Student SET Age=21 WHERE ID=1;
DELETE Remove record(s) DELETE FROM Student WHERE ID=1;
3. Data Control Language (DCL)
Used to control access to data.
Command Purpose Example
GRANT Give user privileges GRANT SELECT ON Student TO User1;
REVOKE Remove user privileges REVOKE SELECT ON Student FROM User1;
4. Transaction Control Language (TCL)
Used to manage transactions.
Command Purpose Example
COMMIT Save all changes COMMIT;
ROLLBACK Undo changes since last commit ROLLBACK;
SAVEPOINT Set a point to rollback to SAVEPOINT sp1;
5. Data Querying Concepts
JOINs: Combine rows from multiple tables.
o INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN
GROUP BY / HAVING: Aggregate data
ORDER BY: Sort results
Advanced SQL Features, suitable for exam or practical reference:
1. Joins (Complex Queries)
Combine data from multiple tables.
Types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, SELF JOIN, CROSS JOIN.
Example:
SELECT [Link], [Link]
FROM Student S
INNER JOIN Enrollment E ON [Link] = [Link]
INNER JOIN Course C ON [Link] = [Link];
2. Subqueries (Nested Queries)
A query inside another query.
Types: Single-row, Multiple-row, Correlated Subquery.
Example:
SELECT Name FROM Student
WHERE ID IN (SELECT StudentID FROM Enrollment WHERE CourseID=101);
3. Views
Virtual tables created using a SELECT query.
Simplifies queries and hides complexity.
Example:
CREATE VIEW StudentView AS
SELECT Name, Age FROM Student;
4. Indexing
Speeds up data retrieval.
Types: Clustered, Non-Clustered, Unique.
Example:
CREATE INDEX idx_name ON Student(Name);
5. Stored Procedures
Predefined SQL code blocks executed as needed.
Can accept parameters and return results.
Example:
CREATE PROCEDURE GetStudent(IN sid INT)
BEGIN
SELECT * FROM Student WHERE ID = sid;
END;
6. Triggers
Automatic actions executed when a table inserts, updates, or deletes.
Example:
CREATE TRIGGER trg_student BEFORE INSERT ON Student
FOR EACH ROW
SET NEW.created_at = NOW();
7. Transactions
Ensure ACID properties (Atomicity, Consistency, Isolation, Durability).
Commands: BEGIN TRANSACTION, COMMIT, ROLLBACK.
8. Advanced Functions
Aggregate Functions: SUM, COUNT, AVG, MAX, MIN
Analytical Functions: ROW_NUMBER(), RANK(), LEAD(), LAG()
String & Date Functions: CONCAT(), SUBSTRING(), NOW(), DATEDIFF()
9. CTE (Common Table Expressions) / WITH Clause
Temporary result sets used within a query.
Example:
WITH TopStudents AS (
SELECT Name, AVG(Marks) AS AvgMarks
FROM StudentMarks
GROUP BY Name
HAVING AVG(Marks) > 80
)
SELECT * FROM TopStudents;
process for converting an ER model into a relational model, explained clearly for exams or assignments:
Step 1: Map Strong Entities
Create a table for each strong entity.
Include all simple attributes.
Choose the primary key.
Example:
Student(StudentID PK, Name, Age)
Course(CourseID PK, CourseName)
Step 2: Map Weak Entities
Create a table for the weak entity.
Include the partial key + primary key of owner entity as foreign key.
The combination of these becomes the primary key.
Example:
Dependent(DepName PK_part, EmpID FK, Age)
Step 3: Map Relationships
1:1 relationship: Add foreign key in either entity table.
1:N relationship: Add foreign key in the table on the “many” side.
M:N relationship: Create a new table with foreign keys of both entities.
Example (M:N):
Enrollment(StudentID FK, CourseID FK, Marks)
Step 4: Map Attributes
Single-valued: Add as column in table.
Multi-valued: Create a separate table with foreign key referencing the owner entity.
Derived: Usually not stored; can be computed.
Example (Multi-valued):
StudentPhone(StudentID FK, PhoneNumber)
Step 5: Map Specialization / Generalization
Option 1: Single Table Inheritance – One table for superclass + all subclass attributes.
Option 2: Class Table Inheritance – Separate tables for superclass and subclasses.
Example (Class Table):
Employee(EmpID PK, Name, Salary)
Manager(EmpID PK FK, Dept)
Engineer(EmpID PK FK, Skill)
Step 6: Map Aggregation (if any)
Treat the relationship being aggregated as a table.
Include primary keys of participating entities.
Use this table in higher-level relationships.
Example:
Works_on(EmpID FK, ProID FK, Hours)
Reviewed_by(EmpID FK, ProID FK, ManagerID FK)
✅Summary
1. Strong entities → Tables
2. Weak entities → Tables with owner keys
3. Relationships → Foreign keys or separate tables
4. Attributes → Map carefully (multi-valued & derived)
5. Specialization → Subclass tables or combined table
6. Aggregation → Relationship as table