Vous êtes sur la page 1sur 100

COMP5111

by Eric Lo, Hong Kong Polytechnic University 2 Tables and SQL

Expected Outcome
Know

how to use SQL to

Create

a database of tables Manipulate and query a database of tables


Know

all the key terms

Relation Schema Keys


2

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.

schema = relation name and attribute

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

Our Running Example


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)
Underline
A

= 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

Declarative vs. others


Imperative
You

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

Year 1977 2010

Length Genre 124 90 sciFi

StudioName Disney

prod-id p1 p2

Fiction Dreamwork

SQL
Primarily
Data

a query language, for getting information from a database.


Manipulation Language (DML)

But

also includes a part for specifying the database schema


Data

Definition Language (DDL)

10

Creating (Declaring) and Destroying a Relation form is: Simplest


CREATE TABLE <name> ( <list of elements> ); To delete a relation: DROP TABLE <name>;

11

Example: Create Table


CREATE TABLE Movies ( title VARCHAR(100), year INT, length INT, genre VARCHAR(10), studioName VARCHAR(30), prod-id INT );
12

SQL Data Types


Integers Reals Strings NULL DATE TIME

yyyy-mm-dd
DATE 2007-09-30

Example: Example:
13

hh:mm:ss

TIME 15:30:02.5 = two and a half seconds after 3:30PM.

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

Declaring Single-Attribute Keys


Place

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

Declaring MultiAttribute Keys


CREATE TABLE Movies ( title VARCHAR(100), year INT, length INT, genre VARCHAR(10), studioName VARCHAR(30), prod-id INT PRIMARY KEY (title, year) );

16

Who decides which attribute(s) form the key?


Up

to you!

Database

users Database designer Database application developer


Anything
Talk

can help you?

to the end users (the one who pays your salary) ER diagram (sketchbook)
17

PRIMARY KEY vs. UNIQUE


1. 2. 3.

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

SQL to create tables

19

Expected Outcome
Know

how to use SQL to

Create

a database of tables [done!] Manipulate and query a database of tables


Know

all the key terms

Relation

[done!] Schema [done!] Keys [done!]


20

SELECT-FROM-WHERE
SELECT [tablename].desired attributes FROM one or more tables WHERE condition about tuples of the tables

21

What is the result of this query?


SELECT * FROM Movies;

Title Star War Kingless Speech

Year 1977 2010

Length Genre 124 90 sciFi

StudioName Disney

prod-id p1 p2
22

Fiction Dreamwork

What is the result of this query?


SELECT title, year FROM Movies;

Title Star War Kingless Speech

Year 1977 2010

Length Genre 124 90 sciFi

StudioName Disney

prod-id p1 p2
23

Fiction Dreamwork

What is the result of this query?


SELECT title, length*0.16667 AS lengthInHours FROM Movies;
Title Star War Kingless Speech Year 1977 2010 Length Genre 124 90 sciFi StudioName Disney prod-id p1 p2
24

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

What is the result of this query?


SELECT * FROM Movies WHERE studioName =Disney AND year = 1990;
Title Star War Kingless Speech Year 1977 2010 Length Genre 124 90 sciFi StudioName Disney prod-id p1 p2
26

Fiction Dreamwork

What is the result of this query?


SELECT * FROM Movies WHERE studioName =Disney AND year < 1990;
Title Star War Kingless Speech Year 1977 2010 Length Genre 124 90 sciFi StudioName Disney prod-id p1 p2
27

Fiction Dreamwork

What is the result of this query?


SELECT * FROM Movies WHERE studioName LIKED%
Title Star War Kingless Speech Year 1977 2010 Length Genre 124 90 sciFi StudioName Disney prod-id p1 p2
28

Fiction Dreamwork

What is the result of this query?


SELECT * FROM Movies WHERE year <>1977
Title Star War Kingless Speech Year 1977 2010 Length Genre 124 90 sciFi StudioName Disney prod-id p1 p2
29

Fiction Dreamwork

What is the result of this query?


SELECT * FROM Movies WHERE year = 1977 OR length < 100;
Title Star War Kingless Speech Year 1977 2010 Length Genre 124 90 sciFi StudioName Disney prod-id p1 p2
30

Fiction Dreamwork

What is the result of this query?


