MATRUSRI ENGINEERING COLLEGE�DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
SUBJECT NAME: DataBase Management Systems
FACULTY NAME: K Sunil Manohar Reddy
Insert Your Photo here🡪
MATRUSRI
ENGINEERING COLLEGE
DATABASE MANAGEMENT SYSTEMS
COURSE OBJECTIVES:
COURSE OUTCOMES:
MATRUSRI
ENGINEERING COLLEGE
INTRODUCTION: ��THIS UNIT DEALS WITH THE RELATIONAL MODEL AND STORING, ACCESSING AND MANAGING DATA USING VARIOUS FORMAL AND COMMERCIAL QUERY LANGUAGES.
UNIT-II
OUTCOMES:
Upon completion of this unit, student will be able to:
MATRUSRI
ENGINEERING COLLEGE
CONTENTS:�RELATIONAL MODEL: STRUCTURE OF RELATIONAL DATABASES, FUNDAMENTAL RELATIONAL-ALGEBRA OPERATIONS, ADDITIONAL RELATIONAL-ALGEBRA OPERATIONS, EXTENDED RELATIONAL-ALGEBRA OPERATIONS, NULL VALUES, MODIFICATION OF THE DATABASES ��
OUTCOMES:
Upon completion of this module, student will be able to:
MODULE-II
MATRUSRI
ENGINEERING COLLEGE
Introduction to SQL
History:
MATRUSRI
ENGINEERING COLLEGE
Data Definition Language
The SQL Data Definition Language (DDL) allows the specification of information about relations, including:
MATRUSRI
ENGINEERING COLLEGE
Domain Types in SQL
MATRUSRI
ENGINEERING COLLEGE
Create Table Construct
create table r (A1 D1, A2 D2, ..., An Dn,� (integrity-constraint1),� ...,� (integrity-constraintk));
create table instructor (� ID char(5),� name varchar(20),� dept_name varchar(20),� salary numeric(8,2));
MATRUSRI
ENGINEERING COLLEGE
Integrity Constraints in Create Table
Example:
create table instructor (� ID char(5),� name varchar(20) not null,� dept_name varchar(20),� salary numeric(8,2),� primary key (ID),� foreign key (dept_name) references department);
primary key declaration on an attribute automatically ensures not null
MATRUSRI
ENGINEERING COLLEGE
Examples of Relation Definitions
foreign key (dept_name) references department);
primary key (ID, course_id, sec_id, semester, year) ,
foreign key (ID) references student,� foreign key (course_id, sec_id, semester, year) references section);
Note: sec_id can be dropped from primary key above, to ensure a student cannot be registered for two sections of the same course in the same semester
MATRUSRI
ENGINEERING COLLEGE
Updates to Tables
MATRUSRI
ENGINEERING COLLEGE
Basic Query Structure
MATRUSRI
ENGINEERING COLLEGE
The Select Clause
MATRUSRI
ENGINEERING COLLEGE
The Select Clause
select distinct dept_name� from instructor;
select *� from instructor;
select ‘437’;
select ‘437’ as FOO;
MATRUSRI
ENGINEERING COLLEGE
The Select Clause
select ‘A’� from instructor;
The select clause can contain arithmetic expressions involving the operation, +, –, , and /, and operating on constants or attributes of tuples.
select ID, name, salary/12� from instructor;
would return a relation that is the same as the instructor relation, except that the value of the attribute salary is divided by 12.
Can rename “salary/12” using the as clause:
select ID, name, salary/12 as monthly_salary;
MATRUSRI
ENGINEERING COLLEGE
The Where Clause
select name� from instructor� where dept_name = ‘Comp. Sci.‘;
select name� from instructor� where dept_name = ‘Comp. Sci.' and salary > 80000;
MATRUSRI
ENGINEERING COLLEGE
The From Clause
select *� from instructor, teaches;
MATRUSRI
ENGINEERING COLLEGE
Examples
MATRUSRI
ENGINEERING COLLEGE
The Rename Operation
old-name as new-name�
MATRUSRI
ENGINEERING COLLEGE
Cartesian Product
MATRUSRI
ENGINEERING COLLEGE
String Operations
select name� from instructor� where name like '%dar%' ;
like ‘100 \%' escape '\'
in that above we use backslash (\) as the escape character.
MATRUSRI
ENGINEERING COLLEGE
String Operations
MATRUSRI
ENGINEERING COLLEGE
Ordering the Display of Tuples
select distinct name� from instructor� order by name;
MATRUSRI
ENGINEERING COLLEGE
Where Clause Predicates
select name, course_id
from instructor, teaches
where (instructor.ID, dept_name) = (teaches.ID, ’Biology’);
MATRUSRI
ENGINEERING COLLEGE
Duplicates
1. σθ (r1): If there are c1 copies of tuple t1 in r1, and t1 satisfies selections σθ,, then there are c1 copies of t1 in σθ (r1).
2. ΠA (r ): For each copy of tuple t1 in r1, there is a copy of tuple ΠA (t1) in ΠA (r1) where ΠA (t1) denotes the projection of the single tuple t1.
3. r1 x r2: If there are c1 copies of tuple t1 in r1 and c2 copies of tuple t2 in r2, there are c1 x c2 copies of the tuple t1. t2 in r1 x r2
MATRUSRI
ENGINEERING COLLEGE
Duplicates
r1 = {(1, a) (2,a)} r2 = {(2), (3), (3)}
{(a,2), (a,2), (a,3), (a,3), (a,3), (a,3)}
select A1,, A2, ..., An� from r1, r2, ..., rm� where P;
is equivalent to the multiset version of the expression:
MATRUSRI
ENGINEERING COLLEGE
Set Operations
1. Find courses that ran in Fall 2009 or in Spring 2010
(select course_id from section where sem = ‘Fall’ and year = 2009)� union�(select course_id from section where sem = ‘Spring’ and year = 2010)
2. Find courses that ran in Fall 2009 and in Spring 2010
(select course_id from section where sem = ‘Fall’ and year = 2009)� intersect�(select course_id from section where sem = ‘Spring’ and year = 2010)
3. Find courses that ran in Fall 2009 but not in Spring 2010
(select course_id from section where sem = ‘Fall’ and year = 2009)� except�(select course_id from section where sem = ‘Spring’ and year = 2010)
4. Find the salaries of all instructors that are less than the largest salary.
select distinct T.salary�from instructor as T, instructor as S�where T.salary < S.salary;
MATRUSRI
ENGINEERING COLLEGE
Set Operations
5. Find all the salaries of all instructors
select distinct salary�from instructor
6. Find the largest salary of all instructors.
(select “second query” )� except� (select “first query”)
Set operations in SQL are union, intersect, and except
Each of the above operations automatically eliminates duplicates
m + n times in r union all s
min(m,n) times in r intersect all s
max(0, m – n) times in r except all s
MATRUSRI
ENGINEERING COLLEGE
Null Values
select name� from instructor� where salary is null
MATRUSRI
ENGINEERING COLLEGE
Null Values and Three Valued Logic
MATRUSRI
ENGINEERING COLLEGE
Aggregate Functions
These functions operate on the multi set of values of a column of a relation, and return a value
select avg (salary)�from instructor�where dept_name= ’Comp. Sci.’;
select count (distinct ID)�from teaches�where semester = ’Spring’ and year = 2010;
select count (*) from course;
MATRUSRI
ENGINEERING COLLEGE
Aggregate Functions – Group By
Find the average salary of instructors in each department
select dept_name, avg (salary) as avg_salary�from instructor�group by dept_name;
Attributes in select clause outside of aggregate functions must appear in group by list
MATRUSRI
ENGINEERING COLLEGE
avg_salary
Aggregate Functions – Having Clause
Find the names and average salaries of all departments whose average salary is greater than 42000
select dept_name, avg (salary)
from instructor
group by dept_name
having avg (salary) > 42000;
Note: Predicates in the having clause are applied after the � formation of groups whereas predicates in the where � clause are applied before forming groups
MATRUSRI
ENGINEERING COLLEGE
Null Values and Aggregates
Select sum (salary )� from instructor;
MATRUSRI
ENGINEERING COLLEGE
Nested Sub queries
�
B <operation> (sub query)
Where B is an attribute and <operation> to be defined later.
MATRUSRI
ENGINEERING COLLEGE
Sub queries in the Where Clause
A common use of sub queries is to perform tests:
select distinct course_id
from section
where semester = ’Fall’ and year= 2009 and � course_id in (select course_id
from section
where semester = ’Spring’ and year= 2010);
MATRUSRI
ENGINEERING COLLEGE
Set Membership
select distinct course_id
from section
where semester = ’Fall’ and year= 2009 and � course_id not in (select course_id
from section
where semester = ’Spring’ and year= 2010);
select count (distinct ID)
from takes
where (course_id, sec_id, semester, year) in � (select course_id, sec_id, semester, year
from teaches
where teaches.ID= 10101);
Note: Above query can be written in a much simpler manner. � The formulation above is simply to illustrate SQL features.
MATRUSRI
ENGINEERING COLLEGE
Set Comparison – “some” Clause
select distinct T.name
from instructor as T, instructor as S
where T.salary > S.salary and S.dept name = ’Biology’;
Same query using > some clause
select name
from instructor
where salary > some (select salary
from instructor
where dept name = ’Biology’);
MATRUSRI
ENGINEERING COLLEGE
Set Comparison – “all” Clause
select name
from instructor
where salary > all (select salary
from instructor
where dept name = ’Biology’);
MATRUSRI
ENGINEERING COLLEGE
Use of “exists” Clause
select course_id� from section as S� where semester = ’Fall’ and year = 2009 and � exists (select *� from section as T� where semester = ’Spring’ and year= 2010 � and S.course_id = T.course_id);
MATRUSRI
ENGINEERING COLLEGE
Use of “not exists” Clause
Find all students who have taken all courses offered in the Biology department.
select distinct S.ID, S.name
from student as S
where not exists ( (select course_id
from course
where dept_name = ’Biology’)
except
(select T.course_id
from takes as T
where S.ID = T.ID));
MATRUSRI
ENGINEERING COLLEGE
Sub queries in the From Clause
select dept_name, avg_salary�from (select dept_name, avg (salary) as avg_salary� from instructor� group by dept_name)�where avg_salary > 42000;
select dept_name, avg_salary�from (select dept_name, avg (salary) � from instructor� group by dept_name) as dept_avg (dept_name, avg_salary)
where avg_salary > 42000;
MATRUSRI
ENGINEERING COLLEGE
Modification of the Database
MATRUSRI
ENGINEERING COLLEGE
Deletion
delete from instructor;
delete from instructor� where dept name in (select dept name� from department� where building = ’Watson’);
MATRUSRI
ENGINEERING COLLEGE
Deletion
delete from instructor
where salary < (select avg (salary)
from instructor);
1. First, compute avg (salary) and find all tuples to delete
2. Next, delete all tuples found above (without recomputing
avg or retesting the tuples)
MATRUSRI
ENGINEERING COLLEGE
Insertion
Add a new tuple to course
insert into course values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);
or equivalently
�
insert into course (course_id, title, dept_name, credits) values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);
Add a new tuple to student with tot_creds set to null
insert into student values (’3003’, ’Green’, ’Finance’, null);
MATRUSRI
ENGINEERING COLLEGE
Insertion
insert into student� select ID, name, dept_name, 0� from instructor
Otherwise queries like
insert into table1 select * from table1
would cause problem
MATRUSRI
ENGINEERING COLLEGE
Updates
Write two update statements:
update instructor� set salary = salary * 1.03� where salary > 100000;� update instructor� set salary = salary * 1.05� where salary <= 100000;
MATRUSRI
ENGINEERING COLLEGE
Test for Absence of Duplicate Tuples
select T.course_id�from course as T�where unique (select R.course_id� from section as R� where T.course_id= R.course_id � and R.year = 2009);
MATRUSRI
ENGINEERING COLLEGE
With Clause
MATRUSRI
ENGINEERING COLLEGE
Complex Queries using With Clause
with dept _total (dept_name, value) as
(select dept_name, sum(salary)
from instructor
group by dept_name),
dept_total_avg(value) as
(select avg(value)
from dept_total)
select dept_name
from dept_total, dept_total_avg
where dept_total.value > dept_total_avg.value;
MATRUSRI
ENGINEERING COLLEGE