Upload
haminh
View
215
Download
0
Embed Size (px)
Citation preview
©Silberschatz, Korth and Sudarshan3.1Database System Concepts - 6th Edition
Explain in words what this relational algebra expression returns:
QUIZ: Relational Algebra
A: The names of all customers who have accounts at both the Downtown and uptown branches
©Silberschatz, Korth and Sudarshan3.2Database System Concepts - 6th Edition
Practice Exercise 2.7
Employee(person_name, street, city)Works(person_name, company_name, salary)Company(company_name, city)
Write relational algebra expressions to:
Several solutions are possible!
©Silberschatz, Korth and Sudarshan3.3Database System Concepts - 6th Edition
A question of efficiency These relational algebra expressions have the same result. Which one is more efficient?
A: Expression a, because the selection is applied before the join, so it doesn’t have a large intermediate result like b.
Database System Concepts, 6th Ed.©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Chapter 3: Introduction to SQL
©Silberschatz, Korth and Sudarshan3.5Database System Concepts - 6th Edition
3.1 History
1970: Edgar F. Codd, a researcher working for IBM, describes the theory of relational databases in "A Relational Model of Data for Large Shared Data Banks"
1971: Codd describes Data Sub-Language ALPHA (DSL/ALPHA) in the paper "Data Base Sublanguage Founded on the Relational Calculus"
1973: IBM San Jose Research Center starts developing a relational DBMS, System R, to implement Codd's concepts.
The first language used in System R was named SEQUEL(Structured English QUEry Language).
©Silberschatz, Korth and Sudarshan3.6Database System Concepts - 6th Edition
• 1981: IBM releases their first commercial DBMS, named SQL/DS (Structured Query Language/Data System). The language is overhauled and renamed SQL (Structured Query Language).
• 1983: IBM introduces the name DB2 for a new version of SQL/DS. The language keeps the name SQL.
• ANSI / ISO/ IEC standards for SQL:• SQL-86, SQL-89, SQL-92• SQL:1999, SQL:2003, SQL:2008, SQL:2011
• Commercial systems offer most features from the standard (not all!), plus various proprietary features.
©Silberschatz, Korth and Sudarshan3.7Database System Concepts - 6th Edition
3.2 Data Definition Language (DDL)
• The schema for each relation.• The domain of values associated with each
attribute.• Integrity constraints (e.g. NOT NULL)• Other information such as
The set of indices to be maintained for each relations. Security and authorization information for each relation. The physical storage structure of each relation on disk.
Allows specification of information about relations:
©Silberschatz, Korth and Sudarshan3.8Database System Concepts - 6th Edition
Basic Domain Types in SQL
char(n). Fixed length character string, with user-specified length n. Shorter strings are padded w/trailing spaces (but the
function LEN ignores them!) varchar(n). Variable length character strings, with user-
specified maximum length n. varchar(MAX) → MAX is implementation-dependent (1 GB
in PostgreSQL 9) Character Large OBject (CLOB)
PostgreSQL implements it through the proprietary types text and varchar().
©Silberschatz, Korth and Sudarshan3.9Database System Concepts - 6th Edition
Basic Domain Types in SQL char(n). Fixed length character string, with user-specified length n. varchar(n). Variable length character strings, with user-specified
maximum length n.
How does SQL compare strings of different lengths?Standard SQL says: Spaces are added to the shorter one until
strings have same length. Not always implemented correctly!
How about Unicode?nvarchar But many implementations (including PostgreSQL) allow
UTF-8 in varchar.
©Silberschatz, Korth and Sudarshan3.10Database System Concepts - 6th Edition
Strings nitty-grittyIn standard SQL and PostgreSQL: single quotes are for strings (required!), double quotes are for identifiers.The double quotes are optional: we only use them if we want to avoid the automatic conversion to uppercase (standard SQL) or lowercase (PostgreSQL).
©Silberschatz, Korth and Sudarshan3.11Database System Concepts - 6th Edition
Basic Domain Types in SQL
int. Integer (a finite subset of the integers that is machine-dependent).
smallint. Small integer (a machine-dependent subset of the integer domain type).
numeric(p , d). Fixed point number, with user-specified precision of p digits, of which n to the right of decimal point.
real, double precision. Floating point and double-precision floating point numbers, with machine-dependent precision.
float(n). Floating point number, with user-specified precision of at least n digits.
p does not include the sign, nor the decimal point!
©Silberschatz, Korth and Sudarshan3.12Database System Concepts - 6th Edition
Numeric Types in PostgreSQL
Source: https://www.postgresql.org/docs/9.5/static/datatype-numeric.html
©Silberschatz, Korth and Sudarshan3.13Database System Concepts - 6th Edition
Basic Domain Types in SQL
All the basic domain types above also supports the NULLvalue!
©Silberschatz, Korth and Sudarshan3.14Database System Concepts - 6th Edition
CREATE TABLE Syntax:
CREATE TABLE r (A1 D1, A2 D2, ..., An Dn,(integrity-constraint1),...,(integrity-constraintk))
r is the name of the relation each Ai is an attribute name in the schema of relation r Di is the data type of values in the domain of attribute Ai
Example:CREATE TABLE instructor (
ID CHAR(5),name VARCHAR(20) NOT NULL,dept_name VARCHAR(20),salary NUMERIC(8,2) …
©Silberschatz, Korth and Sudarshan3.15Database System Concepts - 6th Edition
Integrity Constraints in CREATE TABLE NOT NULL PRIMARY KEY (A1, ..., An ) FOREIGN KEY (Am, ..., An ) REFERENCES r
PRIMARY KEY declaration on an attribute automatically ensures NOT NULL
Example:CREATE TABLE instructor (
ID CHAR(5),name VARCHAR(20) NOT NULL,dept_name VARCHAR(20),salary NUMERIC(8,2) PRIMARY KEY (ID),FOREIGN KEY (dept_name) REFERENCES department)
);
©Silberschatz, Korth and Sudarshan3.16Database System Concepts - 6th Edition
QUIZ: Integrity ConstraintsBased on the example, write CREATE TABLE commands for student and takesCREATE TABLE instructor (
ID CHAR(5),name VARCHAR(20) NOT NULL,dept_name VARCHAR(20),salary VARCHAR(8,2) PRIMARY KEY (ID),FOREIGN KEY (dept_name) REFERENCES department
);
©Silberschatz, Korth and Sudarshan3.17Database System Concepts - 6th Edition
CREATE TABLE student (ID varchar(5),name varchar(20) not null,dept_name varchar(20),tot_cred numeric(3,0),primary key (ID),foreign key (dept_name) references department) );
CREATE TABLE takes (ID varchar(5),course_id varchar(8),sec_id varchar(8),semester varchar(6),year numeric(4,0),grade varchar(2),primary key (ID, course_id, sec_id, semester, year),foreign key (ID) references student,foreign key (course_id, sec_id, semester, year) references section );
Note: sec_id can be dropped from primary key above, to enforce that a student cannot be registered for two sections of the same course in the same semester.
©Silberschatz, Korth and Sudarshan3.18Database System Concepts - 6th Edition
CREATE TABLE course (course_id varchar(8) primary key,title varchar(50),dept_name varchar(20),credits numeric(2,0),foreign key (dept_name) references department) );
Note: Primary key declaration can be combined with attribute declaration as shown.
©Silberschatz, Korth and Sudarshan3.19Database System Concepts - 6th Edition
After creating a table, we enter data in it
CREATE TABLE instructor (ID char(5),name varchar(20) not null,dept_name varchar(20),salary numeric(8,2) PRIMARY KEY (ID),FOREIGN KEY (dept_name) REFERENCES department)
);
INSERT INTO instructor VALUES (‘10211’, ’Smith’, ’Biology’, 66000);
INSERT INTO instructor VALUES (‘10211’, null, ’Biology’, 66000);
©Silberschatz, Korth and Sudarshan3.20Database System Concepts - 6th Edition
DROP TABLE and ALTER TABLE
drop table student Deletes the table and its contents
delete from student Deletes all contents of table, but retains table
alter table alter table r add A D
where A is the name of the attribute to be added to relation r and D is the domain of A.
All tuples in the relation are assigned null as the value for the new attribute.
alter table r drop A
where A is the name of an attribute of relation rDropping of attributes not supported by many
databases
©Silberschatz, Korth and Sudarshan3.21Database System Concepts - 6th Edition
3.3 Basic Query Structure
The SQL data-manipulation language (DML) provides the ability to query information, and insert, delete and update tuples
A typical SQL query has the form:
select A1, A2, ..., Anfrom r1, r2, ..., rmwhere P
Ai represents an attribute Ri represents a relation P is a predicate.
The result of an SQL query is a relation.
©Silberschatz, Korth and Sudarshan3.22Database System Concepts - 6th Edition
The SELECT clause
It lists the attributes desired in the result of a query corresponds to the projection operation of the
relational algebra Example: find the names of all instructors:
select namefrom instructor
NOTE: SQL names are case insensitive (i.e., you may use upper- or lower-case letters.) E.g. Name ≡ NAME ≡ name Some people use upper case wherever we use bold
font.
©Silberschatz, Korth and Sudarshan3.23Database System Concepts - 6th Edition
The SELECT clause
SQL allows duplicates in relations as well as in query results.
To force the elimination of duplicates, insert the keyword distinct after select. Find the names of all departments with instructor, and
remove duplicates:
SELECT distinct dept_nameFROM instructor
The keyword all specifies that duplicates not be removed (seldom used, since it’s the default):
SELECT all dept_nameFROM instructor
©Silberschatz, Korth and Sudarshan3.24Database System Concepts - 6th Edition
The SELECT clause
An asterisk in the select clause denotes “all attributes”SELECT *FROM instructor
The select clause can contain arithmetic expressions involving the operation, +, –, ∗, and /, and operating on constants or attributes of tuples.
The query:
SELECT ID, name, salary/12FROM instructor
would return a relation that is the same as the instructor relation, except that the value of the attribute salary is divided by 12.
©Silberschatz, Korth and Sudarshan3.25Database System Concepts - 6th Edition
The WHERE clause
Specifies conditions that the result must satisfy Corresponds to the selection predicate of the relational
algebra. Find all instructors in Comp. Sci. dept with salary > 80000
select namefrom instructorwhere dept_name = ‘Comp. Sci.' and
salary > 80000 Comparison results can be combined using the logical
connectives and, or, and not. Comparisons can be applied to results of arithmetic
expressions.
©Silberschatz, Korth and Sudarshan3.26Database System Concepts - 6th Edition
The FROM clause
The from clause lists the relations involved in the query Corresponds to the Cartesian product operation of the
relational algebra. Find the Cartesian product instructor X teaches
select ∗from instructor, teaches
generates every possible instructor – teaches pair, with all attributes from both relations
Cartesian product not very useful directly, but useful combined with where-clause condition (selection operation in relational algebra)
©Silberschatz, Korth and Sudarshan3.27Database System Concepts - 6th Edition
Cartesian Product: instructor X teachesinstructor teaches
©Silberschatz, Korth and Sudarshan3.28Database System Concepts - 6th Edition
Joins For all instructors who have taught some course, find their names
and the course ID of the courses they taught.select name, course_idfrom instructor, teacheswhere instructor.ID = teaches.ID
Find the course ID, semester, year and title of each course offered by the Comp. Sci. department
select section.course_id, semester, year, titlefrom section, coursewhere section.course_id = course.course_id and
dept_name = ‘Comp. Sci.'
©Silberschatz, Korth and Sudarshan3.29Database System Concepts - 6th Edition
QUIZ: Queries
Find the names of all students who are taking a class this semester in the SCIENCE building
©Silberschatz, Korth and Sudarshan3.30Database System Concepts - 6th Edition
QUIZ
What are the three integrity constraints we have covered? Give examples of how each is used.
©Silberschatz, Korth and Sudarshan3.31Database System Concepts - 6th Edition
QUIZ
What are the three integrity constraints we have covered? Give examples of how each is used.
NOT NULL
PRIMARY KEY
FOREIGN KEY
©Silberschatz, Korth and Sudarshan3.32Database System Concepts - 6th Edition
QUIZ
True or false:
By default, in SQL a table may contain duplicate tuples.
©Silberschatz, Korth and Sudarshan3.33Database System Concepts - 6th Edition
QUIZ
True or false:
By default, in SQL a table may contain duplicate tuples.
©Silberschatz, Korth and Sudarshan3.34Database System Concepts - 6th Edition
QUIZ
How can we control duplicates in SQL queries?
©Silberschatz, Korth and Sudarshan3.35Database System Concepts - 6th Edition
QUIZ
How can we control duplicates in SQL queries?
©Silberschatz, Korth and Sudarshan3.36Database System Concepts - 6th Edition
Natural Join
Natural join matches tuples with the same values for all common attributes, and retains only one copy of each common column
select *from instructor natural join teaches;
©Silberschatz, Korth and Sudarshan3.37Database System Concepts - 6th Edition
©Silberschatz, Korth and Sudarshan3.38Database System Concepts - 6th Edition
Natural Join Example
List the names of instructors along with the course ID of the courses that they taught.
select name, course_idfrom instructor, teacheswhere instructor.ID = teaches.ID;
select name, course_idfrom instructor natural join teaches;
©Silberschatz, Korth and Sudarshan3.39Database System Concepts - 6th Edition
Diagram for next slide
©Silberschatz, Korth and Sudarshan3.40Database System Concepts - 6th Edition
Beware of unrelated attributes with same name which get equated incorrectly
Example: List the names of instructors along with the the titles of courses that they teach
Incorrect version (makes course.dept_name = instructor.dept_name) select name, title
from instructor natural join teaches natural join course; Correct version
select name, titlefrom instructor natural join teaches, coursewhere teaches.course_id = course.course_id;
Another correct version select name, title
from (instructor natural join teaches)join course using(course_id);
©Silberschatz, Korth and Sudarshan3.41Database System Concepts - 6th Edition
QUIZ: Natural JoinUse the natural join to write queries for the following: Find the names of customers who have (at least) an account Find the names of customers who have an account at the “Uptown” branch Find the names of customers who have a loan at a branch with assets greater
than $1,000,000
EOL 1
©Silberschatz, Korth and Sudarshan3.42Database System Concepts - 6th Edition
QUIZ: SQL DDL
Write the CREATE TABLE for course, based on this example:
CREATE TABLE instructor (ID char(5),name varchar(20) not null,dept_name varchar(20),salary numeric(8,2),primary key (ID),foreign key (dept_name) references department
)
©Silberschatz, Korth and Sudarshan3.43Database System Concepts - 6th Edition
CREATE TABLE course (course_id varchar(8) primary key,title varchar(50),dept_name varchar(20),credits smallint,foreign key (dept_name) references department)
);
Note: Primary key declaration can be combined with attribute declaration as shown.
©Silberschatz, Korth and Sudarshan3.44Database System Concepts - 6th Edition
A table was created in a DB with the following command:
CREATE TABLE circuits (name varchar(15),type varchar(10),nr_gates integer
);
Write commands to• Insert a record for the circuit 74LS04, which contains
six NOT gates.• Display all the records in the table.
QUIZ: SQL DDL
©Silberschatz, Korth and Sudarshan3.45Database System Concepts - 6th Edition
CREATE TABLE circuits (name varchar(15),type varchar(10),nr_gates integer
);
INSERT INTO circuits VALUES ('74LS04', 'NOT', 6);SELECT * FROM circuits;
©Silberschatz, Korth and Sudarshan3.46Database System Concepts - 6th Edition
The Rename Operation
SQL allows renaming relations and attributes using the ASclause:
old-name AS new-name E.g.
SELECT ID, name, salary/12 AS monthly_salaryFROM instructor
The keyword AS is optional and may be omittedinstructor as T ≡ instructor T
©Silberschatz, Korth and Sudarshan3.47Database System Concepts - 6th Edition
QUIZ: Rename
Use RENAME in a query that returns the titles of all courses taught by a department housed in the Science building
Then rewrite the query with a natural join
SELECT titleFROM course as C, department as DWHERE C.dept_name = D.dept.name AND
D.building = ‘Science’
SELECT titleFROM course NATURAL JOIN departmentWHERE department.building = ‘Science’
©Silberschatz, Korth and Sudarshan3.48Database System Concepts - 6th Edition
The Rename Operation
Why is it necessary to rename?
1] To make the query shorter
2] To remove ambiguity when a table is joined with itself.
E.g. Find the names of all instructors who have a higher salary than some instructor in ‘Comp. Sci’:
SELECT DISTINCT T. nameFROM instructor AS T, instructor AS SWHERE T.salary > S.salary AND S.dept_name = ‘Comp. Sci.’
©Silberschatz, Korth and Sudarshan3.49Database System Concepts - 6th Edition
String OperationsSQL includes a string-matching operator for comparisons
on character strings. The operator LIKE uses patterns that are described using two special characters:
percent % → matches any substring.
underscore _ → matches any character.
Unlike keyword names, string patters are case sensitive!
©Silberschatz, Korth and Sudarshan3.50Database System Concepts - 6th Edition
String Operations
Pattern matching examples:
‘Intro%’ matches any string beginning with “Intro”. ‘%Comp%’ matches any string containing “Comp” as a
substring. ‘_ _ _’ matches any string of exactly three characters. ‘_ _ _ %’ matches any string of at least three
characters. '100 \%' ESCAPE '\' matches the string100 %”
Escape character
©Silberschatz, Korth and Sudarshan3.51Database System Concepts - 6th Edition
String Operations
Find the names of all instructors whose name includes the substring “dar”:
SELECT nameNAME instructorWHERE name LIKE '%dar%'
©Silberschatz, Korth and Sudarshan3.52Database System Concepts - 6th Edition
QUIZ
What patterns are matched in each case?
©Silberschatz, Korth and Sudarshan3.53Database System Concepts - 6th Edition
QUIZ: Find the loan number of all loans made in cities that start with ‘S’ and end in ‘ville’:
Hint: Use join and string operations.
©Silberschatz, Korth and Sudarshan3.54Database System Concepts - 6th Edition
String Operations
SQL supports a variety of string operations such as concatenation (using “||”) converting between upper and lower case (both ways) finding string length, extracting substrings, etc.
©Silberschatz, Korth and Sudarshan3.55Database System Concepts - 6th Edition
Ordering the Display of Tuples
List in alphabetic order the names of all instructors: SELECT DISTINCT nameFROM instructorORDER BY name
We may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default.Example: order by name desc
Can sort on multiple attributesExample: order by dept_name, name
©Silberschatz, Korth and Sudarshan3.56Database System Concepts - 6th Edition
WHERE clause predicates
SQL includes a between comparison operatorExample: Find the names of all instructors with salary between $90,000 and $100,000 (inclusive)
select namefrom instructorwhere salary between 90000 and 100000
Tuple comparisonselect name, course_idfrom instructor AS I, teaches AS Twhere (I.ID, dept_name) = (T.ID, ’Biology’)
©Silberschatz, Korth and Sudarshan3.57Database System Concepts - 6th Edition
WHERE Clause Predicates
SQL includes an IN operator Example: Find the names and salaries of all
instructors named ‘Jones’, ‘James’ or ‘Julian’
SELECT name, salaryFROM instructorWHERE salary IN (‘Jones’, ‘James’, ‘Julian’)
©Silberschatz, Korth and Sudarshan3.58Database System Concepts - 6th Edition
Quiz: WHERE Clause Predicates
Write a query to return the names of all students who have exactly 50, 60, 70, or 80 credit hours total Without a WHERE predicate With a WHERE predicate
©Silberschatz, Korth and Sudarshan3.59Database System Concepts - 6th Edition
Quiz: WHERE Clause Predicates
Write a query that uses tuple comparison to return the names of all students who have taken a class in 2014.
Use the previous example:
select name, course_idfrom instructor AS I, teaches AS Twhere (I.ID, dept_name) = (T.ID, ’Biology’)
©Silberschatz, Korth and Sudarshan3.60Database System Concepts - 6th Edition
Write a query that uses tuple comparison to return the names of all students who have taken a class in 2014.
SELECT S.nameFROM student AS S, takes AS TWHERE (T.ID, T.year) = (S.ID, 2014)
©Silberschatz, Korth and Sudarshan3.61Database System Concepts - 6th Edition
Quiz: WHERE Clause Predicates
List all the WHERE options we learned:
©Silberschatz, Korth and Sudarshan3.62Database System Concepts - 6th Edition
QUIZ: String Operations
Find all course names that: Begin with “Comp” End with “Comp” Have the substring “Comp” (anywhere) Have two occurrences of “Comp” Have “comp%_” anywhere
©Silberschatz, Korth and Sudarshan3.63Database System Concepts - 6th Edition
Set Operations
Image source: https://rickscraftofaudit.wordpress.com/2014/10/24/the-joy-of-sets/
Not in text
Remember that union and intersection commute, but difference doesn’t!
Elements that are in A, but not in B
©Silberschatz, Korth and Sudarshan3.64Database System Concepts - 6th Edition
Practice Set Operations
Image source: https://rickscraftofaudit.wordpress.com/2014/10/24/the-joy-of-sets/
Not in text
{w, x, y, z} ∪ {z, a, b} = {w, x, y, z} ∩ {z, a, b} = {w, x, y, z} ∩ {a, b, c} ={w, x, y, z} − {z, a, b} = {z, a, b} − {w, x, y, z} = {w, x, y, z} − {a, b, c} =
©Silberschatz, Korth and Sudarshan3.65Database System Concepts - 6th Edition
Set Operations in SQL
Find courses that ran in Fall 2009 or in Spring 2010
Find courses that ran in Fall 2009 but not in Spring 2010
(select course_id from section where sem = ‘Fall’ and year = 2009)union(select course_id from section where sem = ‘Spring’ and year = 2010)
Find courses that ran in Fall 2009 and in Spring 2010
(select course_id from section where sem = ‘Fall’ and year = 2009)intersect(select course_id from section where sem = ‘Spring’ and year = 2010)
(select course_id from section where sem = ‘Fall’ and year = 2009)except(select course_id from section where sem = ‘Spring’ and year = 2010)
©Silberschatz, Korth and Sudarshan3.66Database System Concepts - 6th Edition
Extra-credit QUIZ
©Silberschatz, Korth and Sudarshan3.67Database System Concepts - 6th Edition
Set Operations
All set operations automatically eliminate duplicates.To retain all duplicates use the corresponding multiset versions union all, intersect all and except all.
Suppose a tuple occurs m times in r and n times in s, then, it occurs:
m + n times in r union all s min(m,n) times in r intersect all s max(0, m – n) times in r except all s
©Silberschatz, Korth and Sudarshan3.68Database System Concepts - 6th EditionImage source: https://rickscraftofaudit.wordpress.com/2014/10/24/the-joy-of-sets/
Not in text
{w, x, y, z} ∪ {z, a, b, b} =
{w, w, x, y, z, z, z} ∩ {z, z, a, b} =
{w, x, y, z, z, z} − {z, a, b} =
{z, a, b} − {w, x, y, z, z, z} =
Practice Set Operations with duplicatesUNION ALL, INTERSECT ALL, EXCEPT ALL
©Silberschatz, Korth and Sudarshan3.69Database System Concepts - 6th Edition
Nice application of set operations!
Algorithm:1. Build two sets:
a) Set A contains all accountsb) Set B contains the accounts whose balance is smaller than
that of some other account2. Take A EXCEPT B
Write a query that returns the account with the maximum balance.
EOL 2
©Silberschatz, Korth and Sudarshan3.70Database System Concepts - 6th Edition
Null Values
It is possible for tuples to have a null value, denoted by NULL, for some of their attributes
NULL signifies an unknown value or that a value does not exist.
The result of any arithmetic expression involving null is nullExample: 5 + NULL returns null
The predicate IS NULL can be used to check for null values.Example: Find all instructors whose salary is null.
SELECT nameFROM instructorWHERE salary IS NULL
©Silberschatz, Korth and Sudarshan3.71Database System Concepts - 6th Edition
Null Values
Important: using = NULL instead of IS NULL does not work, because NULL is never equal to NULL i.e. NULL = NULL is never true!
SELECT nameFROM instructorWHERE salary = NULL
Always returns an empty relation!
OK, then does it mean that NULL = NULL is false?
©Silberschatz, Korth and Sudarshan3.72Database System Concepts - 6th Edition
Null Values and Three-Valued Logic Any comparison with null returns unknown
Example: 5 < null or null <> null or null = null
Three-valued logic using the truth value unknown:OR: (unknown or true) = true,
(unknown or false) = unknown(unknown or unknown) = unknown
AND: (true and unknown) = unknown, (false and unknown) = false,(unknown and unknown) = unknown
NOT: (not unknown) = unknown
©Silberschatz, Korth and Sudarshan3.73Database System Concepts - 6th Edition
QUIZ
Write the truth table for three-valued OR!
©Silberschatz, Korth and Sudarshan3.74Database System Concepts - 6th Edition
©Silberschatz, Korth and Sudarshan3.75Database System Concepts - 6th Edition
Null Values and Three Valued Logic
In a WHERE clause, any predicate is treated as false if it evaluates to unknown.
This means that, in SQL, we prefer to err on the side of caution, retaining only those tuples that surely make the predicate true.
©Silberschatz, Korth and Sudarshan3.76Database System Concepts - 6th Edition
3.7 Aggregate Functions
These functions operate on the set of values of a column in a relation, and return a scalar value
avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values
©Silberschatz, Korth and Sudarshan3.77Database System Concepts - 6th Edition
Examples Find the average salary of instructors in the CS department:
SELECT AVG (salary)FROM instructorWHERE dept_name= ’Comp. Sci.’
Find the total number of instructors who teach a course in the Spring 2010 semester:SELECT COUNT (DISTINCT ID)FROM teachesWHERE semester = ’Spring’ AND year = 2010
Find the number of tuples in course:SELECT COUNT (*)FROM course
Just like in SELECT *, the star means “all attributes”, i.e. the entire tuple.
©Silberschatz, Korth and Sudarshan3.78Database System Concepts - 6th Edition
QUIZ: Aggregate Functions
Write queries to find the following: The highest budget of all departments The lowest number of credit hours of any student
©Silberschatz, Korth and Sudarshan3.79Database System Concepts - 6th Edition
Solutions The highest budget of all departments:
SELECT MAX(budget)FROM department
The lowest number of credit hours of any student:SELECT MIN(tot_cred)FROM student
Food for thought: is it possible to find the name of the dept. with the highest budget?
©Silberschatz, Korth and Sudarshan3.80Database System Concepts - 6th Edition
The GROUP BY clauseFind the average salary of instructors in each department :
SELECT dept_name, AVG (salary)FROM instructorGROUP BY dept_name
©Silberschatz, Korth and Sudarshan3.81Database System Concepts - 6th Edition
All non-aggregated attributes in the SELECT clause must appear in the GROUP BY list:
SELECT dept_name, AVG (salary)FROM instructorGROUP BY dept_name
--incorrectSELECT dept_name, ID, AVG (salary)FROM instructorGROUP BY dept_name
GROUP BY syntax
Can you see why?
©Silberschatz, Korth and Sudarshan3.82Database System Concepts - 6th Edition
Example:Write a query to find the name of the department with the
highest budget. Does this work? Why not?
SELECT dept_name, MAX(budget)FROM department
©Silberschatz, Korth and Sudarshan3.83Database System Concepts - 6th Edition
Example:Write a query to find the name of the department with the
highest budget. Does this work? Why not?
SELECT dept_name, MAX(budget)FROM department
All non-aggregated attributes in the SELECT
clause must appear inthe GROUP BY list!
©Silberschatz, Korth and Sudarshan3.84Database System Concepts - 6th Edition
QUIZ: GROUP BY
Write queries to find the following: The highest budget of a department in each building The lowest number of credit hours of any student in each
department
©Silberschatz, Korth and Sudarshan3.85Database System Concepts - 6th Edition
The highest budget of a department in each building:SELECT building, MAX(budget)FROM departmentGROUP BY building
The lowest number of credit hours of any student in each department:
Solutions
SELECT dept_name, MIN(tot_cred)FROM studentGROUP BY dept_name
©Silberschatz, Korth and Sudarshan3.86Database System Concepts - 6th Edition
Aggregate Functions – HAVING Clause
Find the names and average salaries of all departments whose average salary is greater than 42,000:
Note: predicates in the HAVING clause are applied after the formation of groups whereas predicates in the WHERE clause (if it exists) are applied before.
SELECT dept_name, AVG(salary)FROM instructorGROUP BY dept_nameHAVING AVG(salary) > 42000
©Silberschatz, Korth and Sudarshan3.87Database System Concepts - 6th Edition
QUIZ: Aggregate Functions
Write a query to find the names of the departments whose students have an average total credit of at least 6 credit hours
©Silberschatz, Korth and Sudarshan3.88Database System Concepts - 6th Edition
Write a query to find the names of the departments whose students have an average total credit of at least 6 credit hours:
SELECT department, AVGFROM studentGROUP BY departmentHAVING AVG(tot_cred) >= 6
Solution
©Silberschatz, Korth and Sudarshan3.89Database System Concepts - 6th Edition
The HAVING clause may use aggregate functions different from the ones in SELECT.
However, it cannot use non-aggregated attributes!
HAVING syntax
©Silberschatz, Korth and Sudarshan3.90Database System Concepts - 6th Edition
NULL in aggregatesTo find the total of all salaries:
SELECT SUM (salary )FROM instructor
The above query ignores null amounts Result is null if all amounts are null
All aggregate operations except COUNT(*) ignore tuples with null values on the aggregated attributes.If the attribute has only null values:
count returns the # of tuples all other aggregates return null
In general:
©Silberschatz, Korth and Sudarshan3.91Database System Concepts - 6th Edition
3.8 Nested Subqueries
SQL provides a mechanism for the nesting of subqueries.
A subquery is a select-from-where expression that is nested within another query.
Common uses of subqueries → performing tests for: set membership set comparisons set cardinality.
©Silberschatz, Korth and Sudarshan3.92Database System Concepts - 6th Edition
Nested queries for set membership:IN and NOT IN
Find courses offered in Fall 2009 and in Spring 2010:
Find courses offered in Fall 2009 but not in Spring 2010:
SELECT DISTINCT course_idFROM sectionWHERE semester = ’Fall’ AND year= 2009 AND
course_id IN (SELECT course_idFROM sectionWHERE semester = ’Spring’ AND year = 2010)
SELECT DISTINCT course_idFROM sectionWHERE semester = ’Fall’ AND year= 2009 AND
course_id NOT IN (SELECT course_idFROM sectionWHERE semester = ’Spring’ AND year = 2010)
Outerquery
Inner query
©Silberschatz, Korth and Sudarshan3.93Database System Concepts - 6th Edition
It may seem from the previous examples that (NOT) IN simply reproduces the functionality of existing set operators (INTERSECT, EXCEPT), but this is not so – here’s a task that cannot be achieved without subqueries:
©Silberschatz, Korth and Sudarshan3.94Database System Concepts - 6th Edition
QUIZ: Now we can finally solve this!
Write a query to find the name of the department with the highest budget.
(Hint: Start with the inner query, which find the value of the highest budget!)
©Silberschatz, Korth and Sudarshan3.95Database System Concepts - 6th Edition
Write a query to find the name of the department with the highest budget.
Solution
(SELECT MAX(budget)FROM department)
SELECT dept_nameFROM departmentWHERE budget IN
EOL 3
©Silberschatz, Korth and Sudarshan3.96Database System Concepts - 6th Edition
QUIZ: Write the truth table for three-valued AND
Hint: How many lines does the table have?
©Silberschatz, Korth and Sudarshan3.97Database System Concepts - 6th Edition
solution
Nr. of lines = 32 = 9
©Silberschatz, Korth and Sudarshan3.98Database System Concepts - 6th Edition
QUIZ
In Hardware Description Languages (HDL), a signal can have nine (!) different values; how many lines would a truth table have for the nine-valued AND?
©Silberschatz, Korth and Sudarshan3.99Database System Concepts - 6th Edition
QUIZ
Explain the difference in syntax and behavior between the WHERE and HAVING clauses.
©Silberschatz, Korth and Sudarshan3.100Database System Concepts - 6th Edition
solution
Behavior: Predicates in the HAVING clause are applied afterthe formation of groups whereas predicates in the WHEREclause are applied before.
Syntax: The HAVING clause can only use conditions on aggregated functions whereas the WHERE clause can only use conditions on individual tuples.
The difference in syntax and behavior between the WHERE and HAVING clauses:
©Silberschatz, Korth and Sudarshan3.101Database System Concepts - 6th Edition
QUIZ
Write a query to find the average grade each student made in the Fall of 2016. The query should display two columns: the ID and the average grade.
Hint: Use an aggregate function and the GROUP BY clause.
©Silberschatz, Korth and Sudarshan3.102Database System Concepts - 6th Edition
QUIZ
Write a query to find the average grade each student made in the Fall of 2016. The query should display two columns: the ID and the average grade.
SELECT ID, AVG(grade)FROM takesWHERE semester = 'Fall' AND year = 2016GROUP BY ID
©Silberschatz, Korth and Sudarshan3.103Database System Concepts - 6th Edition
QUIZ
Write a query to find the average grade each student made in the Fall of 2016. The query should display two columns: the ID and the average grade. Only averages 3.0 or higher should be displayed.
Hint: Use the HAVING clause.
©Silberschatz, Korth and Sudarshan3.104Database System Concepts - 6th Edition
QUIZ
SELECT ID, AVG(grade)FROM takesWHERE semester = 'Fall' AND year = 2016GROUP BY IDHAVING AVG(grade) >= 3.0
©Silberschatz, Korth and Sudarshan3.105Database System Concepts - 6th Edition
Extra-credit QUIZ
©Silberschatz, Korth and Sudarshan3.106Database System Concepts - 6th Edition
3.8 Review: Nested Subqueries
Are used for: Determining membership (or lack thereof) in a set:
• IN• NOT IN
Comparing tuples against sets: SOME ALL
©Silberschatz, Korth and Sudarshan3.107Database System Concepts - 6th Edition
SOME example
Find names of instructors with salary greater than that of some (at least one) instructor in the Biology department.
Same query using > some clause
select namefrom instructorwhere salary > SOME(select salary
from instructorwhere dept_name = ’Biology’);
select distinct T.namefrom instructor as T, instructor as Swhere T.salary > S.salary and S.dept_name = ’Biology’;
©Silberschatz, Korth and Sudarshan3.108Database System Concepts - 6th Edition
Formal definition of SOME clause
F <comp> some r ⇔ ∃ t ∈ r such that (F <comp> t )<comp> can be: <, ≤, >, =, ≠
056
(5 < some ) = true
050
) = false
5
05(5 ≠ some ) = true (since 0 ≠ 5)
(read: 5 < some tuple in the relation)
(5 < some
) = true(5 = some
Note well:
©Silberschatz, Korth and Sudarshan3.109Database System Concepts - 6th Edition
ALL example
Find the names of the instructors whose salary is greater than the salary of all instructors in the Biology department.
SELECT nameFROM instructorWHERE salary > ALL (SELECT salary
FROM instructorWHERE dept_name = ’Biology’);
©Silberschatz, Korth and Sudarshan3.110Database System Concepts - 6th Edition
Formal definition of ALL clause
F <comp> all r ⇔ ∀ t ∈ r (F <comp> t)
056
(5 < all ) = false
6104
) = true
5
46(5 ≠ all ) = true (since 5 ≠ 4 and 5 ≠ 6)
(5 < all
) = false(5 = all
Note well:
©Silberschatz, Korth and Sudarshan3.111Database System Concepts - 6th Edition
QUIZ: SOME
Write a nested query to find the departments with budgets lower than some (at least one) departments in the Watson building.
©Silberschatz, Korth and Sudarshan3.112Database System Concepts - 6th Edition
QUIZ: ALL
Write a nested query to find the departments with budgets lower than all departments in the Watson building
©Silberschatz, Korth and Sudarshan3.113Database System Concepts - 6th Edition
3.8 Nested SubqueriesAre used for: Determining membership (or lack thereof) in a set:
IN NOT IN
Comparing a tuple against a set: <comparison> SOME
<comparison> ALL
Testing if a relation (table, set) is empty (or not): NOT EXISTS EXISTS
exists returns true iff the argument subquery is nonempty: exists r ⇔ r ≠ Ø not exists r ⇔ r = Ø
©Silberschatz, Korth and Sudarshan3.114Database System Concepts - 6th Edition
Yet another way to write the query “Find all courses taught in both the Fall 2009 semester and
in the Spring 2010 semester”:
select course_idfrom section as Swhere semester = ’Fall’ and year= 2009 and
EXISTS (select *from section as Twhere semester = ’Spring’ and
year = 2010 and S.course_id = T.course_id);
©Silberschatz, Korth and Sudarshan3.115Database System Concepts - 6th Edition
Yet another way to write the query “Find all courses taught in both the Fall 2009 semester and
in the Spring 2010 semester”:
select course_idfrom section as Swhere semester = ’Fall’ and year= 2009 and
EXISTS (select *from section as Twhere semester = ’Spring’ and
year = 2010 and S.course_id = T.course_id);
Correlated subquery Correlation name or correlation variable
©Silberschatz, Korth and Sudarshan3.116Database System Concepts - 6th Edition
QUIZ: (NOT) EXISTS
Write an SQL expression that is true iff relation (table) A is contained in relation (table) B
©Silberschatz, Korth and Sudarshan3.117Database System Concepts - 6th Edition
QUIZ: (NOT) EXISTS
Write an SQL expression that is true iff relation A is contained in relation B
NOT EXISTS (A EXCEPT B)
©Silberschatz, Korth and Sudarshan3.118Database System Concepts - 6th Edition
Nested SubqueriesAre used for: Determining membership (or lack thereof) in a set:
IN NOT IN
Comparing tuples against sets: <comparison> SOME
<comparison> ALL Testing if a relation is empty (or not):
NOT EXISTS EXISTS
Testing for no duplicate tuples UNIQUE
Note: UNIQUE of an empty set is true!
©Silberschatz, Korth and Sudarshan3.119Database System Concepts - 6th Edition
Test for Absence of Duplicate TuplesFind all courses that were offered once in 2009:
select T.course_idfrom course as Twhere unique (select R.course_id
from section as Rwhere T.course_id= R.course_id
and R.year = 2009);
©Silberschatz, Korth and Sudarshan3.120Database System Concepts - 6th Edition
QUIZ: UNIQUE
Find the names of students who have taken COSC4401 only once.
©Silberschatz, Korth and Sudarshan3.121Database System Concepts - 6th Edition
QUIZ: UNIQUE
Find the names of students who have taken COSC4401 only once.
SELECT S.ID, S.nameFROM student AS SWHERE UNIQUE (
SELECT T.course_idFROM takes AS TWHERE S.ID = T.ID AND
T.course_id = ‘COSC4401’)
©Silberschatz, Korth and Sudarshan3.122Database System Concepts - 6th Edition
QUIZ: UNIQUE
Find the names of students who have taken COSC4401 only once.
EOL 5
SELECT S.ID, S.nameFROM student AS SWHERE UNIQUE (
SELECT T.course_idFROM takes AS TWHERE S.ID = T.ID AND
T.course_id = ‘COSC4401’)
©Silberschatz, Korth and Sudarshan3.123Database System Concepts - 6th Edition
All subqueries we’ve seen so far are in the WHERE clause, i.e. they are used to filter the tuples.SQL allows subqueries in all clauses of the SELECT statement:
SELECT FROM WHERE GROUP BY HAVING ORDER BY
©Silberschatz, Korth and Sudarshan3.124Database System Concepts - 6th Edition
SKIP remainder of 3.8 starting at
3.8.5 Subqueries in the FROM clause
©Silberschatz, Korth and Sudarshan3.125Database System Concepts - 6th Edition
3.9 Modification of the Database
Deletion of tuples from a given relation Insertion of new tuples into a given relation Updating values in some tuples in a given relation
©Silberschatz, Korth and Sudarshan3.126Database System Concepts - 6th Edition
Deletion
Delete all instructorsdelete from instructor
Delete all instructors from the Finance departmentdelete from instructorwhere dept_name= ’Finance’;
Delete all tuples in the instructor relation for those instructors associated with a department located in the Watson building.
delete from instructorwhere dept_name in (select dept_name
from departmentwhere building = ’Watson’);
©Silberschatz, Korth and Sudarshan3.127Database System Concepts - 6th Edition
Deletion
Delete all instructors whose salary is less than the average salary of all instructors
delete from instructorwhere salary< (select avg (salary) from instructor);
Problem: as we delete tuples from deposit, the average salary changes
Solution used in SQL:1. First, compute avg salary and find all tuples to delete2. Next, delete all tuples found above (without recomputing avg or
retesting the tuples)
Aggregate functions in subqueries are not correlated!
©Silberschatz, Korth and Sudarshan3.128Database System Concepts - 6th Edition
Insertion
Add a new tuple to courseinsert into course
values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);
or equivalentlyinsert into course (course_id, title, dept_name, credits)
values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);
Add a new tuple to student with tot_creds set to nullinsert into student
values (’3003’, ’Green’, ’Finance’, null);
©Silberschatz, Korth and Sudarshan3.129Database System Concepts - 6th Edition
Insertion
Add all instructors to the student relation with tot_creds set to 0
insert into studentselect ID, name, dept_name, 0from instructor
The select from where statement is evaluated fully before any of its results are inserted into the relation (otherwise queries like
insert into table1 select * from table1would cause problems, if table1 did not have any primary key defined.
©Silberschatz, Korth and Sudarshan3.130Database System Concepts - 6th Edition
Update
©Silberschatz, Korth and Sudarshan3.131Database System Concepts - 6th Edition
Problem 3.10Employee (employee_name, street, city)Works(employee_name, company_name, salary)Company(company_name, city)Manages(employee_name, manager_name)
Modify the DB so that “Jones” now lives in “Newtown”
©Silberschatz, Korth and Sudarshan3.132Database System Concepts - 6th Edition
Problem 3.10Employee (employee_name, street, city)Works(employee_name, company_name, salary)Company(company_name, city)Manages(employee_name, manager_name)
Modify the DB so that “Jones” now lives in “Newtown”
UPDATE employeeSET city = 'Newtown'WHERE name = 'Jones'
©Silberschatz, Korth and Sudarshan3.133Database System Concepts - 6th Edition
Complex update example
Increase salaries of instructors whose salary is over $100,000 by 3%, and all others receive a 5% raise
Write two update statements:update instructor
set salary = salary * 1.03where salary > 100000;
update instructorset salary = salary * 1.05where salary <= 100000;
The order is important! (Why?) Can be done better using the case statement (next
slide)
©Silberschatz, Korth and Sudarshan3.134Database System Concepts - 6th Edition
CASE statement for conditional updates
update instructorset salary = case
when salary <= 100000 then salary * 1.05else salary * 1.03end
General form:
©Silberschatz, Korth and Sudarshan3.135Database System Concepts - 6th Edition
Updates with Scalar Subqueries
Only works if the nested query returns one value (a.k.a. scalar)!
Is the nested query correlated?Explain in words what the nested query returns!
Database System Concepts, 6th Ed.©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Homework for Ch.3, due Tue, Feb.14:
--End of chapter Practice Exercise: 8--End of chapter Exercises: 11, 12, 16--A table with all SQL clauses from ch.3, each used in an example (OK if example is from text)
EOL 4
©Silberschatz, Korth and Sudarshan3.137Database System Concepts - 6th Edition
Schema for University database
©Silberschatz, Korth and Sudarshan3.138Database System Concepts - 6th Edition
Schema for Bank database (fig.3.19)