Académique Documents
Professionnel Documents
Culture Documents
Expected Outcome
Know
Create
Representing Data
A
set of tables Formally, a set of relations Relational Database Management System (RDBMS) Other methods?
OODBMS ORDBMS XMLDB
3
A Relation is a Table
Attributes (column headers) Title Star War Tuples (rows) Kingless Speech Year 1977 2010 Length 124 90 Genre sciFi Fiction
Relation name
4
Relation Movies
Schemas
Relation
list.
Optionally:
types of attributes. Example: Movie(title, year, length, genre) or Movie(title: string, year: integer, ) No tuples
Database
schema = set of all relation schemas in the database. Database = collection of relations.
5
= key (tuples cannot have the same value in all key attributes).
key is a constraint.
SQL
Structured
Query Language Co-invented by Don Chamberlin Pronounce it Sequel A declarative language for managing relational data
You
say what you want You dont explicitly tell how the database returns the query result
7
Language
explicitly tell what the computer should do Every instructions are actually given you OO Language (e.g., C++, Java) Procedural Language (e.g., C)
Declarative Language
A user query: Give me all the movies (their title only) which were produced before 2000 How to do that? -Up to the specific DBMS product -E.g., Oracle scans the table from the beginning -E.g., SQL Server scans the table from the end -Each DBMS actually invents their best methods to handle the same SQL query -Thats why some DBMS are faster (more expensive) and some are slower Title Star War Kingless Speech
9
StudioName Disney
prod-id p1 p2
Fiction Dreamwork
SQL
Primarily
Data
But
10
11
yyyy-mm-dd
DATE 2007-09-30
Example: Example:
13
hh:mm:ss
Declaring Keys
An
attribute or list of attributes may be declared PRIMARY KEY or UNIQUE. Either says that no two tuples of the relation may agree in all the attribute(s) on the list.
14
PRIMARY KEY or UNIQUE after the type in the declaration of the attribute. Example: CREATE TABLE Studio ( name CHAR(20) UNIQUE, address CHAR(20) );
15
16
to you!
Database
to the end users (the one who pays your salary) ER diagram (sketchbook)
17
There can be only one PRIMARY KEY for a relation, but several UNIQUE attributes. No attribute of a PRIMARY KEY can ever be NULL in any tuple. But attributes declared UNIQUE may have NULLs, and there may be several tuples with NULL.
18
Exercise 2.3.1
Use
19
Expected Outcome
Know
Create
Relation
SELECT-FROM-WHERE
SELECT [tablename].desired attributes FROM one or more tables WHERE condition about tuples of the tables
21
StudioName Disney
prod-id p1 p2
22
Fiction Dreamwork
StudioName Disney
prod-id p1 p2
23
Fiction Dreamwork
Fiction Dreamwork
Projection in SQL
Specifying
attributes in the SELECT clause projects only the selected attributes in the result * means all attributes are of interest AS is a reserved word for renaming
25
Fiction Dreamwork
Fiction Dreamwork
Fiction Dreamwork
Fiction Dreamwork
Fiction Dreamwork
Fiction Dreamwork
Selection in SQL
/
32
Fiction Dreamwork
34
Fiction Dreamwork
Fiction Dreamwork
Exercise
Based
on our running movie database example, write SQL queries to: 1) Find Jim Careys birthdate 2) Find all stars that appeared either in a movie made in 2000 or a movie with Story in the title 3) Find all stars who either are female or live in Hong Kong
37
SQL
Data
UNION, INTERSECTION, DIFFERENCE [Now!] Subqueries DINTINCT Grouping and Aggregation Inserting, Deleting, and Updating Tuples
38
Address CA NY HK
Star War
1977
124
sciFi
Disney
p1
Kingless Speech
2010
90
Fiction
Dreamwork
p2
Address CA NY HK
Kingless Speech
2010
90
Fiction
Dreamwork
p2
Title
Address CA NY HK
Star War
Kingless Speech
2010
90
Fiction
Dreamwork
p2
Title Star War Star War Star War Kingless Speech Kingless Speech
prod-id p1 p1 p1 p2 p2 p2
Address CA NY HK CA NY HK
44
Kingless Speech
Title
Address CA NY HK
Star War
Kingless Speech
2010
90
Fiction
Dreamwork
p2
Title Star War Star War Star War Kingless Speech Kingless Speech
prod-id p1 p1 p1 p2 p2 p2
Address CA NY HK CA NY HK
45
Kingless Speech
Title
Address CA NY HK
Star War
Kingless Speech
2010
90
Fiction
Dreamwork
p2
Address CA NY HK CA NY HK
46
Kingless Speech
Address CA NY HK
Star War
1977
124
sciFi
Disney
p1
Kingless Speech
2010
90
Fiction
Dreamwork
p2
Year 1977
Length 124
Genre sciFi
StudioName Disney
prod-id p1
Name DreamWork
Address CA
Star War
Star War
1977
1977
124
124
sciFi
sciFi
Disney
Disney
p1
p1
Disney
Eric Studio
NY
HK
Kingless Speech
Kingless Speech
2010
2010 2010
90
90 90
Fiction
Dreamwork
Fiction Fiction Dreamwork Dreamwork
p2
p2 p2
DreamWork
Disney Eric Studio
CA
NY HK
47
Kingless Speech
Year 1977
Length 124
Genre sciFi
StudioName Disney
prod-id p1
Name DreamWork
Address CA
Star War
Star War
1977
1977
124
124
sciFi
sciFi
Disney
Disney
p1
p1
Disney
Eric Studio
NY
HK
Kingless Speech
Kingless Speech
2010
2010 2010
90
90 90
Fiction
Dreamwork
Fiction Fiction Dreamwork Dreamwork
p2
p2 p2
DreamWork
Disney Eric Studio
CA
NY HK
48
Kingless Speech
Year 1977
Length 124
Genre sciFi
StudioName Disney
prod-id p1
Name DreamWork
Address CA
Star War
Star War
1977
1977
124
124
sciFi
sciFi
Disney
Disney
p1
p1
Disney
Eric Studio
NY
HK
Kingless Speech
Kingless Speech
2010
2010 2010
90
90 90
Fiction
Dreamwork
Fiction Fiction Dreamwork Dreamwork
p2
p2 p2
DreamWork
Disney Eric Studio
CA
NY HK
49
Kingless Speech
Title
Year
Length
Genre
StudioName
prod-id
Name
Address
Star War
Kingless Speech
1977
2010
124
90
sciFi
Disney
p1
p2
Disney
DreamWork
NY
CA
Fiction
Dreamwork
50
Title
Year
Length
Genre
StudioName
prod-id
Name
Address
Star War
Kingless Speech
1977
2010
124
90
sciFi
Disney
p1
p2
Disney
DreamWork
NY
CA
Fiction
Dreamwork
51
Joining
The
above process is called Joining (two tables) Without specifying , the SQL statement is doing a (stupid) Cartesian Product Movies.StudioName and Studio.Name are called the join keys SELECT * FROM Movies, Studio WHERE Movies.StudioName = Studio.Name
52
Exercise
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address) Query: Display the movie title, the names of the star(s) in the movie, and the birthdate of those star(s)
53
Query: Display the movie title, the names of the star(s) in the movie, and the birthdate of those star(s) Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
SELECT title, starName, birthdate FROM Movies, MovieStar, StarIn WHERE Movies.title = StarIn.movietitle and Movies.year = StarIn.movieYear and StarIn.starName = MovieStar.name
54
Exercise
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address) Query: Display the movie title, the names of the star(s) in the movie, and the birthdate of those star(s), but only show those female star(s)
55
Query: Display the movie title, the names of the star(s) in the movie, and the birthdate of those star(s) Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
SELECT title, starName, birthdate FROM Movies, MovieStar, StarIn WHERE Movies.title = StarIn.movietitle and Movies.year = StarIn.movieYear and StarIn.starName = MovieStar.name and gender = `female
56
Multi-Table Queries
Join
is the most common type of queries when querying multiple tables But some other types do exist
INTERSECTION UNION DIFFERENCE
57
INTERSECTION
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address) Query: Display the names and addresses of all female movie stars who are also movie staffs with revenue over 1,000,000
58
Query: Display the names and addresses of all female movie stars who are also movie staffs with revenue over 1,000,000
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
(SELECT name, address FROM MovieStar WHERE gender = Female ) INTERSECT (SELECT name, address FROM MovieStaff WHERE revenue > 1000000 )
59
We cant intersect two result sets that are in different lists of attributes
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
(SELECT name, address FROM MovieStar WHERE gender = Female ) INTERSECT (SELECT name, address, id FROM MovieStaff WHERE revenue > 1000000 )
ERROR!
60
DIFFERENCE
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address) Query: Display the names and addresses of movie stars who are not movie staffs
61
Query: Display the names and addresses of movie stars who are not movie staffs
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
(SELECT name, address FROM MovieStar ) EXCEPT (SELECT name, address FROM MovieStaff )
62
UNION
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address) Query: Display the names and years of all movies - Given that some movies may record in StarIn only - Very possible Human input error, etc.
63
Query: Display the names and years of all movies (duplicated removed)
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
(SELECT title, year FROM Movie) UNION (SELECT movietitle AS title, movieYear as year FROM StarIn )
64
B
C
65
SQL
Data
UNION, INTERSECTION, DIFFERENCE Subqueries [Now!] DINTINCT Grouping and Aggregation Inserting, Deleting, and Updating Tuples
66
Subqueries
In
SQL, one query can be nested inside another query You have seen some examples already
Subquery 1 (SELECT title, year FROM Movie) UNION (SELECT movietitle AS title, movieYear as year FROM StarIn Subquery 2 )
67
More subqueries
A
68
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
Answer 1: SELECT name FROM Movies, MovieStaff WHERE title =`Star Wars AND prod-id = MovieStaff.id
69
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
Answer 2: SELECT name FROM MovieStaff WHERE id = (SELECT prod-id FROM Movies WHERE title = Star Wars);
70
Caveat
Same
71
More subqueries
A
72
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
73
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
SELECT name FROM MovieStaff, ( SELECT prod-id FROM Movies, StarsIn WHERE title =movietitle AND year = movieYear AND starName = `Tony Leung ) Prod WHERE id = Prod.prod-id
74
SQL
Data
UNION, INTERSECTION, DIFFERENCE Subqueries DINTINCT [Now!] Grouping and Aggregation Inserting, Deleting, and Updating Tuples
75
Eliminating Duplicates
INTERSECT,
EXCEPT, and UNION by default eliminates duplicates Other by default preserves the duplicates To remove duplicates in SELECT: SELECT DISTINCT title FROM Movies
76
Preserving Duplicates
To
(SELECT title, year FROM Movie) UNION ALL (SELECT movietitle AS title, movieYear as year FROM StarIn )
77
SQL
Data
UNION, INTERSECTION, DIFFERENCE Subqueries DINTINCT Grouping and Aggregation [Now!] Inserting, Deleting, and Updating Tuples
78
can group tuples and for each group calculates an aggregated value For example
GROUP
BY movie genre For each movie genre, COUNT how many movies in that group
Sci-fict Love Action 2345 789 1234
79
example
GROUP
BY movie genre For each movie genre, COUNT how many movies in that group
SELECT genre, COUNT(*) FROM Movies GROUP by genre Sci-fict Love Action 2345 789 1234
80
Query: Display the sum of the lengths of all movies for each studio
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
82
Query: Display the sum of the lengths of all movies for each studio
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
SELECT studio, SUM(length) FROM Movies GROUP by studio
83
Exercise
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address) Query: Display a table listing each producers total length of film produced.
84
Exercise
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address) SELECT name, SUM(length) Query: Display a table listing each producers total length of film produced.
85
the HAVING clause to filter some groups The WHERE clause can filter tuples only
86
Query: Display the sum of the lengths of all movies for studios which the sum of the lengths is > 1000 minutes
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
87
Query: Display the sum of the lengths of all movies for studios which the sum of the lengths is > 1000 minutes
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
SELECT studio, SUM(length) FROM Movies GROUP by studio HAVING SUM(length) > 1000
88
SQL
Data
UNION, INTERSECTION, DIFFERENCE Subqueries DINTINCT Grouping and Aggregation Inserting, Deleting, and Updating Tuples [Now!]
89
90
INSERT
INSERT INTO R(A1, A2, , An) VALUES (v1, v2, , vn);
Movies(title, year, length, genre, studio, prod-id)
INSERT INTO Movies VALUES (KungFuPanda2, 2011, 91, Dreamwork, Disney, Jennifer-Yuh) vs. INSERT INTO Movies (year, title, length, genre, prodid, studio) VALUES (2011, KungFuPanda2, 91, Dreamwork, JenniferYuh, Disney)
91
Suppose we want to add the the relation Studio all movies that are mentioned in Movies
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
INSERT INTO Studio (name) SELECT DISTINCT studio FROM Movies WHERE studio NOT IN (SELECT name FROM Studio);
92
If address is not specified, and if that field can be null (i.e., not a primary key), then by default NULL is inserted
Delete a movie
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) Studio(name, address)
DELETE FROM Movies WHERE title like KungFuPanad%
93
DELETE
DELETE FROM R WHERE condition;
94
95
UPDATE
UPDATE R SET <new-value-assignments> WHERE <tuples-to-be-updated>;
96
some constraints on the relation to restrict the data to be stored Primary Key constraint: that attribute value cannot be duplicated (otherwise I cant distinguish two tuples easily) Data type is also a constraint: ensure Gender contains only 0 or 1, but not 0, 1, and 2!
97
that a value in one tuple must also appear in another tuple E.g.,
CourseEnroll(course-id,
know this for PRIMARY KEY To specify referential constraints, we use the keyword REFERENCES in SQL E.g.,
CREATE TABLE Movies ( title , studio VARCHAR(255) REFERENCES Studio(name) );
Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) 99 Studio(name, address)
Now, you cant insert a tuple to Movies with a studio name not exist in Studio table (e.g., NULL is not allowed now) Now, you cant update a tuple in Movies whose studio name is not in Studio table
CREATE TABLE Movies ( title , studio VARCHAR(255) REFERENCES Studio(name) ON DELETE SET NULL / CASCADE ON UPDATE CASCADE ); Movies(title, year, length, genre, studio, prod-id) MovieStar(name, address, gender, birthdate) Or otherwise return ERROR!StarIn(movietitle, movieYear, starName) MovieStaff(name, address, id, revenue) 100 Studio(name, address)