SELECT title AS name FROM Movies WHERE year = 1977 OR length < 100;
Title Star War Kingless Speech Year 1977 2010 Length Genre 124 90 sciFi StudioName Disney prod-id p1 p2
31

Fiction Dreamwork

Selection in SQL

Quite confusing here:


Projecting some interesting attributes SQL: SELECT clause Selecting some interesting tuples SQL: WHERE clause

In the WHERE clause


Number comparison operator =, <>, <, >, <=, >= String pattern matching LIKE pattern % is a wildcard of a string of arbitrary length _ is a wildcard of a character Support usual arithmetic operators +, -, *,

/
32

What is the result of this query?


SELECT Movies.title FROM Movies WHERE (length+20) < 120
Title Star War Kingless Speech Year 1977 2010 Length Genre 124 90 sciFi StudioName Disney prod-id p1 p2
33

Fiction Dreamwork

Ordering the result


SELECT desired attributes FROM one or more tables WHERE condition about tuples of the tables ORDER BY <list of attributes> [ASC/DESC]

34

What is the result of this query?


SELECT * FROM Movies WHERE length < 220 ORDER BY length
Title Star War Kingless Speech Year 1977 2010 Length Genre 124 90 sciFi StudioName Disney prod-id p1 p2
35

Fiction Dreamwork

What is the result of this query?


SELECT * FROM Movies WHERE length < 220 ORDER BY length, year DESC
Title Star War Kingless Speech Year 1977 2010 Length Genre 124 90 sciFi StudioName Disney prod-id p1 p2
36

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

Definition Language Data Manipulation Language


SELECT-FROM-WHERE JOIN,

UNION, INTERSECTION, DIFFERENCE [Now!] Subqueries DINTINCT Grouping and Aggregation Inserting, Deleting, and Updating Tuples
38

Querying multiple tables


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 title and studio address of the movies
39

Which tables should be involved?


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 title and studio address of the movies
40

Which tables should be involved?


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 title and studio address of the movies
41

Joining the tables


Movies(title, year, length, genre, studio, prod-id) Studio(name, address)
Title Year Length Genre StudioName prod-id

Name DreamWork Disney EricStudio

Address CA NY HK

Star War

1977

124

sciFi

Disney

p1

Kingless Speech

2010

90

Fiction

Dreamwork

p2

Query: Display the title and studio address of the movies


42

Query: Display the title and studio address of the movies

Movies(title, year, length, genre, studio, prod-id) Studio(name, address)


Title Year Length Genre StudioName prod-id Star War 1977 124 sciFi Disney p1

Name DreamWork Disney EricStudio

Address CA NY HK

Kingless Speech

2010

90

Fiction

Dreamwork

p2

SELECT * FROM Movies, Studio


43

Query: Display the title and studio address of the movies

SELECT * FROM Movies, Studio


Year Length Genre StudioName prod-id 1977 124 sciFi Disney p1

Title

Name DreamWork Disney EricStudio

Address CA NY HK

Star War

Kingless Speech

2010

90

Fiction

Dreamwork

p2

Title Star War Star War Star War Kingless Speech Kingless Speech

Year 1977 1977 1977 2010 2010 2010

Length 124 124 124 90 90 90

Genre sciFi sciFi sciFi Fiction Fiction Fiction

StudioName Disney Disney Disney Dreamwork Dreamwork Dreamwork

prod-id p1 p1 p1 p2 p2 p2

Name DreamWork Disney Eric Studio DreamWork Disney Eric Studio

Address CA NY HK CA NY HK

44

Kingless Speech

This is called Cartesian Product (Denoted by a sign)

SELECT * FROM Movies, Studio


Year Length Genre StudioName prod-id 1977 124 sciFi Disney p1

Title

Name DreamWork Disney EricStudio

Address CA NY HK

Star War

Kingless Speech

2010

90

Fiction

Dreamwork

p2

Title Star War Star War Star War Kingless Speech Kingless Speech

Year 1977 1977 1977 2010 2010 2010

Length 124 124 124 90 90 90

Genre sciFi sciFi sciFi Fiction Fiction Fiction

StudioName Disney Disney Disney Dreamwork Dreamwork Dreamwork

prod-id p1 p1 p1 p2 p2 p2

Name DreamWork Disney Eric Studio DreamWork Disney Eric Studio

Address CA NY HK CA NY HK

45

Kingless Speech

This is called Cartesian Product (Denoted by a sign)

