Docsity

Database Management Systems complete guide, Study notes of Database Management Systems (DBMS)

Toggle sidebar
bg1
DATABASE MANAGEMENT SYSTEMS
A Simple, Detailed & Easy-to-Understand Study Guide
UNIT โ€“ 2
Relational Query Languages, Relational Database Design
and Query Processing & Optimization
(9 Hours)
Topics Covered in this Guide
1. Relational Algebra
2. Tuple and Domain Relational Calculus
3. SQL3, DDL and DML Constructs
4. Open Source and Commercial DBMS (MySQL, Oracle, DB2, SQL Server)
5. Relational Database Design (Domain & Data Dependency)
6. Armstrong's Axioms
7. Normal Forms (1NF, 2NF, 3NF, BCNF)
8. Dependency Preservation & Lossless Design
9. Query Processing and Optimization
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download Database Management Systems complete guide and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

DATABASE MANAGEMENT SYSTEMS

A Simple, Detailed & Easy-to-Understand Study Guide

UNIT โ€“ 2

Relational Query Languages, Relational Database Design

and Query Processing & Optimization

(9 Hours)

Topics Covered in this Guide

  1. Relational Algebra
  2. Tuple and Domain Relational Calculus
  3. SQL3, DDL and DML Constructs
  4. Open Source and Commercial DBMS (MySQL, Oracle, DB2, SQL Server)
  5. Relational Database Design (Domain & Data Dependency)
  6. Armstrong's Axioms
  7. Normal Forms (1NF, 2NF, 3NF, BCNF)
  8. Dependency Preservation & Lossless Design
  9. Query Processing and Optimization

Table of Contents

1. Introduction to Unit 2

A Database Management System (DBMS) stores data and lets us ask questions about that data. But how do we actually 'ask' the database a question? We need a query language. This unit is all about the languages and design rules that make a relational database (a database made of tables) work correctly and efficiently.

Think of this unit as answering three simple questions:

  • How do we ask questions to a database? โ†’ Relational Algebra, Relational Calculus, and SQL.
  • How do we design good tables so that data is not repeated or messed up?^ โ†’^ Functional Dependency, Armstrong's Axioms, and Normal Forms.
  • How does the database answer our questions quickly?^ โ†’^ Query Processing and Optimization.

Note: If you remember these three questions, the entire unit will feel like a story instead of random topics. Every topic below is simply explaining one part of this story.

2. Relational Algebra

Relational Algebra is a collection of operations that take one or two tables (called relations ) as input and produce a new table as output. It is called a procedural language because we tell the database how to get the result, step by step, like a recipe.

Example: If you want the names of employees who earn more than Rs. 50,000, relational algebra tells the database: 'First pick the rows where salary > 50,000, then pick only the Name column.' This is exactly two operations chained together.

Every relational algebra operation follows one golden rule, called closure : the output of an operation is always another table. This means we can take the output of one operation and immediately use it as the input of the next operation โ€” just like we did in the example above.

2.1 Basic (Fundamental) Operations

There are five fundamental operations. All other operations can be built using only these five.

(a) SELECT ( ฯƒ ) โ€” Picking Rows

The SELECT operation picks rows (tuples) from a table that satisfy a given condition. It works like a filter. In everyday language: 'Show me only the rows I am interested in.'

ฯƒcondition (Relation) Example: ฯƒSalary > 50000 (EMPLOYEE)

Example: Table EMPLOYEE has 100 rows. ฯƒ Dept = 'Sales'(EMPLOYEE) returns only the rows where the Dept column is 'Sales'. If 20 employees work in Sales, the output table has exactly 20 rows, and it still has all the original columns.

Note: Easy way to remember: SELECT chooses ROWS. Think of the Greek letter sigma ( ฯƒ ) as a sieve that lets only matching rows pass through.

(b) PROJECT ( ฯ€ ) โ€” Picking Columns

The PROJECT operation picks specific columns (attributes) from a table and removes the rest. It also automatically removes duplicate rows from the result, because a relation (mathematically, a set) cannot have two identical rows.

ฯ€column1, column2 (Relation) Example: ฯ€Name, Dept (EMPLOYEE)

