0% found this document useful (0 votes)
91 views17 pages

Advanced DBMS Concepts and SQL Guide

The document covers advanced topics in Database Management Systems (DBMS), including relational algebra, domain and tuple relational calculus, SQL fundamentals, integrity constraints, triggers, and views. It provides theoretical foundations, practical examples, and operations for querying and manipulating relational data, along with practice questions and solutions. Key concepts such as normalization, query optimization, and transaction management are also discussed.

Uploaded by

Satyajeet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views17 pages

Advanced DBMS Concepts and SQL Guide

The document covers advanced topics in Database Management Systems (DBMS), including relational algebra, domain and tuple relational calculus, SQL fundamentals, integrity constraints, triggers, and views. It provides theoretical foundations, practical examples, and operations for querying and manipulating relational data, along with practice questions and solutions. Key concepts such as normalization, query optimization, and transaction management are also discussed.

Uploaded by

Satyajeet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DBMS Notes – Advanced Topics

(Relational Algebra → Calculus → SQL


→ Integrity & More)
Prepared: Amity University [Link] Level
Index
1. 1. Relational Algebra (Theory & Examples)
2. 2. Domain Relational Calculus (DRC)
3. 3. Tuple Relational Calculus (TRC)
4. 4. Fundamental Operations
5. 5. Additional Operations
6. 6. SQL Fundamentals (Examples & Queries)
7. 7. Integrity Constraints
8. 8. Triggers
9. 9. Views
10.10. Relational Database Concepts & Summary
11.11. Practice Questions (20)
12.12. Solutions
1. Relational Algebra
Relational algebra is a procedural query language that works on relations
(tables). It consists of a set of operations that take one or two relations as
input and produce a new relation as output. It forms the theoretical
foundation for SQL and query optimization.

Common operators: Selection (σ), Projection (π), Union (∪), Set Difference
(−), Cartesian Product (×), Rename (ρ), Join (⋈), Intersection (∩), Division
(÷).

Example relation EMP:

EmpID Name Dept Salary


1 Aman CSE 50000
2 Riya ECE 48000
3 Sameer CSE 52000
Selection example: σ_Dept='CSE'(EMP) returns tuples where Dept is CSE.

Projection example: π_Name,Salary(EMP) returns only the Name and Salary


columns. These operators can be composed to express complex queries.

Join example: Combining EMP and DEPT relations using common attribute
DeptID (natural join or equi-join).
2. Domain Relational Calculus (DRC)
DRC is a non-procedural query language that specifies what to retrieve rather
than how. Queries are expressions that use domain variables which take
values from domains of attributes.

Syntax: { <d1, d2, ... , dn> | P(d1, d2, ..., dn) } where P is a predicate.

Example: { n | ∃i ∃c (STUDENT(i,n,c) AND c='CSE') } returns names n of


students in CSE.
3. Tuple Relational Calculus (TRC)
TRC is another non-procedural query language where variables range over
tuples. A TRC query has the form: { t | P(t) } where t is a tuple variable and P
is a predicate.

Example: { t | STUDENT(t) AND [Link] > 20 } returns tuples of STUDENT with


Age > 20.

TRC and DRC are equivalent in expressive power to relational algebra for safe
queries (queries that produce finite results).
4. Fundamental Operations
Selection (σ): selects tuples satisfying a predicate.
Projection (π): selects certain columns.
Union (∪): combines tuples from two relations with same schema.
Set Difference (−): tuples in one relation but not in another.
Cartesian Product (×): pairs tuples from two relations.

Properties: Commutativity: UNION, Intersection. Associativity: UNION,


Intersection. Distributivity and De Morgan's laws apply in set operations.
5. Additional Operations
Intersection (∩): common tuples.
Join (⋈): combines related tuples from two relations.
Division (÷): useful for queries like 'find entities related to all values of
another set'.
Renaming (ρ): change attribute names.

Join types: Natural Join, Equi-Join, Theta Join, Outer Joins (Left, Right, Full).
Division example: If R(A,B) and S(B) then R ÷ S returns A values related to
every B in S.
6. SQL Fundamentals
SQL is a declarative language used to define, query, and manipulate
relational data. Key statements include CREATE, INSERT, SELECT, UPDATE,
DELETE, and DDL/DML constructs.

