0% found this document useful (0 votes)
3 views

Module-4

This document covers SQL syntax for creating, dropping, deleting, and altering tables, as well as functions for date manipulation and query processing. It details the steps involved in SQL query processing, including lexical analysis, parsing, and validation, along with heuristic query optimization techniques. Additionally, it provides examples of SQL queries and their corresponding relational algebra representations, illustrating the process of transforming and optimizing query trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module-4

This document covers SQL syntax for creating, dropping, deleting, and altering tables, as well as functions for date manipulation and query processing. It details the steps involved in SQL query processing, including lexical analysis, parsing, and validation, along with heuristic query optimization techniques. Additionally, it provides examples of SQL queries and their corresponding relational algebra representations, illustrating the process of transforming and optimizing query trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Base Management System (CSE )

Module No. 4 SQL, Query Processing and Optimization 7 Hours


SQL, Steps in Query Processing, Transforming SQL queries to Relational Algebra, Heuristic Query
Optimization.

Prepared By: Dr. Radha Mohan Pattanayak


Module-4
SQL, Query Processing and Optimization

CREATE Syntax:
 Create table <table name>(Attribute <type><size>, Attribute <type><size>, ……,
Attribute <type><size> );
 create table Employee (SSN integer PRIMARY KEY, name varchar (5), bdate DATE,
salary integer, Dname varchar (5), DNo integer FOREIGN KEY(DNo) References
Department);
DROP Syntax:
It is used to remove a relation (base table) and its definition Permanently.
 Drop table <table name>;
 Drop table Employee;
DELETE Syntax:
It is used to delete a record or tuple from a relation (base table.
 Delete from <table name>;
 Delete from Employee;
ALTER Syntax:
This command is used to modify any physical information of an existing relation.
Syntax to add new Attribute:
 Alter table <table name> add <new-attribute > <type> <size>;
 Alter table Employee add course varchar (2);
Syntax to Delete an existing attribute:
 Alter table <table name> drop column <attribute name>;
 Alter table Employee drop column dname;
Syntax to Rename an existing attribute:
 Alter table <table name> Rename column <old name> to <new name>;
 Alter table Employee rename column Salary to Esalary;
To add primary key into an existing relation:
 Alter table <table name> Add PRIMARY KEY (attribute name);
 Alter table Employee ADD PRIMARY KEY (Essn);
SYSDATE Syntax:
This function returns the current date time from the operating system.

Prepared By: Dr. Radha Mohan Pattanayak


 Select SYSDATE from dual;
CURRENT_DATE:
This function returns the current date time according to the session time zone.
 Select CURRENT_DATE from dual;
ADD_MONTHS (Date, value to be add)
ADD_MONTHS function adds a specified number of months from the specified date.
 select ADD_MONTHS (SYSDATE, 3) from dual;
 select ADD_MONTHS (bdate,3) from std;
MONTHS_BETWEEN (Date, Date)
This function finds the difference of months in between two specified given date.
 select MONTHS_BETWEEN (SYSDATE, ’25-JAN-2021’) from dual;
 select MONTHS_BETWEEN (SYSDATE, bdate) from Employee;
Syntax for WHERE clause:
It is used to retrieve the records from a relation based on condition.
 Retrieve the birthdate and address of the employee whose name is 'John B. Smith'.
 Select Bdate, Address from Employee Where Fname = 'John’ and Mname = ‘B’ and
Lname = 'Smith’
Syntax for ‘DISTINCT’:
In a query result to eliminate the duplicate tuples from a relation, the keyword DISTINCT is
used.
 Distinct(<attribute>)
 Print the salary of all employees distinctly.
 Select distinct(salary) from Employee.
SQL sub query operation

 Write a subquery to Print the names of all employees who belongs to the department
which is higher or equal than all department number.
Select names from Employee where Dno. >= All (Select Dnumber from
Department);
 Write a subquery to print all employees in the database who earn more than each
employee of ‘WIPRO’.
Select names from Employee where salary > (Select MAX (salary) from Employee
where Cname = ‘WIPRO’);

Prepared By: Dr. Radha Mohan Pattanayak


 Write a subquery to Print the names, street, address, and cities of residence of all
employees who work for ‘WIPRO’ and earn more than 10,000.
Select names, street, address, city from Employee where name in (Select name
from Employee where Cname = ‘WIPRO’ and salary > 10000);
Query Processing
An SQL query undergoes several stages like: lexical analysis (scanning), parsing (YACC),
validation
 scanning: identify SQL tokens
 parser: check the query syntax according to the SQL grammar
 validation: check that all attributes/relation names are valid in the particular database
being queried
After that the query tree will create.
Heuristic Query Optimization
 The main idea to apply heuristic optimization is to transform a query tree into an
equivalent one, representing a RA expression that is more efficient to execute than the
original one, but gives the same results as the original one.
Steps in converting a query tree during heuristic optimization.
(a) Initial (canonical) query tree for SQL query Q.
(b) Moving SELECT operations down the query tree near to its best position of
execution.
(c) Applying the more restrictive SELECT operation first.
(d) Replacing CARTESIAN PRODUCT and SELECT with JOIN operations.
(e) Moving PROJECT operations down the query tree.
Rules for Query Optimization
1) Cascade of σ: A conjunctive selection condition can be broken up into a cascade sequence)
of individual σ operations:
σc1 AND c2 AND ... AND cn(R) = σc1 (σc2 (...(σcn (R))...) )
2) Commutativity of σ: The σ operation is commutative:
σc1 (σc2 (R)) = σc2 (σc1 (R))
3) Cascade of π: In a cascade (sequence) of π operations, all but the last one can be ignored:
πList1 (πList2 (...(πListn(R)).)) = πList1 (R)