Example: If EMPLOYEE has columns (ID, Name, Dept, Salary), then ฯ€ Name, Dept(EMPLOYEE) gives a new table with only two columns: Name and Dept. If two employees happen to have identical Name and Dept, only one copy is kept in the result.

2.2 Derived Operations

Derived operations are not fundamentally new โ€” they can be written using the five basic operations above. But they are used so often that they are given their own names and symbols for convenience.

(a) INTERSECTION ( โˆฉ )

Gives only the rows that appear in both tables.

Relation1 โˆฉ Relation2 is the same as Relation1 โˆ’ (Relation1 โˆ’ Relation2)

Example: STUDENTS_IN_MATHS_CLUB โˆฉ STUDENTS_IN_SCIENCE_CLUB gives students who are members of both clubs.

(b) JOIN

JOIN combines rows from two tables based on a related column, keeping only the combinations that make logical sense. It is simply Cartesian Product followed by a SELECT condition, but written as one single, convenient operation. Joins are explained fully in section 2.3 below.

(c) DIVISION (รท)

Division answers questions of the type 'find X that is related to every Y'. It is the relational algebra equivalent of the word 'ALL' or 'EVERY' in English.

Example: Suppose relation ENROLLED(Student, Course) lists which student has enrolled in which course, and relation ALL_COURSES(Course) lists every course offered this semester. Then ENROLLED รท ALL_COURSES gives the names of students who are enrolled in every single course offered โ€” not just some of them.

Note: DIVISION is considered the trickiest of the operations for beginners. A simple trick: whenever you see the words 'all', 'every', or 'each' in a query, DIVISION is very likely the operation you need.

2.3 Join Operations in Detail

A JOIN combines related rows from two tables using a common column (usually a matching key). It is the most frequently used operation in real-world databases because real data is almost always spread across multiple tables.

Join Type What It Returns Simple Explanation

Inner Join Only rows that match in both tables If a row in Table A has no match in Table B,it is left out completely.

Left Outer Join All rows of the left table + matchesfrom the right Unmatched columns from the right table arefilled with NULL.

Right Outer Join All rows of the right table + matchesfrom the left Unmatched columns from the left table arefilled with NULL.

Full Outer Join All rows from both tables Rows that don't match on either side are stillkept, with NULLs filling the gaps.

Equi Join Rows matched using the '=' operatoronly The most common type of join, e.g. matchingon ID = ID.

Natural Join Equi join that auto-removes duplicatecolumns Automatically joins on columns with thesame name, and keeps that column only once.

INNER JOIN Only matching rows from both tables

LEFT OUTER JOIN All of Left + matches from Right

RIGHT OUTER JOIN All of Right + matches from Left

FULL OUTER JOIN All rows from both tables

Fig 2.1: The four common types of joins and what each keeps

Example: EMPLOYEE(ID, Name, DeptID) and DEPARTMENT(DeptID, DeptName). EMPLOYEE JOIN DEPARTMENT (on matching DeptID) gives us the department name next to each employee. If an employee's DeptID does not exist in DEPARTMENT, an Inner Join drops that employee, while a Left Outer Join keeps that employee with DeptName shown as NULL.

PROJECT (Name, Dept)

SELECT (Salary > 50000)

Table: EMPLOYEE

3. Relational Calculus

Relational Algebra is procedural โ€” we describe the exact steps to get the answer. Relational Calculus is non-procedural (declarative) โ€” we only describe what we want, not how to get it. The database itself figures out the steps. SQL, which you use in real life, is actually based on relational calculus ideas.

Note: Simple analogy: Relational Algebra is like giving someone turn-by-turn driving directions. Relational Calculus is like just telling them the destination address and letting them find their own way.

3.1 Tuple Relational Calculus (TRC)

In TRC, a query is written using a tuple variable that represents a row of a table. The general form is:

{ t | P(t) }

This is read as: 'Give me every tuple t such that condition P(t) is true.'

Example: To find employees with salary greater than 50000 from EMPLOYEE table: { t | t โˆˆ EMPLOYEE AND t.Salary > 50000 } In plain English: 'Give me every row t from EMPLOYEE where the salary value of that row is greater than 50000.'

If we only want specific columns, we write:

{ t.Name | t โˆˆ EMPLOYEE AND t.Salary > 50000 }