Example schema and queries:

CREATE TABLE STUDENT(


StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Course VARCHAR(30)
);

INSERT INTO STUDENT VALUES (101, 'Asha', 20, 'CSE');


INSERT INTO STUDENT VALUES (102, 'Vikram', 21, 'ECE');

-- Simple SELECT
SELECT Name, Course FROM STUDENT WHERE Age > 20 ORDER BY Name;

-- JOIN example
CREATE TABLE DEPT(DeptID INT PRIMARY KEY, DeptName VARCHAR(30));
INSERT INTO DEPT VALUES (1, 'CSE');
INSERT INTO DEPT VALUES (2, 'ECE');

SELECT [Link], [Link] FROM STUDENT S JOIN DEPT D ON


[Link]='CSE' /*example mapping*/;
Aggregate functions: COUNT, SUM, AVG, MIN, MAX. GROUP BY and HAVING
for grouped queries. Subqueries: correlated and uncorrelated. Transactions:
BEGIN, COMMIT, ROLLBACK.
7. Integrity Constraints
Integrity constraints enforce correctness of data. Types:
- Domain constraints
- Entity integrity (Primary Key cannot be NULL)
- Referential integrity (Foreign Key must refer to existing primary key)
- User-defined constraints (CHECK)

Example:

CREATE TABLE EMP(


EmpID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
DeptID INT,
CONSTRAINT fk_dept FOREIGN KEY (DeptID) REFERENCES DEPT(DeptID)
);

Cascading actions: ON DELETE CASCADE, ON UPDATE SET NULL, etc. Ensure


careful design to avoid unintended deletions.
8. Triggers
Triggers are procedural code automatically executed in response to certain
events on a table (INSERT, UPDATE, DELETE). They are used for auditing,
complex constraints, and derived column maintenance.

Example trigger (pseudo-SQL):

CREATE TRIGGER trg_after_insert_emp


AFTER INSERT ON EMP
FOR EACH ROW
BEGIN
INSERT INTO AUDIT_LOG(EventTime, EventType, EmpID) VALUES (NOW(),
'INSERT', [Link]);
END;

Care: avoid long-running triggers; they execute within the transaction and
can affect performance.
9. Views
A view is a virtual table defined by a query. It simplifies complex queries,
enforces security, and provides a level of abstraction.

Example: CREATE VIEW CSE_STUDENTS AS SELECT StudentID, Name FROM


STUDENT WHERE Course='CSE';

Views can be updatable depending on DBMS and view definition. Materialized


views store results physically for performance.
10. Relational Database Concepts & Summary
Relational databases store data in normalized tables to reduce redundancy
and improve integrity. Normal forms (1NF, 2NF, 3NF, BCNF) help design
reliable schemas.

Relational algebra and calculus give formal foundations; SQL implements


much of this functionality in practical systems. Proper use of keys,
constraints, and indexes is essential for performance and correctness.

Indexing, query optimization, and transaction management (ACID properties)


are critical in real-world DBMS.
11. Practice Questions
Q1. Define relational algebra and list its basic operators.

Q2. Write a relational algebra expression to find names of employees in CSE.

Q3. Describe the difference between DRC and TRC with examples.

Q4. Given STUDENT(StudentID, Name, Course, Age), write a TRC query for
students older than 21.

Q5. Show how to express JOIN of EMP and DEPT in relational algebra and SQL.

Q6. Demonstrate a division operation example: 'Find students who took all
courses in a set'.

Q7. Write SQL to create tables STUDENT and ENROLL, and insert sample
rows.

Q8. Explain referential integrity and show an example with FOREIGN KEY.

Q9. Create a trigger to update a LAST_MODIFIED timestamp after row update.

Q10. What is a materialized view and when is it used?

Q11. List differences between INNER JOIN and OUTER JOIN.

Q12. Give an example of a correlated subquery.

Q13. Explain normalization up to 3NF with an example.

Q14. How do indexes improve query performance?

Q15. Write SQL to find average salary per department.

Q16. Explain ACID properties of transactions with examples.

