Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Miscellaneous Topics. Views A database view is: – a virtual or logical table based on a query. – a stored query. CREATE VIEW viewname AS query; –CREATE.

Similar presentations


Presentation on theme: "SQL Miscellaneous Topics. Views A database view is: – a virtual or logical table based on a query. – a stored query. CREATE VIEW viewname AS query; –CREATE."— Presentation transcript:

1 SQL Miscellaneous Topics

2 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’;

3 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.

4 Project Table: –PID, Pname, Budget Expenses Table: –PID, Date, Expense Find projects where total expenses exceeding budget. Method 1: Using view a. Create a TotalExpense view: PID, TotalExpense. b. Then join the Project table with the TotalExpense view. Method 2: Using natural join and subtotal SELECT pid, pname, budget FROM project NATURAL JOIN Expense GROUP BY pid, pname,budget HAVING budget < SUM(expense); Method 3:Can we use sub query? SELECT pid, pname, budget FROM project WHERE budget < (SELECT SUM(expense) FROM Expense);

5 Passing Values from Main Query to SubQuery SELECT pid, pname, budget FROM project WHERE budget < (SELECT SUM(expense) FROM Expense WHERE pid=project.pid);

6 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);

7 EXISTS, NOT EXISTS Test whether a subquery returns any rows. It returns True/False

8 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);

9 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);

10 SELECT List Containing Nested Query SELECT sid, sname, (SELECT COUNT(cid) FROM registration WHERE sid=student.sid) AS courses FROM student;

11 Self Join Recursive relationship: –Employee supervise Employee Ex: EmpFile: empID, empName, superID Find employees’ supervisor name: –SELECT e1.empid, e1.empname,e2.empname as sueprname –FROM empfile e1, empfile e2 –WHERE e1.superid=e2.empid;

12 Multiple Levels of Nesting Find student who are taking 5-units course. SELECT * FROM student WHERE sid IN ( –SELECT sid FROM registration WHERE cid IN (SELECT cid FROM course WHERE units = 5));

13 Nesting Aggregates SELECT MAX(AVG(gpa)) FROM student GROUP BY major;

14 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’;

15 READ ONLY Views CREATE VIEW viewname AS query –WITH READ ONLY; Ex: –CREATE VIEW readEmp –AS (SELECT * FROM emp) –WITH READ ONLY;

16 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;

17 Updating Through Views A view is updatable if –the update command does not violate database constraints; –The view is not read only.

18 DROP VIEW viewname

19 ROWNUM Field ROWNUM field is a pseudocolumn that applies to every table and view. Use ROWNUM to do Top n Analysis: –Select students with the best 3 GPA Create a view order by GPA, then select from view with rownum <=3. Or using InLineView

20 InLine View When a multiple-column subquery is used in the FROM clause of an outer query, it basically creates a temporary table that can be referenced by other clauses of the outer query. The temporary table is called InLine view. Ex 1: –SELECT sid, sname FROM –(SELECT * FROM student NATURAL JOIN registration) –WHERE cid=‘isys263’; Note: This is different from: –SELECT sid, sname FROM –student NATURAL JOIN registration –WHERE cid=‘isys263’; Ex 2: –SELECT * FROM (SELECT * FROM student ORDER BY GPA DESC);

21 Top (Last) n Analysis Find students with the top 3 GPA. Can we do: –SELECT * FROM student ORDER BY GPA desc WHERE ROWNUM<=3; ? --- No! SELECT * FROM (SELECT * FROM student ORDER BY GPA DESC) WHERE ROWNUM<=3; Note: Use the ROWNUM of the InLineView.

22 Indexes Field declared as PRIMARY KEY will have an index. CREATE INDEX indexname –ON tablename (column names separated by commas); –Ex: CREATE INDEX fkFID ON student (fid); DROP INDEX indexname;

23 Bitmap Index Useful for improving queries on columns that have a small number of distinct values. CREATE BITMAPINDEX indexname –ON tablename (column names separated by commas);

24 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');

25 ON DELETE ON DELETE SET NULL ON DELETE CASCADE


Download ppt "SQL Miscellaneous Topics. Views A database view is: – a virtual or logical table based on a query. – a stored query. CREATE VIEW viewname AS query; –CREATE."

Similar presentations


Ads by Google