340 likes | 1.04k Views
Correlated SubQueries and Miscellaneous Topics ISYS 464 Correlated SubQueries A correlated subquery references values of the main query. In a correlated subquery, the main query provides values which are used by subquery’s WHERE clause.
E N D
Correlated SubQueries • A correlated subquery references values of the main query. • In a correlated subquery, the main query provides values which are used by subquery’s WHERE clause. • The subquery is executed repeatedly, once for each row that might be selected by the main query.
Project Table: PID, Pname, Budget ProjExpense Table: PID, EDate, Amount • Find projects where total expenses exceeding budget. • Method 1: Using natural join and subtotal • SELECT pid, pname, budget, Sum(Amount) • FROM project NATURAL JOIN Expense • GROUP BY pid, pname,budget • HAVING budget < SUM(Amount); • If Sum(Amount) is not required to show: • SELECT pid, pname, budget • FROM project NATURAL JOIN Expense • GROUP BY pid, pname,budget • HAVING budget < SUM(Amount); • Method 2:Can we use sub query to avoid join? • SELECT pid, pname, budget FROM project • WHERE budget < (SELECT SUM(expense) FROM Expense);
Passing Values from Main Query to SubQuery • SELECT pid, pname, budget • FROM project • WHERE budget < • (SELECT SUM(Amount) FROM Expense WHERE pid=project.pid);
Find students taking at least 3 courses. • 1. Join: • SELECT sid, sname • FROM student NATURAL JOIN registration • GROUP BY sid, sname • HAVING COUNT(cid) >=3; • 2. Subquery: • SELECT sid, sname FROM student • WHERE sid IN (SELECT sid FROM registration GROUP BY sid • HAVING COUNT(cid) >=3); • 3. Correlated query: • SELECT sid, sname FROM student • WHERE 3 <= (SELECT COUNT(cid) FROM registration WHERE sid = student.sid);
EXISTS, NOT EXISTS • Test whether a subquery returns any rows. • It returns True/False
Find faculty who advise at least one student. • 1. Subquery: • SELECT fid,fname FROM faculty • WHERE fid in (SELECT DISTINCT fid FROM student); • 2. Correlated query: • SELECT fid,fname FROM faculty • WHERE EXISTS (SELECT * FROM student • WHERE fid = faculty.fid);
Find students taking 3 courses • 1. Sub query: • SELECT sid, sname FROM student • WHERE sid IN (SELECT sid FROM registration GROUP BY sid • HAVING COUNT(cid) = 3); • 2. Correlated query: • SELECT sid, sname FROM student • WHERE 3 = (SELECT COUNT(cid) FROM registration WHERE sid=student.sid);
SELECT List Containing CorrelatedSub Query SELECT sid, sname, (SELECT COUNT(cid) FROM registration WHERE sid=student.sid) AS courses FROM student;
Display project budget and total expenses Select pid,pname,budget,(Select Sum(amount) From projexpense Where pid=project.pid)as TotalExpense From project; Find projects where total expenses exceeding budget Select pid,pname,budget,totalexpense from (Select pid,pname,budget, (Select sum(amount) from projexpense where pid=project.pid) as TotalExpense From project) Where budget<totalExpense; Note: Calculated field cannot used in the WHERE clause.
Multiple Levels of Nesting • Find student who are taking 1-unit course. • Regular subquery: • SELECT * FROM student WHERE sid IN ( • SELECT sid FROM registration WHERE cid IN(SELECT cid FROM course WHERE units = 1)); • Correlated subquery • SELECT sid,sname FROM student WHERE exists (SELECT sid FROM registration WHERE sid=student.sid and exists (SELECT * FROM course WHERE cid=registration.cid and units = 1));
Nesting Aggregates • SELECT MAX(AVG(gpa)) • FROM student • GROUP BY major;
Views • A database view is: • a virtual or logical table based on a query. • a stored query. • CREATE VIEW viewname AS query; • CREATE VIEW femalestudent AS • SELECT * FROM student WHERE sex=‘f’; • CREATE OR REPLACE VIEW femalestudent AS SELECT * FROM student WHERE sex=‘f’;
READ ONLY Views • CREATE VIEW viewname AS query • WITH READ ONLY; • Ex: • CREATE VIEW readEmp • AS (SELECT * FROM emp) • WITH READ ONLY;
Updating Through Views • A view is updatable if • the update command does not violate database constraints; • The view is not read only.
Materialized Views • Database snapshot • A physical copy • CREATE MATERIALIZED VIEW mvOrders • REFRESH COMPLETE • START WITH SYSDATE NEXT SYSDATE+7 • AS SELECT * FROM Orders; • DROP MATERIALIZED VIEW viewname;
DROP VIEW • DROP VIEW viewname
Indexes • Field declared as PRIMARY KEY will have an index. • CREATE INDEX indexname • ON tablename (columnnames separated by commas); • Ex: • CREATE INDEX fkFID • ON student (fid); • DROP INDEX indexname;
Maintaining Referential Integrity • FOREIGN KEY (field) REFREENCES parentable DROP TABLE parent; CREATE TABLE parent ( pid char(3) not null, pname varchar(20), PRIMARY KEY (pid) ); drop table child; CREATE TABLE child ( cid char(3) not null, cname varchar(20), pid char(3), PRIMARY KEY (cid), FOREIGN KEY (pid) REFERENCES parent ON DELETE SET NULL ); Insert into parent values ('p1','peter'); Insert into parent values ('p2','paul'); Insert into child values ('c1','mary','p1'); Insert into child values ('c2','john','p1'); Insert into child values ('c3','mary','p2');
ON DELETE • ON DELETE SET NULL • ON DELETE CASCADE