Q17. Show an example of UNION and UNION ALL and explain difference.

Q18. Write a DRC query for student names enrolled in 'DBMS' course.

Q19. Explain how CHECK constraints work with an example.

Q20. Describe query optimization strategies used by RDBMS.


12. Solutions
A1. Relational algebra is a set of operations on relations: σ, π, ∪, −, ×, ρ, ⋈,
∩, ÷.

A2. σ_Dept='CSE'(EMP) then π_Name(σ_Dept='CSE'(EMP)). This selects rows


where Dept='CSE' then projects Name.

A3. DRC uses domain variables, TRC uses tuple variables. Example DRC: { n |
∃i ∃c (STUDENT(i,n,c) AND c='CSE') }. TRC: { t | STUDENT(t) AND
[Link]='CSE' }

A4. TRC: { t | STUDENT(t) AND [Link] > 21 } or using projection { [Link] |


STUDENT(t) AND [Link] > 21 }

A5. Relational algebra join: EMP ⋈_{[Link] = [Link]} DEPT. SQL:


SELECT [Link], [Link] FROM EMP E JOIN DEPT D ON [Link] =
[Link];

A6. Division example: R(A,B) ÷ S(B) returns A values related to all B in S. For
students who took all courses C1,C2 in set S, use division.

A7. Example DDL provided earlier in SQL Fundamentals section; insert rows
using INSERT INTO.

A8. Referential integrity: FOREIGN KEY ensures referenced value exists.


Example: CONSTRAINT fk_dept FOREIGN KEY (DeptID) REFERENCES
DEPT(DeptID).

A9. Example trigger provided earlier: trg_after_insert_emp inserts into


AUDIT_LOG; for update modify LAST_MODIFIED.

A10. Materialized view stores the result physically and is used for expensive
queries to improve performance at cost of storage and maintenance.

A11. INNER JOIN returns matched rows only; OUTER JOIN returns matched
plus unmatched from one or both sides (LEFT/RIGHT/FULL).

A12. Correlated subquery example: SELECT [Link] FROM STUDENT S


WHERE [Link] > (SELECT AVG(Age) FROM STUDENT WHERE Course =
[Link]);

A13. Normalization: 1NF remove repeating groups; 2NF remove partial


dependency on PK; 3NF remove transitive dependency. Example: splitting
student-course table into STUDENT and ENROLL.
A14. Indexes allow faster lookup using tree structures (B-trees), reducing full
table scans.

A15. SELECT DeptID, AVG(Salary) FROM EMP GROUP BY DeptID;

A16. ACID: Atomicity (all or nothing), Consistency (constraints preserved),


Isolation (transactions don't interfere), Durability (committed changes
persist).

A17. UNION removes duplicates, UNION ALL keeps duplicates. Example:


SELECT Name FROM A UNION SELECT Name FROM B;

A18. DRC: { n | ∃i ∃c (ENROLL(i,n,c) AND c='DBMS') } or appropriate schema


variables.

A19. CHECK constraint: CREATE TABLE EMP(... Salary INT CHECK (Salary >
0));

A20. Query optimization: use indexes, rewrite queries, push-down predicates,


use cost-based optimizer and statistics.

Common questions

Powered by AI

The division operation in relational algebra finds tuples in a relation R that are associated with all tuples in another relation S, typically used for queries like 'find students who took all courses in set S'. For a student-course relation R(Student, Course) and a set of courses S(Course), the result is students who took every course in S. In SQL, this requires careful use of subqueries to capture missing associations, by checking that the count of distinct courses a student enrolled in matches the count of distinct courses in the set S. It is useful for ensuring comprehensive participation across courses or categories .

ACID properties ensure reliable transactions in a database: Atomicity guarantees that transactions are all or nothing, Consistency ensures database invariants are maintained, Isolation ensures transactions do not interfere with each other, and Durability assures that committed transactions persist, even after a failure. For example, Atomicity means if a transaction fails in the middle, none of its operations are applied, maintaining database integrity. Consistency checks ensure that all data validations, such as foreign key constraints, are upheld. Isolation is illustrated by concurrent transactions not seeing each other's uncommitted changes due to mechanisms like locks. Durability is demonstrated by a bank transaction still being applied after a power failure, owing to commit logs .