3.2 Domain Relational Calculus (DRC)

DRC is very similar to TRC, but instead of using a whole row (tuple) as a variable, it uses individual domain variables โ€” one variable per column value.

{ | P(x1, x2, ..., xn) }

Example: To find the names and salaries of employees earning more than 50000: { | โˆƒ id, dept ( โˆˆ EMPLOYEE AND salary > 50000) } In plain English: 'Give me name and salary pairs such that there exists an id and dept making a complete employee row, where the salary is above 50000.'

Feature Tuple Relational Calculus (TRC) Domain Relational Calculus (DRC) Variable represents An entire row (tuple) A single column value (domain) Symbol used t (tuple variable) x1, x2, ... (domain variables) Example basis QUEL language QBE (Query By Example) language

Note: Both TRC and DRC are 'safe' only when they produce a finite set of results. An 'unsafe' expression (like asking for all tuples NOT in a table, without any limiting condition) could try to generate infinite results, so real systems restrict queries to be safe expressions.

);

This builds a new, empty table called Student with five columns. RollNo is set as the PRIMARY KEY (a column that uniquely identifies each row), and DeptID is a FOREIGN KEY linking to another table (Department), which enforces that any DeptID used here must actually exist in the Department table.

ALTER โ€” change an existing table's structure

ALTER TABLE Student ADD COLUMN Email VARCHAR(100); ALTER TABLE Student DROP COLUMN Age; ALTER TABLE Student MODIFY COLUMN Name VARCHAR(80);

ALTER lets us add a new column, remove an existing column, or change a column's data type or size, without having to delete and rebuild the whole table.

DROP โ€” permanently delete a table

DROP TABLE Student;

This removes the table and all the data inside it completely from the database. It cannot normally be undone, so it is used carefully.

TRUNCATE โ€” empty a table quickly

TRUNCATE TABLE Student;

This removes all rows from a table very quickly, but keeps the table's structure intact so you can still insert new rows into it later.

Note: Easy way to remember the difference: DROP removes the entire cupboard (shelves and all), TRUNCATE empties everything off the shelves but keeps the cupboard, and DELETE (a DML command, seen next) removes chosen items one by one and can be undone before it is saved.

4.2 DML โ€“ Data Manipulation Language

DML commands work with the actual data inside the tables that DDL already created. Continuing the cupboard analogy โ€” DML is putting items onto the shelves, taking items out, or rearranging them.

SELECT โ€” retrieve data

SELECT Name, Age FROM Student WHERE DeptID = 5 ORDER BY Name;

This fetches the Name and Age of every student whose DeptID is 5, sorted alphabetically by Name. SELECT is by far the most frequently used SQL command.

INSERT โ€” add new data

INSERT INTO Student (RollNo, Name, Age, DeptID) VALUES (101, 'Anjali Sharma', 19, 5);

This adds one new row to the Student table.

UPDATE โ€” modify existing data

UPDATE Student SET Age = 20 WHERE RollNo = 101;

This changes the Age value of the row where RollNo is 101. The WHERE clause is very important here โ€” without it, every row in the table would be updated.

DELETE โ€” remove data

DELETE FROM Student WHERE RollNo = 101;

This removes the row(s) matching the condition. Again, forgetting the WHERE clause would delete every row in the table.

Command Type What It Does Undo-able before commit? CREATE DDL Makes a new table/object N/A (structural) ALTER DDL Changes table structure N/A (structural) DROP DDL Deletes table + data permanently No SELECT DML Reads/fetches data N/A (read-only) INSERT DML Adds new rows Yes, via ROLLBACK UPDATE DML Changes existing rows Yes, via ROLLBACK DELETE DML Removes rows Yes, via ROLLBACK

Oracle DB Commercial Oracle Corporation Large enterprises, banks, airlines IBM DB2 Commercial IBM Mainframes, banking, insurance

SQL Server Commercial Microsoft Microsoft-based businessapplications

Note: A common exam trick: MySQL is owned by Oracle Corporation, but MySQL itself is still open source and free, while the separate product called 'Oracle Database' is a completely different, paid product from the same company.

6. Relational Database Design

