Authors: Abraham Silberschatz, Henry F. Korth, and S. Sudarshan
ISBN: 978-0-07-352332-3
%defaultDatasource jdbc:h2:mem:db
CREATE TABLE IF NOT EXISTS classroom (
building VARCHAR(15),
roomNumber VARCHAR(7),
capacity NUMERIC(4,0),
PRIMARY KEY (building, roomNumber)
);
CREATE TABLE IF NOT EXISTS department (
deptName VARCHAR(20),
building VARCHAR(15),
budget NUMERIC(12,2) CHECK (budget > 0),
PRIMARY KEY (deptName)
);
CREATE TABLE IF NOT EXISTS course (
courseId VARCHAR(8),
title VARCHAR(50),
deptName VARCHAR(20),
credits NUMERIC(2,0) CHECK (credits > 0),
PRIMARY KEY (courseId),
FOREIGN KEY (deptName) REFERENCES department ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS instructor (
instructorId VARCHAR(5),
name VARCHAR(20) NOT NULL,
deptName VARCHAR(20),
salary NUMERIC(8,2) CHECK (salary > 29000),
PRIMARY KEY (instructorId),
FOREIGN KEY (deptName) REFERENCES department ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS section (
courseId VARCHAR(8),
sectionId VARCHAR(8),
semester VARCHAR(6) CHECK (semester IN ('Fall', 'Winter', 'Spring', 'Summer')),
year NUMERIC(4,0) CHECK (year > 1701 AND year < 2100),
building VARCHAR(15),
roomNumber VARCHAR(7),
timeSlotId VARCHAR(4),
PRIMARY KEY (courseId, sectionId, semester, year),
FOREIGN KEY (courseId) REFERENCES course ON DELETE CASCADE,
FOREIGN KEY (building, roomNumber) REFERENCES classroom ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS teaches (
instructorId VARCHAR(5),
courseId VARCHAR(8),
sectionId VARCHAR(8),
semester VARCHAR(6),
year NUMERIC(4,0),
PRIMARY KEY (instructorId, courseId, sectionId, semester, year),
FOREIGN KEY (courseId, sectionId, semester, year) REFERENCES section ON DELETE CASCADE,
FOREIGN KEY (instructorId) REFERENCES instructor ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS student (
studentId VARCHAR(5),
name VARCHAR(20) NOT NULL,
deptName VARCHAR(20),
totalCredit NUMERIC(3,0) CHECK (totalCredit >= 0),
PRIMARY KEY (studentId),
FOREIGN KEY (deptName) REFERENCES department ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS takes (
studentId VARCHAR(5),
courseId VARCHAR(8),
sectionId VARCHAR(8),
semester VARCHAR(6),
year NUMERIC(4,0),
grade VARCHAR(2),
PRIMARY KEY (studentId, courseId, sectionId, semester, year),
FOREIGN KEY (courseId, sectionId, semester, year) REFERENCES section ON DELETE CASCADE,
FOREIGN KEY (studentID) REFERENCES student ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS advisor (
studentId VARCHAR(5),
instructorId VARCHAR(5),
PRIMARY KEY (studentId),
FOREIGN KEY (studentId) REFERENCES student ON DELETE CASCADE,
FOREIGN KEY (instructorId) REFERENCES instructor ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS prereq (
courseId VARCHAR(8),
prereqId VARCHAR(8),
PRIMARY KEY (courseId, prereqId),
FOREIGN KEY (courseId) REFERENCES course ON DELETE CASCADE,
FOREIGN KEY (prereqId) REFERENCES course
);
CREATE TABLE timeSlot (
timeSlotId VARCHAR(4),
day VARCHAR(1),
startHr NUMERIC(2) CHECK (startHr >= 0 AND startHr < 24),
startMin NUMERIC(2) CHECK (startMin >= 0 AND startMin < 60),
endHr NUMERIC(2) CHECK (endHr >= 0 AND endHr < 24),
endMin NUMERIC(2) CHECK (endMin >= 0 AND endMin < 60),
PRIMARY KEY (timeSlotId, day, startHr, startMin)
);
-- Clear Tables
DELETE FROM prereq;
DELETE FROM timeSlot;
DELETE FROM advisor;
DELETE FROM takes;
DELETE FROM student;
DELETE FROM teaches;
DELETE FROM section;
DELETE FROM instructor;
DELETE FROM course;
DELETE FROM department;
DELETE FROM classroom;
-- Classroom
INSERT INTO classroom VALUES ('Packard', '101', '500');
INSERT INTO classroom VALUES ('Painter', '514', '10');
INSERT INTO classroom VALUES ('Taylor', '3128', '70');
INSERT INTO classroom VALUES ('Watson', '100', '30');
INSERT INTO classroom VALUES ('Watson', '120', '50');
-- Department
INSERT INTO department VALUES ('Biology', 'Watson', '90000');
INSERT INTO department VALUES ('Comp. Sci.', 'Taylor', '100000');
INSERT INTO department VALUES ('Elec. Eng.', 'Taylor', '85000');
INSERT INTO department VALUES ('Finance', 'Painter', '120000');
INSERT INTO department VALUES ('History', 'Painter', '50000');
INSERT INTO department VALUES ('Music', 'Packard', '80000');
INSERT INTO department VALUES ('Physics', 'Watson', '70000');
-- Course
INSERT INTO course VALUES ('BIO-101', 'Intro. to Biology', 'Biology', '4');
INSERT INTO course VALUES ('BIO-301', 'Genetics', 'Biology', '4');
INSERT INTO course VALUES ('BIO-399', 'Computational Biology', 'Biology', '3');
INSERT INTO course VALUES ('CS-101', 'Intro. to Computer Science', 'Comp. Sci.', '4');
INSERT INTO course VALUES ('CS-190', 'Game Design', 'Comp. Sci.', '4');
INSERT INTO course VALUES ('CS-315', 'Robotics', 'Comp. Sci.', '3');
INSERT INTO course VALUES ('CS-319', 'Image Processing', 'Comp. Sci.', '3');
INSERT INTO course VALUES ('CS-347', 'Database System Concepts', 'Comp. Sci.', '3');
INSERT INTO course VALUES ('EE-181', 'Intro. to Digital Systems', 'Elec. Eng.', '3');
INSERT INTO course VALUES ('FIN-201', 'Investment Banking', 'Finance', '3');
INSERT INTO course VALUES ('HIS-351', 'World History', 'History', '3');
INSERT INTO course VALUES ('MU-199', 'Music Video Production', 'Music', '3');
INSERT INTO course VALUES ('PHY-101', 'Physical Principles', 'Physics', '4');
-- Instructor
INSERT INTO instructor VALUES ('10101', 'Srinivasan', 'Comp. Sci.', '65000');
INSERT INTO instructor VALUES ('12121', 'Wu', 'Finance', '90000');
INSERT INTO instructor VALUES ('15151', 'Mozart', 'Music', '40000');
INSERT INTO instructor VALUES ('22222', 'Einstein', 'Physics', '95000');
INSERT INTO instructor VALUES ('32343', 'El Said', 'History', '60000');
INSERT INTO instructor VALUES ('33456', 'Gold', 'Physics', '87000');
INSERT INTO instructor VALUES ('45565', 'Katz', 'Comp. Sci.', '75000');
INSERT INTO instructor VALUES ('58583', 'Califieri', 'History', '62000');
INSERT INTO instructor VALUES ('76543', 'Singh', 'Finance', '80000');
INSERT INTO instructor VALUES ('76766', 'Crick', 'Biology', '72000');
INSERT INTO instructor VALUES ('83821', 'Brandt', 'Comp. Sci.', '92000');
INSERT INTO instructor VALUES ('98345', 'Kim', 'Elec. Eng.', '80000');
-- Section
INSERT INTO section VALUES ('BIO-101', '1', 'Summer', '2009', 'Painter', '514', 'B');
INSERT INTO section VALUES ('BIO-301', '1', 'Summer', '2010', 'Painter', '514', 'A');
INSERT INTO section VALUES ('CS-101', '1', 'Fall', '2009', 'Packard', '101', 'H');
INSERT INTO section VALUES ('CS-101', '1', 'Spring', '2010', 'Packard', '101', 'F');
INSERT INTO section VALUES ('CS-190', '1', 'Spring', '2009', 'Taylor', '3128', 'E');
INSERT INTO section VALUES ('CS-190', '2', 'Spring', '2009', 'Taylor', '3128', 'A');
INSERT INTO section VALUES ('CS-315', '1', 'Spring', '2010', 'Watson', '120', 'D');
INSERT INTO section VALUES ('CS-319', '1', 'Spring', '2010', 'Watson', '100', 'B');
INSERT INTO section VALUES ('CS-319', '2', 'Spring', '2010', 'Taylor', '3128', 'C');
INSERT INTO section VALUES ('CS-347', '1', 'Fall', '2009', 'Taylor', '3128', 'A');
INSERT INTO section VALUES ('EE-181', '1', 'Spring', '2009', 'Taylor', '3128', 'C');
INSERT INTO section VALUES ('FIN-201', '1', 'Spring', '2010', 'Packard', '101', 'B');
INSERT INTO section VALUES ('HIS-351', '1', 'Spring', '2010', 'Painter', '514', 'C');
INSERT INTO section VALUES ('MU-199', '1', 'Spring', '2010', 'Packard', '101', 'D');
INSERT INTO section VALUES ('PHY-101', '1', 'Fall', '2009', 'Watson', '100', 'A');
-- Teaches
INSERT INTO teaches VALUES ('10101', 'CS-101', '1', 'Fall', '2009');
INSERT INTO teaches VALUES ('10101', 'CS-315', '1', 'Spring', '2010');
INSERT INTO teaches VALUES ('10101', 'CS-347', '1', 'Fall', '2009');
INSERT INTO teaches VALUES ('12121', 'FIN-201', '1', 'Spring', '2010');
INSERT INTO teaches VALUES ('15151', 'MU-199', '1', 'Spring', '2010');
INSERT INTO teaches VALUES ('22222', 'PHY-101', '1', 'Fall', '2009');
INSERT INTO teaches VALUES ('32343', 'HIS-351', '1', 'Spring', '2010');
INSERT INTO teaches VALUES ('45565', 'CS-101', '1', 'Spring', '2010');
INSERT INTO teaches VALUES ('45565', 'CS-319', '1', 'Spring', '2010');
INSERT INTO teaches VALUES ('76766', 'BIO-101', '1', 'Summer', '2009');
INSERT INTO teaches VALUES ('76766', 'BIO-301', '1', 'Summer', '2010');
INSERT INTO teaches VALUES ('83821', 'CS-190', '1', 'Spring', '2009');
INSERT INTO teaches VALUES ('83821', 'CS-190', '2', 'Spring', '2009');
INSERT INTO teaches VALUES ('83821', 'CS-319', '2', 'Spring', '2010');
INSERT INTO teaches VALUES ('98345', 'EE-181', '1', 'Spring', '2009');
-- Student
INSERT INTO student VALUES ('00128', 'Zhang', 'Comp. Sci.', '102');
INSERT INTO student VALUES ('12345', 'Shankar', 'Comp. Sci.', '32');
INSERT INTO student VALUES ('19991', 'Brandt', 'History', '80');
INSERT INTO student VALUES ('23121', 'Chavez', 'Finance', '110');
INSERT INTO student VALUES ('44553', 'Peltier', 'Physics', '56');
INSERT INTO student VALUES ('45678', 'Levy', 'Physics', '46');
INSERT INTO student VALUES ('54321', 'Williams', 'Comp. Sci.', '54');
INSERT INTO student VALUES ('55739', 'Sanchez', 'Music', '38');
INSERT INTO student VALUES ('70557', 'Snow', 'Physics', '0');
INSERT INTO student VALUES ('76543', 'Brown', 'Comp. Sci.', '58');
INSERT INTO student VALUES ('76653', 'Aoi', 'Elec. Eng.', '60');
INSERT INTO student VALUES ('98765', 'Bourikas', 'Elec. Eng.', '98');
INSERT INTO student VALUES ('98988', 'Tanaka', 'Biology', '120');
-- Takes
INSERT INTO takes VALUES ('00128', 'CS-101', '1', 'Fall', '2009', 'A');
INSERT INTO takes VALUES ('00128', 'CS-347', '1', 'Fall', '2009', 'A-');
INSERT INTO takes VALUES ('12345', 'CS-101', '1', 'Fall', '2009', 'C');
INSERT INTO takes VALUES ('12345', 'CS-190', '2', 'Spring', '2009', 'A');
INSERT INTO takes VALUES ('12345', 'CS-315', '1', 'Spring', '2010', 'A');
INSERT INTO takes VALUES ('12345', 'CS-347', '1', 'Fall', '2009', 'A');
INSERT INTO takes VALUES ('19991', 'HIS-351', '1', 'Spring', '2010', 'B');
INSERT INTO takes VALUES ('23121', 'FIN-201', '1', 'Spring', '2010', 'C+');
INSERT INTO takes VALUES ('44553', 'PHY-101', '1', 'Fall', '2009', 'B-');
INSERT INTO takes VALUES ('45678', 'CS-101', '1', 'Fall', '2009', 'F');
INSERT INTO takes VALUES ('45678', 'CS-101', '1', 'Spring', '2010', 'B+');
INSERT INTO takes VALUES ('45678', 'CS-319', '1', 'Spring', '2010', 'B');
INSERT INTO takes VALUES ('54321', 'CS-101', '1', 'Fall', '2009', 'A-');
INSERT INTO takes VALUES ('54321', 'CS-190', '2', 'Spring', '2009', 'B+');
INSERT INTO takes VALUES ('55739', 'MU-199', '1', 'Spring', '2010', 'A-');
INSERT INTO takes VALUES ('76543', 'CS-101', '1', 'Fall', '2009', 'A');
INSERT INTO takes VALUES ('76543', 'CS-319', '2', 'Spring', '2010', 'A');
INSERT INTO takes VALUES ('76653', 'EE-181', '1', 'Spring', '2009', 'C');
INSERT INTO takes VALUES ('98765', 'CS-101', '1', 'Fall', '2009', 'C-');
INSERT INTO takes VALUES ('98765', 'CS-315', '1', 'Spring', '2010', 'B');
INSERT INTO takes VALUES ('98988', 'BIO-101', '1', 'Summer', '2009', 'A');
INSERT INTO takes VALUES ('98988', 'BIO-301', '1', 'Summer', '2010', null);
-- Advisor
INSERT INTO advisor VALUES ('00128', '45565');
INSERT INTO advisor VALUES ('12345', '10101');
INSERT INTO advisor VALUES ('23121', '76543');
INSERT INTO advisor VALUES ('44553', '22222');
INSERT INTO advisor VALUES ('45678', '22222');
INSERT INTO advisor VALUES ('76543', '45565');
INSERT INTO advisor VALUES ('76653', '98345');
INSERT INTO advisor VALUES ('98765', '98345');
INSERT INTO advisor VALUES ('98988', '76766');
-- Time Slot
INSERT INTO timeSlot VALUES ('A', 'M', '8', '0', '8', '50');
INSERT INTO timeSlot VALUES ('A', 'W', '8', '0', '8', '50');
INSERT INTO timeSlot VALUES ('A', 'F', '8', '0', '8', '50');
INSERT INTO timeSlot VALUES ('B', 'M', '9', '0', '9', '50');
INSERT INTO timeSlot VALUES ('B', 'W', '9', '0', '9', '50');
INSERT INTO timeSlot VALUES ('B', 'F', '9', '0', '9', '50');
INSERT INTO timeSlot VALUES ('C', 'M', '11', '0', '11', '50');
INSERT INTO timeSlot VALUES ('C', 'W', '11', '0', '11', '50');
INSERT INTO timeSlot VALUES ('C', 'F', '11', '0', '11', '50');
INSERT INTO timeSlot VALUES ('D', 'M', '13', '0', '13', '50');
INSERT INTO timeSlot VALUES ('D', 'W', '13', '0', '13', '50');
INSERT INTO timeSlot VALUES ('D', 'F', '13', '0', '13', '50');
INSERT INTO timeSlot VALUES ('E', 'T', '10', '30', '11', '45 ');
INSERT INTO timeSlot VALUES ('E', 'R', '10', '30', '11', '45 ');
INSERT INTO timeSlot VALUES ('F', 'T', '14', '30', '15', '45 ');
INSERT INTO timeSlot VALUES ('F', 'R', '14', '30', '15', '45 ');
INSERT INTO timeSlot VALUES ('G', 'M', '16', '0', '16', '50');
INSERT INTO timeSlot VALUES ('G', 'W', '16', '0', '16', '50');
INSERT INTO timeSlot VALUES ('G', 'F', '16', '0', '16', '50');
INSERT INTO timeSlot VALUES ('H', 'W', '10', '0', '12', '30');
-- Prereq
INSERT INTO prereq VALUES ('BIO-301', 'BIO-101');
INSERT INTO prereq VALUES ('BIO-399', 'BIO-101');
INSERT INTO prereq VALUES ('CS-190', 'CS-101');
INSERT INTO prereq VALUES ('CS-315', 'CS-101');
INSERT INTO prereq VALUES ('CS-319', 'CS-101');
INSERT INTO prereq VALUES ('CS-347', 'CS-101');
INSERT INTO prereq VALUES ('EE-181', 'PHY-101');
-- INT
-- BOOLEAN
-- TINYINT
-- SMALLINT
-- BIGINT
-- IDENTITY
-- DECIMAL
-- DOUBLE
-- REAL
-- TIME
-- DATE
-- TIMESTAMP
-- TIMESTAMP WITH TIME ZONE
-- BINARY
-- OTHER
-- VARCHAR
-- VARCHAR_IGNORECASE
-- CHAR
-- BLOB
-- CLOB
-- UUID
-- ARRAY
-- ENUM
-- GEOMETRY
-- INTERVAL
-- Visit https://en.wikipedia.org/wiki/Data_definition_language
-- for more information regarding DDL.
-- CREATE TABLE r
-- (
-- Attribute1 Domain1,
-- Attribute2 Domain2,
-- ...
-- AttributeN DomainN,
-- (IntegrityConstraint1),
-- ...
-- (IntegrityConstraintk),
-- );
SELECT
clause specifies the attributes to project for the output.FROM
clause specifies the relation from which to query.-- List attribute deptName of all relation instructor.
SELECT deptName
FROM instructor;
-- List attribute deptName of all relation instructor.
-- Disallow Duplicates
SELECT DISTINCT deptName
FROM instructor;
-- List attribute deptName of all relation instructor.
-- Allow Duplicates
SELECT ALL deptName
FROM instructor;
-- List 1.
SELECT 1.0;
-- List Addition.
SELECT 1.0 + 1.0;
-- List Subtraction
SELECT 1.0 - 1.0;
-- List Multiplication.
SELECT 2.0 * 2.0;
-- List Division;
SELECT 1.0 / 2.0;
* Attribute
*
symbol denotes all attributes.SELECT * FROM instructor;
WHERE
clause specifies conditions by which the query should be filtered.-- Connectives: 'and', 'or', 'not'
-- Operators: '<', '<=', '>', '>=', '=', '<>'
SELECT name
FROM instructor
WHERE deptName = 'Comp. Sci.' AND salary > 70000;
SELECT name, courseId
FROM instructor, teaches
WHERE instructor.instructorId = teaches.instructorId
AND instructor.deptName = 'Comp. Sci.';
SELECT name, courseId
FROM instructor NATURAL JOIN teaches
WHERE deptName = 'Comp. Sci.';
AS
operation aliases attributes and relations for efficiency and disambiguity.-- What are the names of all instructors whose salary
-- is greater than at least one instructor in the Biology
-- department?
SELECT DISTINCT T.name
FROM instructor AS T, instructor AS S
WHERE T.salary > S.salary AND S.deptName = 'Biology';
ORDER BY
clause specifies the ordering by which the tuples in the result of a query should be sorted.-- Alphabetically, what are the names of all instructors in the
-- Physics department?
SELECT name
FROM instructor
WHERE deptName = 'Physics'
ORDER BY name;
-- Ascending
SELECT name
FROM instructor
WHERE deptName = 'Physics'
ORDER BY name ASC;
-- Descending
SELECT name
FROM instructor
WHERE deptName = 'Physics'
ORDER BY name DESC;
-- What are the names of instructors with salary amounts
-- between $90,000 and $100,000?
SELECT name
FROM instructor
WHERE salary BETWEEN 90000 AND 100000;
-- What are the instructor names and the courses they taught
-- for all instructors in the Biology department who have
-- taught some course?
SELECT name, courseId
FROM instructor, teaches
WHERE (instructor.instructorId, deptName) = (teaches.instructorId, 'Biology');
-- What are all the courses taught either in Fall 2009 or
-- in Spring 2010, or both?
-- Disallow Duplicates
(SELECT courseId FROM section WHERE semester = 'Fall' AND year = 2009)
UNION
(SELECT courseId FROM section WHERE semester = 'Spring' AND year = 2010);
-- What are all the courses taught either in Fall 2009 or
-- in Spring 2010, or both?
-- Allow Duplicates
(SELECT courseId FROM section WHERE semester = 'Fall' AND year = 2009)
UNION ALL
(SELECT courseId FROM section WHERE semester = 'Spring' AND year = 2010);
-- What are all the courses taught either in Fall 2009 and
-- in Spring 2010?
-- Disallow Duplicates
(SELECT courseId FROM section WHERE semester = 'Fall' AND year = 2009)
INTERSECT
(SELECT courseId FROM section WHERE semester = 'Spring' AND year = 2010);
-- What are all the courses taught either in Fall 2009 but not
-- in Spring 2010?
-- Disallow Duplicates
(SELECT courseId FROM section WHERE semester = 'Fall' AND year = 2009)
EXCEPT
(SELECT courseId FROM section WHERE semester = 'Spring' AND year = 2010);
SELECT NULL;
-- Addition, All NULL
-- SELECT NULL + NULL;
-- SELECT 1.0 + NULL;
-- SELECT NULL + 1.0;
-- Subtraction, All NULL
-- SELECT NULL - NULL;
-- SELECT 1.0 - NULL;
-- SELECT NULL - 1.0;
-- Multiplication, All NULL
-- SELECT NULL * NULL;
-- SELECT 1.0 * NULL;
-- SELECT NULL * 1.0;
-- Division, All NULL
-- SELECT NULL / NULL;
-- SELECT 1.0 / NULL;
-- SELECT NULL / 1.0;
-- And
SELECT NULL AND TRUE;
SELECT NULL AND FALSE;
-- Or
SELECT NULL OR TRUE;
SELECT NULL OR FALSE;
-- Not
SELECT NOT NULL;
-- Is
SELECT NULL IS NULL;
SELECT NULL IS NOT NULL;
-- Average
SELECT AVG(salary)
FROM instructor;
-- Minimum
SELECT MIN(salary)
FROM instructor;
-- Maximum
SELECT MAX(salary)
FROM instructor;
-- Sum
SELECT SUM(salary)
FROM instructor;
-- Count
SELECT COUNT(*)
FROM instructor;
-- What is the total number of instructors who teach a course
-- in Spring 2010 semester?
SELECT COUNT(DISTINCT instructorId)
FROM teaches
WHERE (semester, year) = ('Spring', 2010);
GROUP BY
clause specifies attributes by which tuples with the same value on all specified attributes are placed in the same group.-- What is the average salary in each department?
SELECT deptName, AVG(salary) AS avgSalary
FROM instructor
GROUP BY deptName;
HAVING
clause specifies conditions by which the query should be filtered after groups have been formed.-- What is the average salary in each department
-- if the average salary is greater than $42,000?
SELECT deptName, AVG(salary) AS avgSalary
FROM instructor
GROUP BY deptName
HAVING AVG(salary) > 42000;
COUNT(*)
ignore null values in their input collection.IN
connective tests set membership.NOT IN
connective tests the absense of set membership.-- What are all the courses taught either in Fall 2009 and
-- in Spring 2010?
SELECT DISTINCT courseId
FROM section
WHERE semester = 'Fall'
AND year = 2009
AND courseId IN (
SELECT courseId
FROM section
WHERE semester = 'Spring'
AND year = 2010
);
SOME
connective asserts a condition for any member of a set.ALL
connective asserts a condition for all members of a set.-- What are the names of all instructors whose salary
-- is greater than at least one instructor in the Biology
-- department?
SELECT name
FROM instructor
WHERE salary > SOME (
SELECT salary
FROM instructor
WHERE deptName = 'Biology'
);
-- What are the names of all instructors whose salary
-- is greater than all instructors in the Biology
-- department?
SELECT name
FROM instructor
WHERE salary > ALL (
SELECT salary
FROM instructor
WHERE deptName = 'Biology'
);
EXISTS
connective asserts whether a set is empty.NOT EXISTS
connective asserts whether a set is non-empty.-- What are all the courses taught either in Fall 2009 and
-- in Spring 2010?
SELECT courseId
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.courseId = T.courseId
);
UNIQUE
connective asserts whether a set contains no duplicates.NOT UNIQUE
connective asserts whether a set contains duplicates.-- DOES NOT WORK WITH H2!
-- What are all the courses that were offered at most once
-- in 2009?
-- SELECT T.courseId
-- FROM course AS T
-- WHERE UNIQUE (
-- SELECT R.courseId
-- FROM section AS R
-- WHERE T.courseId = R.courseId
-- AND R.year = 2009
-- );
WITH
clause defines a temporary relation whose definition is available only to the query in which the clause occurs.-- DOES NOT WORK WITH H2!
-- What department has the maximum budget?
-- WITH maxBudget(value) AS (
-- SELECT MAX(budget) FROM department
-- )
-- SELECT budget
-- FROM department, maxBudget
-- WHERE department.budget = maxBudget.value;
-- What are all the departments along with the number of
-- instructors in each department?
SELECT deptName, (
SELECT COUNT(*)
FROM instructor
WHERE department.deptName = instructor.deptName
) AS numInstructors
FROM department;
DELETE
statement deletes all tuples in a relation for which a given predicate is true.SET AUTOCOMMIT FALSE;
SELECT CONCAT('Setup: ', COUNT(*))
FROM instructor;
--
DELETE FROM instructor;
--
SELECT CONCAT('Test 1: ', COUNT(*))
FROM instructor;
--
ROLLBACK;
--
DELETE FROM instructor
WHERE deptName = 'Finance';
--
SELECT CONCAT('Test 2: ', COUNT(*))
FROM instructor
WHERE deptName = 'Finance';
--
ROLLBACK;
--
DELETE FROM instructor
WHERE salary BETWEEN 13000 AND 15000;
--
SELECT CONCAT('Test 3: ', COUNT(*))
FROM instructor
WHERE salary BETWEEN 13000 AND 15000;
--
ROLLBACK;
--
DELETE FROM instructor
WHERE deptName IN (
SELECT deptName
FROM department
WHERE building = 'Watson'
);
--
SELECT CONCAT('Test 4: ', COUNT(*))
FROM instructor
WHERE deptName IN (
SELECT deptName
FROM department
WHERE building = 'Watson'
);
--
ROLLBACK;
SELECT CONCAT('Teardown: ', COUNT(*))
FROM instructor;
SET AUTOCOMMIT TRUE;
INSERT
statement inserts tuples into a relation.SET AUTOCOMMIT FALSE;
SELECT CONCAT('Setup: ', COUNT(*))
FROM course;
--
INSERT INTO course
VALUES ('CS-437', 'Database Systems', 'Comp. Sci.', 4);
--
SELECT CONCAT('Test 1: ', COUNT(*))
FROM course;
--
ROLLBACK;
--
INSERT INTO course (courseId, title, deptName, credits)
VALUES ('CS-437', 'Database Systems', 'Comp. Sci.', 4);
--
SELECT CONCAT('Test 2: ', COUNT(*))
FROM course;
--
ROLLBACK;
SELECT CONCAT('Teardown: ', COUNT(*))
FROM course;
SET AUTOCOMMIT TRUE;
UPDATE
statement updates tuples of a relation.SET AUTOCOMMIT FALSE;
SELECT CONCAT('Setup: ', COUNT(*))
FROM instructor;
--
UPDATE instructor
SET salary = salary * 1.05;
--
ROLLBACK;
--
UPDATE instructor
SET salary = salary * 1.05
WHERE salary < 70000;
--
ROLLBACK;
SELECT CONCAT('Teardown: ', COUNT(*))
FROM instructor;
SET AUTOCOMMIT TRUE;
SELECT *
FROM student NATURAL JOIN takes;
JOIN ... USING
clause specifies the required attributes to match for the join.JOIN ... ON <condition>
clause specifies the required condition to satisfy for the join.INNER JOIN
do not preserve nonmatched tuples.-- DOES NOT WORK WITH H2!
-- SELECT *
-- FROM student INNER JOIN takes USING (studentId);
SELECT student.name, takes.courseId
FROM student INNER JOIN takes
ON student.studentId = takes.studentId;
LEFT OUTER JOIN
preserves tuples only in the relation left of the left outer join operation.RIGHT OUTER JOIN
preserves tuples only in the relation right of the left outer join operation.FULL OUTER JOIN
preserves tuples in both relations.SELECT student.name, takes.courseId
FROM student LEFT OUTER JOIN takes
ON student.studentId = takes.studentId;
SELECT student.name, takes.courseId
FROM student RIGHT OUTER JOIN takes
ON student.studentId = takes.studentId;
-- DOES NOT WORK WITH H2!
-- SELECT student.name, takes.courseId
-- FROM student FULL OUTER JOIN takes
-- ON student.studentId = takes.studentId;
Isolation Level | Allow Phantom Reads? | Allow Non-Repeatable Reads? | Allow Dirty Reads? |
---|---|---|---|
Serializable | No | No | No |
Repeatable Read | Yes | No | No |
Read Committed | Yes | Yes | No |
Read Uncommitted | Yes | Yes | Yes |
Wait-Die (Non-Preemptive)(Elders are Selfless):
Wound-Wait (Preemptive)(Elders are Selfish):
Mohan, C., et al. "ARIES: a transaction recovery method supporting fine-granularity locking and partial rollbacks using write-ahead logging." ACM Transactions on Database Systems (TODS) 17.1 (1992): 94-162.