







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A part of the advanced database systems course (cps 216) at the university level. It covers sql subqueries, including scalar subqueries, in subqueries, and exists subqueries, as well as operational semantics and scoping rules. Additionally, it discusses sql constraints, such as not null, key, referential integrity, and general assertion. The document also includes examples and summaries of sql features covered so far.
Typology: Slides
1 / 13
This page cannot be seen from the preview
Don't miss anything!








2
System R paper and Lomet’s B+^ -tree tricks Due Thursday night
Instructor out of town for a program committee meeting
3
Bags, NULL’s
SELECT-FROM-WHERE statements, set and bag operations, table expressions, aggregation and grouping )Next: subqueries
A query that returns a single row can be used as a value in WHERE, SELECT, etc.
Example: students at the same age as Bart
SELECT * FROM Student WHERE age = (
);
SELECT age FROM Student WHERE name = ’Bart’
What’s Bart’s age?
Runtime error if subquery returns more than one row Under what condition will this runtime error never occur?
What if subquery returns no rows? Return NULL
5
SELECT * FROM Student WHERE age IN (
);
SELECT age FROM Student WHERE name = ’Bart’
What’s Bart’s age?
6
SELECT * FROM Student AS s WHERE EXISTS (SELECT * FROM Student WHERE name = ’Bart’ AND age = s.age); It is a correlated subquery—a subquery that references tuple variables in surrounding queries
A quantified subquery can be used as a value in a WHERE condition
Universal quantification (for all): … WHERE x op ALL ( subquery ) … True iff for all t in the result of subquery , x op t
Existential quantification (exists): … WHERE x op ANY ( subquery ) … True iff there exists some t in the result of subquery such that x op t )Beware
11
FROM Student WHERE GPA >= ALL (SELECT GPA FROM Student); SELECT * FROM Student WHERE NOT (GPA < ANY (SELECT GPA FROM Student)); )Use NOT to negate a condition
12
Bags, NULL’s
SELECT-FROM-WHERE statements, set and bag operations, table expressions, aggregation and grouping Subqueries: not much more expressive power added
14
INSERT INTO Enroll VALUES (456, ’CPS216’);
INSERT INTO Enroll (SELECT SID, ’CPS216’ FROM Student WHERE SID NOT IN (SELECT SID FROM Enroll WHERE CID = ’CPS216’));
15
Delete everything DELETE FROM Enroll;
Delete according to a WHERE condition
Example: Student 456 drops CPS DELETE FROM Enroll WHERE SID = 456 AND CID = ’CPS216’; Example: Drop students with GPA lower than 1.0 from all CPS classes DELETE FROM Enroll WHERE SID IN (SELECT SID FROM Student WHERE GPA < 1.0) AND CID LIKE ’CPS%’;
20
CREATE TABLE Student (SID INTEGER NOT NULL, name VARCHAR(30) NOT NULL, email VARCHAR(30), age INTEGER, GPA FLOAT); CREATE TABLE Course (CID CHAR(10) NOT NULL, title VARCHAR(100) NOT NULL); CREATE TABLE Enroll (SID INTEGER NOT NULL, CID CHAR(10) NOT NULL);
21
Typically implies a primary index Rows are stored inside the index, typically sorted by the primary key value
Typically implies a secondary index Pointers to rows are stored inside the index
CREATE TABLE Student (SID INTEGER NOT NULL PRIMARY KEY, name VARCHAR(30) NOT NULL, email VARCHAR(30) UNIQUE, age INTEGER, GPA FLOAT); CREATE TABLE Course (CID CHAR(10) NOT NULL PRIMARY KEY, title VARCHAR(100) NOT NULL); CREATE TABLE Enroll (SID INTEGER NOT NULL, CID CHAR(10) NOT NULL, PRIMARY KEY(SID, CID));
Works on Oracle but not DB2: DB2 requires UNIQUE key columns to be NOT NULL
This form is required for multi-attribute keys
23
Enroll. SID references Student. SID If an SID appears in Enroll , it must appear in Student Enroll. CID references Course. CID If a CID appears in Enroll , it must appear in Course ) That is, no “dangling pointers”
Student Enroll Course
SID name age GPA 142 Bart 10 2. 123 Milhouse 10 3. 857 Lisa 8 4. 456 Ralph 8 2. ... ... ... ...
CID title CPS216 Advanced Database Systems CPS230 Analysis of Algorithms CPS214 Computer Networks ... ...
SID CID 142 CPS 142 CPS 123 CPS 857 CPS 857 CPS 456 CPS ... ...
24
CREATE TABLE Enroll (SID INTEGER NOT NULL REFERENCES Student(SID), CID CHAR(10) NOT NULL, PRIMARY KEY(SID, CID), FOREIGN KEY CID REFERENCES Course(CID));
Bags, NULL’s
CREATE/DROP TABLE, constraints )Next: views
SELECT-FROM-WHERE statements, set and bag operations, table expressions, aggregation and grouping, subqueries
29
Defined by a query, which describes how to compute the view contents on the fly DBMS stores the view definition query instead of view contents Can be used in queries just like a regular table
30
CREATE VIEW CPS216Roster AS SELECT SID, name, age, GPA FROM Student WHERE SID IN (SELECT SID FROM Enroll WHERE CID = ’CPS216’);
DROP VIEW view_name ;
Called “base tables”
SELECT AVG(GPA) FROM CPS216Roster; To process the query, replace the reference to the view by its definition SELECT AVG(GPA) FROM (SELECT SID, name, age, GPA FROM Student WHERE SID IN (SELECT SID FROM Enroll WHERE CID = ’CPS216’));
32
If applications deal with views, we can change the underlying schema without affecting applications Recall physical data independence: change the physical organization of data without affecting applications
33
Bags, NULL’s
CREATE/DROP TABLE, constraints, views
SELECT-FROM-WHERE statements, set and bag operations, table expressions, aggregation and grouping, subqueries
38
Bags, NULL’s
CREATE/DROP TABLE, constraints, views
SELECT-FROM-WHERE statements, set and bag operations, table expressions, aggregation and grouping, subqueries
Indexes
39