Materialized views are saved tables containing the results of a query, stored physically, unlike regular views which are virtual tables retrieving data dynamically upon each access. They are beneficial in scenarios involving complex, expensive queries that need repeated execution, as they improve performance by caching the results and reducing computation overhead. For instance, running aggregated data reports regularly from transactional data can be optimized using materialized views to avoid recalculating aggregates repeatedly, which saves both time and computational resources .

INNER JOIN returns only the rows from both tables that meet the join condition, exemplified by SELECT * FROM A INNER JOIN B ON A.ID = B.ID where only matching ID rows appear. In contrast, OUTER JOINs (LEFT, RIGHT, FULL) include unmatched rows from one or both sides: LEFT JOIN returns all rows from the left table with matching rows from the right table or NULLs, demonstrated by SELECT * FROM A LEFT JOIN B ON A.ID = B.ID with all A's rows. RIGHT JOIN does the reverse, returning all rows from the right. FULL OUTER JOIN combines all matched and unmatched rows from both tables. These operations cater to various data inclusion needs in queries .

Relational databases utilize several types of joins to combine rows from two or more tables based on related columns. The primary types are Natural Join, Equi-Join, Theta Join, and Outer Joins (Left, Right, Full). A Natural Join connects two tables based on common attribute names; an Equi-Join specifies specific attribute equality; a Theta Join uses any condition. Outer Joins differ by including unmatched rows: a Left Outer Join includes all rows from the left table, Right Outer Join includes all from the right, and Full Outer Join includes all matching and non-matching rows from both .

Indexes improve query performance by allowing quick lookups using data structures like B-trees, which enable the database to find rows more efficiently than scanning the entire table. They drastically reduce the number of data pages that need to be read. However, the choice of indexing should be strategic, as excessive or unnecessary indexes can degrade performance by increasing maintenance overhead, such as during data updates or insertions which require index adjustment. Therefore, indexes should be based on query patterns and should target columns frequently involved in WHERE, JOIN, or ORDER BY clauses .

Integrity constraints ensure the correctness and accuracy of data. They include domain constraints (attribute values must be within a certain range), entity integrity (primary keys must be unique and not null), referential integrity (foreign keys must match primary keys in the referenced table), and user-defined constraints (CHECK constraints). Without these constraints, erroneous or invalid data could be stored, such as orphaned records if foreign key constraints are not enforced properly. For example, if a foreign key isn't implemented correctly in a relational database, deletion of a primary key record in the parent table could result in orphaned child records, leading to invalid references and inconsistencies .

Relational algebra is a procedural query language that defines a set of operations on relations (tables) and serves as the theoretical foundation for SQL, enabling query optimization. Key operators in relational algebra include Selection (σ), Projection (π), Union (∪), Set Difference (−), Cartesian Product (×), and various types of Joins (⋈). These operators provide a structured framework to manipulate and retrieve data, which is practically implemented in SQL through clauses and operations .

TRC and DRC are both non-procedural query languages with equivalent expressive power for safe queries, meaning they yield finite results. TRC focuses on selecting entire tuples based on predicates, using tuple variables that range over such tuples, exemplified by { t | STUDENT(t) AND t.Age > 20 }, which selects tuples where a student's age is greater than 20. DRC, on the other hand, uses domain variables that take values from the domains of attributes, specified as { <d1, d2, ...> | P(d1, d2, ...) }, like { n | ∃i ∃c (STUDENT(i,n,c) AND c='CSE') } to return names of students in CSE .

Triggers automate the execution of procedural code in response to specific events on a table, such as INSERT, UPDATE, or DELETE, enabling complex integrity enforcement, auditing, and derived data maintenance. Unlike static integrity constraints, triggers provide dynamic, conditional response capabilities. For instance, a trigger can automatically log changes to an AUDIT_LOG table whenever updates are made to a sensitive EMP table, thus maintaining an audit trail without requiring application logic changes. Triggers are advantageous over application-side checks as they centralize business logic within the database layer ensuring consistency across different applications accessing the database .

You might also like