Designing a database is not just about creating tables โ€” it is about creating good tables that avoid repeating the same information again and again and prevent strange errors when data is inserted, updated, or deleted. This is where the idea of dependency becomes important.

6.1 Domain and Data Dependency

Domain

A domain is simply the set of all possible legal values a column (attribute) is allowed to take.

Example: The domain of a column called 'Gender' might be the set {Male, Female, Other}. The domain of 'Age' might be all whole numbers between 0 and 120.

Data Dependency

Data dependency (also simply called dependency) describes a situation where the value of one column depends on, or is decided by, the value of another column. This is the single most important idea for designing correct tables, because it is the root cause of duplicate and inconsistent data.

Example: In a table Orders(OrderID, ProductID, ProductPrice), the ProductPrice depends on ProductID โ€” because once you know the ProductID, the price is fixed. If the same ProductID appears in many rows, this fact (dependency) means the price gets repeated unnecessarily many times, and if the price changes, we would have to update it in every single row.

6.2 Functional Dependency

A Functional Dependency (FD) is a formal, precise way of writing a dependency. We write:

A โ†’ B

This is read as ' A functionally determines B ', which means: for any two rows with the same value of A, the value of B must also be the same. In other words, if you know A, you can always find exactly one value of B.

Example: In table Student(RollNo, Name, DeptID, DeptName): RollNo โ†’ Name (a roll number belongs to exactly one student name) DeptID โ†’ DeptName (a department ID always maps to exactly one department name)

Term Meaning Determinant The column(s) on the left side of the arrow (the 'A' in A โ†’ B) Dependent The column(s) on the right side of the arrow (the 'B' in A โ†’ B)

7. Armstrong's Axioms

Once we know some functional dependencies of a table, we often need to figure out other dependencies that must also be true, even if nobody wrote them down directly. Armstrong's Axioms are a small set of proven rules that let us do exactly this kind of reasoning, safely and completely.

The Three Basic (Primary) Axioms

1. Reflexivity Rule

If B is a subset of A, then A โ†’ B

If B's columns are already contained inside A's columns, then A trivially determines B. This is somewhat obvious, but it is still listed as a formal rule.

Example: (RollNo, Name) โ†’ RollNo โ€” obviously true, since RollNo is already part of the left-hand side.

2. Augmentation Rule

If A โ†’ B, then AC โ†’ BC (for any set of columns C)

If A determines B, then adding the same extra column(s) C to both sides keeps the dependency true.

Example: If RollNo โ†’ Name is true, then (RollNo, DeptID) โ†’ (Name, DeptID) is also true.

3. Transitivity Rule

If A โ†’ B and B โ†’ C, then A โ†’ C

This is exactly like a mathematical chain: if A leads to B, and B leads to C, then A must also lead to C.

Example: If RollNo โ†’ DeptID and DeptID โ†’ DeptName, then RollNo โ†’ DeptName.

Additional (Derived) Rules

These three extra rules are not strictly necessary (they can be proven using the three rules above), but they make everyday reasoning much faster.

Rule Name Statement In Simple Words

Union Rule If Aโ†’B and Aโ†’C, then Aโ†’BC

If A determines B and also determines C separately, it determines both together.

Decomposition Rule If Aโ†’BC, then Aโ†’B and Aโ†’C

If A determines a group of columns, it determines each column in that group on its own.

Pseudo-transitivity Rule If Aโ†’B and BCโ†’D, then ACโ†’D

A mixed version of transitivity, useful for combining dependencies with extra columns.

These rules are called sound (they never produce a wrong/false dependency) and complete (they can derive every dependency that logically follows), which is why they are trusted as the standard toolkit for reasoning about functional dependencies.

Closure of a Set of Attributes

The closure of a set of attributes A, written A+, is the complete set of all attributes that can be functionally determined by A, using Armstrong's Axioms repeatedly. Closure is mainly used to find candidate keys and to check whether a given functional dependency is implied by the others.

Example: Given FDs: RollNo โ†’ Name, RollNo โ†’ DeptID, DeptID โ†’ DeptName. RollNo+^ = {RollNo, Name, DeptID, DeptName} โ€” because starting from RollNo alone, we can eventually reach every other column using the rules above. Since RollNo+^ contains all columns of the table, RollNo is a candidate key.