DbmsModule 2
DbmsModule 2
MODULE-II
Contents:
Entity relationship model, Components of ER model, Mapping E-R model to Relational schema,
Relational Algebra, Tuple & Domain Relational Calculus, Relational Query Languages: SQL and
QBE. Database Design:-Database development life cycle (DDLC), Automated design tools,
Functional dependency and Decomposition, Join strategies, Dependency Preservation & lossless
Design, Normalization, Normal forms:1NF, 2NF,3NF, and BCNF, Multi-valued Dependencies,
4NF & 5NF. Query processing and optimization: Evaluation of Relational Algebra Expressions,
Query optimization, Query cost estimation.
The Entity which is having its own Primary Key is called Strong Entity
The Entity which is not having its own Primary Key is called Weak Entity
1
DATABASE MANAGEMENT SYSTEMS
Attributes: Attributes are the properties which define the entity type. For example,
Roll_No, Name, DOB, Age, Address, Mobile_No are the attributes which defines entity type
Student. In ER diagram, attribute is represented by an oval.
Types of attributes:
1. Key Attribute: The attribute which uniquely identifies each entity in the entity set is called
key attribute. For example, Roll_No will be unique for each student. In ER diagram, key
attribute is represented by an oval with underlying lines.
3. Multivalued Attribute: An attribute consisting more than one value for a given entity. For
example, Phone_No (can be more than one for a given student). In ER diagram, multivalued
attribute is represented by double oval.
4. Derived Attribute: An attribute which can be derived from other attributes of the entity
type is known as derived attribute. e.g.; Age (can be derived from DOB). In ER diagram,
derived attribute is represented by dashed oval.
5. Simple attribute: The attributes with values that are atomic and cannot be broken down
further are simple attributes.
The complete entity type Student with its attributes can be represented as:
2
DATABASE MANAGEMENT SYSTEMS
Relationships: When an Entity is related to another Entity, they are said to have a
relationship. For example, A Class Entity is related to Student entity, because students study
in classes, hence this is a relationship.
Relationship Type: A relationship type represents the association between entity types.
For example, ‘Enrolled in’ is a relationship type that exists between entity type Student and
Course.
Relationship Set: A set of relationships of same type is known as relationship set. The
following relationship set depicts S1 is enrolled in C2, S2 is enrolled in C1 and S3 is enrolled
in C3.
1. Unary Relationship – When there is only ONE entity set participating in a relation, the
relationship is called as unary relationship. For example, one person is married to only one
person.
3
DATABASE MANAGEMENT SYSTEMS
2. Binary Relationship – When there are TWO entities set participating in a relation, the
relationship is called as binary relationship. For example, Student is enrolled in Course.
3. n-ary Relationship – When there are n entities set participating in a relation, the relationship
is called as n-ary relationship.
Cardinality: The number of times an entity of an entity set participates in a relationship set
is known as cardinality. Cardinality can be of different types:
1. One to one – When each entity in each entity set can take part only once in the relationship,
the cardinality is one to one. Let us assume that a male can marry to one female and a female
can marry to one male. So the relationship will be one to one.
2. Many to one – When entities in one entity set can take part only once in the relationship
set and entities in other entity set can take part more than once in the relationship set,
cardinality is many to one. Let us assume that a student can take only one course but one
course can be taken by many students. So the cardinality will be n to 1. It means that for one
course there can be n students but for one student, there will be only one course.
4
DATABASE MANAGEMENT SYSTEMS
In this case, each student is taking only 1 course but 1 course has been taken by many
students.
3. Many to many – When entities in all entity sets can take part more than once in the
relationship cardinality is many to many. Let us assume that a student can take more than
one course and one course can be taken by many students. So the relationship will be many
to many.
In this example, student S1 is enrolled in C1 and C3 and Course C3 is enrolled by S1, S3 and
S4. So it is many to many relationships.
5
DATABASE MANAGEMENT SYSTEMS
Participation Constraint: Participation Constraint is applied on the entity participating in
the relationship set.
1. Total Participation – Each entity in the entity set must participate in the relationship. If
each student must enrol in a course, the participation of student will be total. Total
participation is shown by double line in ER diagram.
2. Partial Participation – The entity in the entity set may or may NOT participate in the
relationship. If some courses are not enrolled by any of the student, the participation of
course will be partial.
The diagram depicts the ‘Enrolled in’ relationship set with Student Entity set having total
participation and Course Entity set having partial participation.
Every student in Student Entity set is participating in relationship but there exists a course
C4 which is not taking part in the relationship.
Weak Entity Type and Identifying Relationship: As discussed before, an entity type has a
key attribute which uniquely identifies each entity in the entity set. But there exists some
entity type for which key attribute can’t be defined. These are called Weak Entity type.
For example, a company may store the information of dependants (Parents, Children, Spouse)
of an Employee. But the dependents don’t have existence without the employee. So
Dependent will be weak entity type and Employee will be Identifying Entity type for
Dependant.
A weak entity type is represented by a double rectangle. The participation of weak entity
type is always total. The relationship between weak entity type and its identifying strong
entity type is called identifying relationship and it is represented by double diamond.
6
DATABASE MANAGEMENT SYSTEMS
7
DATABASE MANAGEMENT SYSTEMS
E-R Diagram for Hospital:
8
DATABASE MANAGEMENT SYSTEMS
E-R Diagram for University and marks Database:
9
DATABASE MANAGEMENT SYSTEMS
E-R diagram for student examination:
Relational Algebra:
Relational algebra is a procedural query language. It gives a step by step process to obtain
the result of the query. It uses operators to perform queries.
Types:
10
DATABASE MANAGEMENT SYSTEMS
[Link] Operation:
1. Notation: σ p(r)
Where:
Input:
1. σ BRANCH_NAME="perryride" (LOAN)
Output:
2. Project Operation:
This operation shows the list of those attributes that we wish to appear in the result. Rest of the
attributes are eliminated from the table.
It is denoted by ∏.
Where
11
DATABASE MANAGEMENT SYSTEMS
Input:
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
3. Union Operation:
Suppose there are two tuples R and S. The union operation contains all the tuples that are either in
R or S or both in R & S.
It eliminates the duplicate tuples. It is denoted by ∪.
1. Notation: R ∪ S
Example:
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
12
DATABASE MANAGEMENT SYSTEMS
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Set Intersection:
Suppose there are two tuples R and S. The set intersection operation contains all tuples that are in
both R & S.
It is denoted by intersection ∩.
1. Notation: R ∩ S
Input:
Output:
CUSTOMER_NAME
13
DATABASE MANAGEMENT SYSTEMS
Smith
Jones
5. Set Difference:
Suppose there are two tuples R and S. The set intersection operation contains all tuples that are in R
but not in S.
It is denoted by intersection minus (-).
1. Notation: R - S
Input:
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
6. Cartesian product
The Cartesian product is used to combine each row in one table with each row in the other table. It
is also known as a cross product.
It is denoted by X.
1. Notation: E X D
Example:
EMPLOYEE
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
14
DATABASE MANAGEMENT SYSTEMS
C Legal
Input:
1. EMPLOYEE X DEPARTMENT
Output:
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).
Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
1. ρ(STUDENT1, STUDENT)
Relational Calculus
Relational calculus is a non-procedural query language. In the non-procedural query language, the user is
concerned with the details of how to obtain the end results.
The relational calculus tells what to do but never explains how to do.
15
DATABASE MANAGEMENT SYSTEMS
Types of Relational calculus:
The tuple relational calculus is specified to select the tuples in a relation. In TRC, filtering variable uses the
tuples of a relation.
The result of the relation can have one or more tuples.
Notation:
Where
For example:
OUTPUT: This query selects the tuples from the AUTHOR relation. It returns a tuple with 'name' from
Author who has written an article on 'database'.
TRC (tuple relation calculus) can be quantified. In TRC, we can use Existential (∃) and Universal
Quantifiers (∀).
For example:
Output: This query will yield the same result as the previous one.
16
DATABASE MANAGEMENT SYSTEMS
The second form of relation is known as Domain relational calculus. In domain relational calculus, filtering
variable uses the domain of attributes.
Domain relational calculus uses the same operators as tuple calculus. It uses logical connectives ∧ (and), ∨
(or) and ┓ (not).
It uses Existential (∃) and Universal Quantifiers (∀) to bind the variable.
Notation:
Where
For example:
Output: This query will yield the article, page, and subject from the relational javatpoint, where the
subject is a database.
QBE (Query By Example): Stands for "Query By Example." QBE is a feature included with various
database applications that provides a user-friendly method of running database queries. Typically without
QBE, a user must write input commands using correct SQL (Structured Query Language) syntax. This is a
standard language that nearly all database programs support. However, if the syntax is slightly incorrect the
query may return the wrong results or may not run at all.
The Query By Example feature provides a simple interface for a user to enter queries. Instead of writing an
entire SQL command, the user can just fill in blanks or select items to define the query she wants to
perform. For example, a user may want to select an entry from a table called "Table1" with an ID of 123.
Using SQL, the user would need to input the command, "SELECT * FROM Table1 WHERE ID = 123".
The QBE interface may allow the user to just click on Table1, type in "123" in the ID field and click
"Search."
QBE is offered with most database programs, though the interface is often different between applications.
For example, Microsoft Access has a QBE interface known as "Query Design View" that is completely
graphical. The phpMyAdmin application used with MySQL, offers a Web-based interface where users can
select a query operator and fill in blanks with search terms. Whatever QBE implementation is provided
with a program, the purpose is the same – to make it easier to run database queries and to avoid the
frustrations of SQL errors.
17
DATABASE MANAGEMENT SYSTEMS
Database Development Life Cycle(DDLC):
Database development is just one part of the much wider field of software engineering, the process of
developing and maintaining software. A core aspect of software engineering is the subdivision of the
development process into a series of phases, or steps, each of which focuses on one aspect of the
development. The collection of these steps is sometimes referred to as a development life cycle.
[Link]: this allows the designer to draw a conceptual schema diagram in some tool specific
notations.
[Link] Mapping: this implements mapping algorithms, this mapping is a system specific most tools
generated schemas in SQL DDL for Oracle.
[Link] Normalization: This utilizes a set of functional dependencies that are supplied at the conceptual
design.
[Link] off analysis: A tool should be present the designer with adequate analysis
[Link] of Design results: Design results such as schemas are often displayed in diagrammatic form.
Diagrams are not easy to generate automatically so we can slow the design in form the tables.
[Link] Verification: Its purpose is to verify that the resulting design satisfies the initial requirements.
Join Strategies:
A Join operation combines related tuples from different relations, if and only if a given join condition is
satisfied. It is denoted by ⋈.
Example:
EMPLOYEE
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
SALARY
EMP_CODE SALARY
101 50000
102 30000
103 25000
Result:
19
DATABASE MANAGEMENT SYSTEMS
101 Stephan 50000
1. Natural Join:
A natural join is the set of tuples of all combinations in R and S that are equal on their common attribute
names.
It is denoted by ⋈.
Example: Let's use the above EMPLOYEE table and SALARY table:
Input:
Output:
EMP_NAME SALARY
Stephan 50000
Jack 30000
20
DATABASE MANAGEMENT SYSTEMS
Harry 25000
2. Outer Join:
The outer join operation is an extension of the join operation. It is used to deal with missing information.
Example:
EMPLOYEE
FACT_WORKERS
Input:
1. (EMPLOYEE ⋈ FACT_WORKERS)
Output:
Left outer join contains the set of tuples of all combinations in R and S that are equal on their common
attribute names.
In the left outer join, tuples in R have no matching tuples in S.
It is denoted by ⟕.
Input:
1. EMPLOYEE ⟕ FACT_WORKERS
Right outer join contains the set of tuples of all combinations in R and S that are equal on their common
attribute names.
In right outer join, tuples in S have no matching tuples in R.
It is denoted by ⟖.
Input:
1. EMPLOYEE ⟖ FACT_WORKERS
Output:
Full outer join is like a left or right join except that it contains all rows from both tables.
In full outer join, tuples in R that have no matching tuples in S and tuples in S that have no matching tuples
in R in their common attribute name.
It is denoted by ⟗.
22
DATABASE MANAGEMENT SYSTEMS
Example: Using the above EMPLOYEE table and FACT_WORKERS table
Input:
1. EMPLOYEE ⟗ FACT_WORKERS
Output:
3. Equi join:
It is also known as an inner join. It is the most common join. It is based on matched data as per the equality condition.
The equi join uses the comparison operator(=).
Example:
CUSTOMER RELATION
CLASS_ID NAME
1 John
2 Harry
3 Jackson
PRODUCT
PRODUCT_ID CITY
1 Delhi
2 Mumbai
3 Noida
Input:
1. CUSTOMER ⋈ PRODUCT
Output:
23
DATABASE MANAGEMENT SYSTEMS
1 John 1 Delhi
2 Harry 2 Mumbai
3 Harry 3 Noida
Integrity Constraints
Integrity constraints are a set of rules. It is used to maintain the quality of information.
Integrity constraints ensure that the data insertion, updating, and other processes have to be performed in
such a way that data integrity is not affected.
Thus, integrity constraint is used to guard against accidental damage to the database.
1. Domain constraints
Domain constraints can be defined as the definition of a valid set of values for an attribute.
The data type of domain includes string, character, integer, time, date, currency, etc. The value of the
attribute must be available in the corresponding domain.
Example:
The entity integrity constraint states that primary key value can't be null.
This is because the primary key value is used to identify individual rows in relation and if the primary key
has a null value, then we can't identify those rows.
24
DATABASE MANAGEMENT SYSTEMS
A table can contain a null value other than the primary key field.
Example:
Example:
25
DATABASE MANAGEMENT SYSTEMS
4. Key constraints
Keys are the entity set that is used to identify an entity within its entity set uniquely.
An entity set can have multiple keys, but out of which one key will be the primary key. A primary key can
contain a unique and null value in the relational table.
Example:
Functional Dependency
The functional dependency is a relationship that exists between two attributes. It typically exists between the primary
key and non-key attribute within a table.
1. X → Y
The left side of FD is known as a determinant; the right side of the production is known as a dependent.
For example:
Here Emp_Id attribute can uniquely identify the Emp_Name attribute of employee table because if we know the
Emp_Id, we can tell that employee name associated with it.
1. Emp_Id → Emp_Name
26
DATABASE MANAGEMENT SYSTEMS
Types of Functional dependency
Example:
Example:
1. ID → Name,
2. Name → DOB
27
DATABASE MANAGEMENT SYSTEMS
1. Reflexive Rule (IR1)
1. If X ⊇ Y then X → Y
Example:
1. X = {a, b, c, d, e}
2. Y = {a, b, c}
The augmentation is also called as a partial dependency. In augmentation, if X determines Y, then XZ determines
YZ for any Z.
1. If X → Y then XZ → YZ
Example:
In the transitive rule, if X determines Y and Y determine Z, then X must also determine Z.
1. If X → Y and Y → Z then X → Z
Union rule says, if X determines Y and X determines Z, then X must also determine Y and Z.
1. If X → Y and X → Z then X → YZ
Proof:
1. X → Y (given)
2. X → Z (given)
3. X → XY (using IR2 on 1 by augmentation with X. Where XX = X)
4. XY → YZ (using IR2 on 2 by augmentation with Y)
5. X → YZ (using IR3 on 3 and 4)
5. Decomposition Rule (IR5)
Decomposition rule is also known as project rule. It is the reverse of union rule.
This Rule says, if X determines Y and Z, then X determines Y and X determines Z separately.
1. If X → YZ then X → Y and X → Z
Proof:
1.X → YZ (given)
2. YZ → Y (using IR1 Rule)
3. X → Y (using IR3 on 1 and 2)
28
DATABASE MANAGEMENT SYSTEMS
6. Pseudo transitive Rule (IR6)
1. If X → Y and YZ → W then XZ → W
Proof:
1. X → Y (given)
2. WY → Z (given)
3. WX → WY (using IR2 on 1 by augmenting with W)
4. WX → Z (using IR3 on 3 and 2)
Normalization
Normal
Description
Form
A relation will be in 2NF if it is in 1NF and all non-key attributes are fully functional dependent on the
2NF
primary key.
4NF A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-valued dependency.
5NF A relation is in 5NF if it is in 4NF and not contains any join dependency and joining should be lossless.
29
DATABASE MANAGEMENT SYSTEMS
First Normal Form (1NF)
EMPLOYEE table:
7272826385,
14 John UP
9064738238
7390372389,
12 Sam Punjab
8589830302
The decomposition of the EMPLOYEE table into 1NF has been shown below:
14 John 7272826385 UP
14 John 9064738238 UP
Example: Let's assume, a school can store the data of teachers and the subjects they teach. In a school, a
teacher can teach more than one subject.
TEACHER table
25 Chemistry 30
25 Biology 30
30
DATABASE MANAGEMENT SYSTEMS
47 English 35
83 Math 38
83 Computer 38
In the given table, non-prime attribute TEACHER_AGE is dependent on TEACHER_ID which is a proper
subset of a candidate key. That's why it violates the rule for 2NF.
To convert the given table into 2NF, we decompose it into two tables:
TEACHER_DETAIL table:
TEACHER_ID TEACHER_AGE
25 30
47 35
83 38
TEACHER_SUBJECT table:
TEACHER_ID SUBJECT
25 Chemistry
25 Biology
47 English
83 Math
83 Computer
A relation will be in 3NF if it is in 2NF and not contain any transitive partial dependency.
3NF is used to reduce the data duplication. It is also used to achieve the data integrity.
If there is no transitive dependency for non-prime attributes, then the relation must be in third
normal form.
A relation is in third normal form if it holds atleast one of the following conditions for every non-trivial
function dependency X → Y.
1. X is a super key.
2. Y is a prime attribute, i.e., each element of Y is part of some candidate key.
Example:
31
DATABASE MANAGEMENT SYSTEMS
EMPLOYEE_DETAIL table:
Non-prime attributes: In the given table, all attributes except EMP_ID are non-prime.
Here, EMP_STATE & EMP_CITY dependent on EMP_ZIP and EMP_ZIP dependent on EMP_ID.
The non-prime attributes (EMP_STATE, EMP_CITY) transitively dependent on super
key(EMP_ID). It violates the rule of third normal form.
That's why we need to move the EMP_CITY and EMP_STATE to the new <EMPLOYEE_ZIP>
table, with EMP_ZIP as a Primary key.
EMPLOYEE table:
EMPLOYEE_ZIP table:
201010 UP Noida
02228 US Boston
32
DATABASE MANAGEMENT SYSTEMS
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
Example: Let's assume there is a company where employees work in more than one department.
EMPLOYEE table:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
The table is not in BCNF because neither EMP_DEPT nor EMP_ID alone are keys.
To convert the given table into BCNF, we decompose it into three tables:
EMP_COUNTRY table:
EMP_ID EMP_COUNTRY
264 India
264 India
EMP_DEPT table:
33
DATABASE MANAGEMENT SYSTEMS
EMP_DEPT_MAPPING table:
EMP_ID EMP_DEPT
D394 283
D394 300
D283 232
D283 549
Functional dependencies:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
Candidate keys:
Now, this is in BCNF because left side part of both the functional dependencies is a key.
A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-valued dependency.
For a dependency A → B, if for a single value of A, multiple values of B exists, then the relation
will be a multi-valued dependency.
Example
STUDENT
21 Computer Dancing
21 Math Singing
34 Chemistry Dancing
34
DATABASE MANAGEMENT SYSTEMS
74 Biology Cricket
59 Physics Hockey
The given STUDENT table is in 3NF, but the COURSE and HOBBY are two independent entity. Hence,
there is no relationship between COURSE and HOBBY.
In the STUDENT relation, a student with STU_ID, 21 contains two courses, Computer and Math and two
hobbies, Dancing and Singing. So there is a Multi-valued dependency on STU_ID, which leads to
unnecessary repetition of data.
So to make the above table into 4NF, we can decompose it into two tables:
STUDENT_COURSE
STU_ID COURSE
21 Computer
21 Math
34 Chemistry
74 Biology
59 Physics
STUDENT_HOBBY
STU_ID HOBBY
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey
A relation is in 5NF if it is in 4NF and not contains any join dependency and joining should be lossless.
5NF is satisfied when all the tables are broken into as many tables as possible in order to avoid redundancy.
5NF is also known as Project-join normal form (PJ/NF).
Example
35
DATABASE MANAGEMENT SYSTEMS
In the above table, John takes both Computer and Math class for Semester 1 but he doesn't take Math class
for Semester 2. In this case, combination of all these fields required to identify a valid data.
Suppose we add a new Semester as Semester 3 but do not know about the subject and who will be taking
that subject so we leave Lecturer and Subject as NULL. But all three columns together acts as a primary
key, so we can't leave other two columns blank.
So to make the above table into 5NF, we can decompose it into three relations P1, P2 & P3:
P1
SEMESTER SUBJECT
Semester 1 Computer
Semester 1 Math
Semester 1 Chemistry
Semester 2 Math
P2
SUBJECT LECTURER
Computer Anshika
Computer John
Math John
36
DATABASE MANAGEMENT SYSTEMS
Math Akash
Chemistry Praveen
P3
SEMSTER LECTURER
Semester 1 Anshika
Semester 1 John
Semester 1 John
Semester 2 Akash
Semester 1 Praveen
Relational Decomposition
When a relation in the relational model is not in appropriate normal form then the decomposition of
a relation is required.
In a database, it breaks the table into multiple tables.
If the relation has no proper decomposition, then it may lead to problems like loss of information.
Decomposition is used to eliminate some of the problems of bad design like anomalies,
inconsistencies, and redundancy.
Types of Decomposition
Lossless Decomposition
If the information is not lost from the relation that is decomposed, then the decomposition will be
lossless.
The lossless decomposition guarantees that the join of relations will result in the same relation as it
was decomposed.
The relation is said to be lossless decomposition if natural joins of all the decomposition give the
original relation.
Example:
37
DATABASE MANAGEMENT SYSTEMS
EMPLOYEE_DEPARTMENT table:
The above relation is decomposed into two relations EMPLOYEE and DEPARTMENT
EMPLOYEE table:
22 Denim 28 Mumbai
33 Alina 25 Delhi
46 Stephan 30 Bangalore
52 Katherine 36 Mumbai
60 Jack 40 Noida
DEPARTMENT table
827 22 Sales
438 33 Marketing
869 46 Finance
575 52 Production
678 60 Testing
Now, when these two relations are joined on the common column "EMP_ID", then the resultant relation
will look like:
Employee ⋈ Department
38
DATABASE MANAGEMENT SYSTEMS
Dependency Preserving
Multivalued Dependency
Multivalued dependency occurs when two attributes in a table are independent of each other but,
both depend on a third attribute.
A multivalued dependency consists of at least two attributes that are dependent on a third attribute
that's why it always requires at least three attributes.
Example: Suppose there is a bike manufacturer company which produces two colors(white and black) of
each model every year.
Here columns COLOR and MANUF_YEAR are dependent on BIKE_MODEL and independent of each
other.
39
DATABASE MANAGEMENT SYSTEMS
In this case, these two columns can be called as multivalued dependent on BIKE_MODEL. The
representation of these dependencies is shown below:
1. BIKE_MODEL → → MANUF_YEAR
2. BIKE_MODEL → → COLOR
Join Dependency
Query Plans: A query plan (or query execution plan) is an ordered set of steps used to access data in a
SQL relational database management system.
Query Optimization: A single query can be executed through different algorithms or re-written in
different forms and structures. Hence, the question of query optimization comes into the picture – Which of
these forms or pathways is the most optimal? The query optimizer attempts to determine the most efficient
way to execute a given query by considering the possible query plans.
Importance: The goal of query optimization is to reduce the system resources required to fulfil a query,
and ultimately provide the user with the correct result set faster.
40
DATABASE MANAGEMENT SYSTEMS
Query optimization involves three steps, namely query tree generation, plan generation, and query plan
code generation.
A query tree is a tree data structure representing a relational algebra expression. The tables of the query are
represented as leaf nodes. The relational algebra operations are represented as the internal nodes. The root
represents the query as a whole.
During execution, an internal node is executed whenever its operand tables are available. The node is then
replaced by the result table. This process continues for all internal nodes until the root node is executed and
replaced by the result table.
EMPLOYEE
DEPARTMENT
Example 1
πEmpID(σEName="ArunKumar"(EMPLOYEE))
The corresponding query tree will be −
41
DATABASE MANAGEMENT SYSTEMS
Example 2
πEName,Salary(σDName="Marketing"(DEPARTMENT))⋈DNo=DeptNo(EMPLOYEE)
Following is the query tree for the above query.
After the query tree is generated, a query plan is made. A query plan is an extended query tree that includes
access paths for all operations in the query tree. Access paths specify how the relational operations in the
tree should be performed. For example, a selection operation can have an access path that gives details
about the use of B+ tree index for selection.
Besides, a query plan also states how the intermediate tables should be passed from one operator to the
next, how temporary tables should be used and how operations should be pipelined/combined.
Cost Estimation: The main of Query Optimization is to choose the most efficient way of implement the
relation algebra operations at the lowest cost.
Query Optimization should not dependent solely, but it should also estimate the cost of executing the
different strategies and find the strategies with minimum cost estimate.
The cost function is used in query optimization is estimates and but exact cost function
The cost of an operation is heavily dependent on its selectivity that is proportion of select operation that
forms output.
43