Prepared By: Dr. Radha Mohan Pattanayak


4) Commuting σ with π: If the selection condition c involves only the attributes A1, ..., An in
the projection list, the two operations can be commuted:
πA1, AC2, ..., An (σc (R)) = σc (πA1, A2, ..., An (R))
5) Commutativity of ⋈ (and x): The ⋈ operation is commutative as is the x operation:
R ⋈C S = S ⋈C R; R ⋈ S = S ⋈ R
6) Commuting π with (or x): Suppose that the projection list is L = {A1, ..., An, B1,..., Bm},
where A1, ..., An are attributes of R and B1, ..., Bm are attributes of S. If the join condition c
involves only attributes in L, the two operations can be commuted as follows:
πL(R ⋈C S ) = (πA1, ..., An (R)) ⋈C (π B1, ..., Bm (S))
Question:
Write the SQL query and it’s corresponding Relation algebra (RA) to draw an initial query tree
for the query” Find the name, address, and grade of all students who have enrolled the
course ‘DBMS’”. Then apply heuristic query optimization on the above initial query tree to
find the optimal query tree.
Course (Cno, Cname, Credits)
Student (Regd. No, Name, Address, Sem)
Enrolment (Cno, Regd. No, Grade)
Ans:
SQL Query for the above question
Select Name, Address, Grade from Course C, Student S, Enrolment E on C.Cno = E.
Cno and S. Regd. No = E. Regd. No and C. Cname = ‘DBMS’;
Equivalent Relational Algebra (RA) for the above SQL Query

π Name ᴧ Address ᴧ Grade (((σCname = ‘DBMS’ (Course)) ⋈ (Enrolment)) ⋈


(Student));
Step-1: Design the initial Query Tree for the above RA

Prepared By: Dr. Radha Mohan Pattanayak


𝜋 . , . , .

σ . = ‘DBMS’ ∧
× S. Regd No = S. Regd No
C. CNo = E. CNo

S E C

Student Enrolment Course

Step-2: Move the select operation downwards of the tree and place it in their appropriate
place.

𝜋 . , . , .

𝝈𝑪.𝑪𝑵𝒐 𝑬.𝑪𝑵𝒐

𝝈 . .

× 𝝈 .

S E C

Student Enrolment Course

Step-3: Replace the Cartesian product and select operation with join operation.

Prepared By: Dr. Radha Mohan Pattanayak


𝜋 . , . , .

𝐂. 𝐂𝐍𝐨 = 𝐄. 𝐂𝐍𝐨

𝝈 .

S E C

Student Enrolment Course

Step-4:
To reduce the number of resultant records, interchange the relations and perform the join
operation with their appropriate joining attributes.

𝜋 . , . , .

𝐒. 𝐑𝐞𝐠𝐝 𝐍𝐨 = 𝐄. 𝐑𝐞𝐠𝐝 𝐍𝐨

C E S

Course Enrolment Student

Step-5:

Prepared By: Dr. Radha Mohan Pattanayak


Instead of considering all the attributes consider only the attributes which are required to take
part in the operation. For this, make project operation to select only the appropriate attributes
from the relation.

𝜋 . , . , .

S. Regd No = E. Regd No

𝜋 . . , . , .

𝝈 .

𝜋 . , .

E S
C

Course Enrolment Student

Question:
Write the SQL query and it’s corresponding Relation algebra (RA) to draw an initial query tree
for the query” For every project located in ‘Stafford’, retrieve the project number, the
controlling department number, and the department manager’s last name, address, and
birthdate”. Then apply heuristic query optimization on the above initial query tree to find the
optimal query tree.

Prepared By: Dr. Radha Mohan Pattanayak

You might also like