Relational Query Languages (2) SQL and QBE
Walid G. Aref
Relational Query Languages (2) SQL and QBE Walid G. Aref Query - - PowerPoint PPT Presentation
Relational Query Languages (2) SQL and QBE Walid G. Aref Query Languages For The Relational Model Relational Algebra Procedural Domain Relational Query Query By Languages Relational Example (QBE) Calculus Declarative Tuple Relational
Walid G. Aref
Relational Query Languages Procedural
Relational Algebra
Declarative
Domain Relational Calculus Query By Example (QBE) Tuple Relational Calculus SQL
Walid G. Aref
Relational Query Languages Procedural
Relational Algebra
Declarative
Domain Relational Calculus Query By Example (QBE) Tuple Relational Calculus SQL
Walid G. Aref
From (Table List) Where (Qualification Predicates)
From Students S ß S is a Tuple Variable Where S.age < 21
which is expensive), SQL is based on bags (duplicate tuples allowed)
Walid G. Aref
Walid G. Aref
True or False? Or Null?
(unknown or true) = true (unknown or false) = unknown (unknown or unknown) = unknown
(true and unknown) = unknown (false and unknown) = false (unknown and unknown) = unknown
(not unknown) = unknown
evaluates to unknown
Walid G. Aref
Walid G. Aref
login: string, age: integer, gpa: real)
grade: string)
(sid: CHAR(20), name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL, PRIMARY KEY SID);
(sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid, cid), FOREIGN KEY (sid) REFERENCES Students ON DELETE CASCADE ON UPDATE SET DEFAULT, FOREIGN KEY (cid) REFERENCES Courses ON DELETE CASCADE ON UPDATE CASCADE)
Walid G. Aref
VALUES (0111, ‘Bright, Mary’, ‘mb@purdue.edu’, 22, 4.0)
WHERE S.name = ‘Bright, Mary’
constraints.
program logic
the system
value.
Create Table … … Foreign key ... References … On delete reject On update cascade On delete set default
(sid: CHAR(20), name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL, PRIMARY KEY SID, check gpa >= 0.0 and gpa <=4.0);
(select count(*) from students) / (select count(*) from instructors) < 15)
AS SELECT name, gpa FROM Students WHERE gpa > 3.6 ORDER BY gpa desc <<< to order the tuples by gpa in descending order
student’s grades or gpa when student is in the University health center. Also, hiding medical info of a student from the course instructor
Walid G. Aref
the materialized view tables
Eager maintenance of materialized views (overhead)
materialized view tables eagerly.
the materialized view, then evaluate the query
Walid G. Aref
è Scalar Function
è Table Function (Function that returns a table)
where a table is expected)
Walid G. Aref
From instructors i where i.iid in (select t.iid from teaches t where t.cid = “CS541”);
expecting a table, e.g., in the From clause or as shown above.
UNIQUE, op ANY, op ALL, where op is any of: >, <, =, ≤, ≥, ≠
Walid G. Aref
From instructors i where i.iid in (select t.iid from teaches t where t.cid = “CS541”);
From instructors i where exists (select * from teaches t where i.iid = t.iid and t.cid = “CS541”);
query.
Walid G. Aref
nested query: Find the names of the instructors who taught CS541 at most once Select i.iname From instructors i where unique ( select t.cid from teaches t where i.iid = t.iid and t.cid = “CS541”);
for every outer tuple
Select name, min(age) from Students
Select age, avg(gpa) From Students Group by age
be listed in the GROUP BY clause
Group by age
Group by age, name
GPA for each age group in Students Select age, avg(gpa) From Students where GPA >= 2 Group by age
e.g., where min(gpa) >2
average GPA for each age group in Students when the group has at least 3 students Select age, avg(gpa) From Students where GPA >= 2 Group by age Having count(*) > 2 Count(*) computes the number of tuples
Systems course
from students s, enrolled e, courses c where c.cname = ‘Database Systems’ and c.cid = e.cid and e.sid = s.sid and e.grade >= all (Select e1.grade from enrolled e1, courses c1 where c1.cname = ‘CS541’ and c1.cid = e1.cid)
from students s, enrolled e, courses c where c.cname = ‘Database Systems’ and c.cid = e.cid and e.sid = s.sid and e.grade = (Select max(e1.grade) from enrolled e1, courses c1 where c1.cname = ‘Database Systems’ and c1.cid = e1.cid)
from enrolled e1, courses c1 where c1.cname = ‘Database Systems’ and c1.cid = e1.cid
when some event takes place
SQL statement
specify:
should execute)
trigger executes)
CREATE TRIGGER YoungBrightStudents AFTER INSERT ON Students REFERENCING NEW TABLE NewStudents FOR EACH STATEMENT INSERT INTO YoungAndBright (sid, name, login, age, gpa) SELECT sid, name, login, age, gpa FROM NewStudents N WHERE N.age <= 18 and N.gpa > 3.6
in
table
select max (gpa) from students select name from students, best where students.gpa = best.maxGPA
sid name login age gpa iid iname irank isalary Students Teaches iid cid year semester Instructors
!Mary’ ) ∧ (𝑏𝑓 < 20)(Students)
sid name login age gpa P. P. iid iname irank isalary Students Teaches iid cid year semester Instructors sid name login age gpa P. ’Mary’ <20 iid iname irank isalary Students Teaches iid cid year semester Instructors
sid name login age gpa P. iid iname irank isalary Students Enrolled sid cid grade P. Instructors sid name login age gpa P. _y iid iname irank isalary Students Enrolled sid cid grade P. _y Instructors S1.sid = E.sid
sid name login age gpa _y iid iname irank isalary _x Students Enrolled sid cid grade Instructors Result Person Name P. _y P. _x
sid name login age gpa P._y iid iname irank isalary _𝑧 Students Enrolled sid cid grade Instructors sid name login age gpa P._y iid iname irank isalary ¬_𝑧 Students Enrolled sid cid grade Instructors
sid name login age gpa P. P.AO(2) P.DO(1) Students
Enrolled sid cid grade P.CNT.ALL ‘CS541’
Enrolled sid cid grade P.G. P.CNT.ALL
sid name login age gpa D. <1 Students sid name login age gpa I. Ada Bell AB@p 19 3.8 Students iid iname irank isalary U._X*1.05 Instructors