SELECT * FROM Movies, Studio


Year Length Genre StudioName prod-id 1977 124 sciFi Disney p1

Title

Name DreamWork Disney EricStudio

Address CA NY HK

Star War

Kingless Speech

2010

90

Fiction

Dreamwork

p2

Is this what you want?


Title Star War Star War Star War Kingless Speech Kingless Speech Year 1977 1977 1977 2010 2010 2010 Length 124 124 124 90 90 90 Genre sciFi sciFi sciFi Fiction Fiction Fiction StudioName Disney Disney Disney Dreamwork Dreamwork Dreamwork prod-id p1 p1 p1 p2 p2 p2

Name DreamWork Disney Eric Studio DreamWork Disney Eric Studio

Address CA NY HK CA NY HK

46

Kingless Speech

SELECT * FROM Movies, Studio


Title Year Length Genre StudioName prod-id

Name DreamWork Disney EricStudio

Address CA NY HK

Star War

1977

124

sciFi

Disney

p1

Kingless Speech

2010

90

Fiction

Dreamwork

p2

Title Star War

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

SELECT * FROM Movies, Studio

Title Star War

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

SELECT * FROM Movies, Studio WHERE Movies.StudioName = Studio.Name

Title Star War

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

SELECT * FROM Movies, Studio WHERE Movies.StudioName = Studio.Name

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

Query: Display the title and studio address of the movies

SELECT Title, Address FROM Movies, Studio WHERE Movies.StudioName = Studio.Name

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

INTERSECT, DIFFERNECE, and UNION


(A

intersect B) = ? (A except B) = ? (A union B) = ?

B
C

65

SQL
Data

Definition Language Data Manipulation Language


SELECT-FROM-WHERE JOIN,

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

subquery that returns a single value

68

Query: Display the producer name of Star Wars

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

Query: Display the producer name of Star Wars

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

query can be expressed by different SQL statements Exam-oriented


Same

question More than one (SQL) answers T_T

71

More subqueries
A

subquery in FROM clause

72

Query: Display the producers of Tony Leungs 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)

73

Query: Display the producers of Tony Leungs 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)
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

Definition Language Data Manipulation Language


SELECT-FROM-WHERE JOIN,

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

preserve duplicates in INTERSECT, EXCEPT, and UNION

(SELECT title, year FROM Movie) UNION ALL (SELECT movietitle AS title, movieYear as year FROM StarIn )

77

SQL
Data

Definition Language Data Manipulation Language


SELECT-FROM-WHERE JOIN,

UNION, INTERSECTION, DIFFERENCE Subqueries DINTINCT Grouping and Aggregation [Now!] Inserting, Deleting, and Updating Tuples
78

Grouping and aggregation


We

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

Grouping and aggregation


For

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

Grouping and aggregation


Without

grouping, the whole relation is one big group


SELECT COUNT(*) FROM Movies;

SELECT AVG(revenue) FROM MovieStaff; SELECT SUM(revenue) FROM MovieStaff;

SELECT MAX(revenue) FROM MovieStaff; SELECT MIN(revenue) FROM MovieStaff;


81

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

FROM MovieStaff, Movies WHERE prod-id = id GROUP BY name;

Filtering some groups


Use

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

Definition Language Data Manipulation Language


SELECT-FROM-WHERE JOIN,

UNION, INTERSECTION, DIFFERENCE Subqueries DINTINCT Grouping and Aggregation Inserting, Deleting, and Updating Tuples [Now!]
89

Insert a new 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)
INSERT INTO Movies VALUES (KungFuPanda2, 2011, 91, Dreamwork, Disney, Jennifer Yuh)

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

Update a stars address


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)
UPDATE MovieStar SET address = HungHom WHERE name = Tony Leung

95

UPDATE
UPDATE R SET <new-value-assignments> WHERE <tuples-to-be-updated>;

96

Declaring Referential Integrity Constraints


Constraints:
Specify

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

Declaring Referential Integrity Constraints


Assert

that a value in one tuple must also appear in another tuple E.g.,
CourseEnroll(course-id,

student-id) Course(couse-id, coursename.) Student(student-id, studentname, )


A

referential integrity constraint on course-id A referential integrity constraint on student-id


98

Specifying constraints in SQL


You

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)

Maintaining Referential Integrity


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)

Vous aimerez peut-être aussi