the zhanglamhoyts case study de wen zhong vincent tang distinction assignment, autumn 2007

55
The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Upload: june-stone

Post on 11-Jan-2016

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

The ZhangLamHoyts Case Study

De Wen ZhongVincent Tang

Distinction Assignment, Autumn 2007

Page 2: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Introduction to ZhangLamHoyts

The database refers to the website:

http://hoyts.ninemsn.com.au/ Holds information about 17 Hoyts cinemas in NSW Cinemas contact details

“express code” – when entered into the phone links the person directly to the associated cinema.

26 movies and their screening times. The different pricing structures:

La Premiere pricing - Exclusive lounge. Mums and Bubs pricing - specialised cinemas where mums are

able to bring babies/toddlers/young children Regular pricing.

Page 3: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

ERD Diagram

Page 4: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

SQL Queries on a

Single Entity Tables

Page 5: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Project – “Select” statement

movietitle | movierating

--------------------------------+------------- American Dreamz | M Darna Zaroor Hai | M Eight Below | PG Failure to Launch | M Final Destination 3 | MA15+ Go For Zucker | M Ice Age 2: The Meltdown | PG In The Mix | M Kokoda | M March of the Penguins | G Mission Impossible 3 | M Scary Movie 4 | M...(26 rows)

Select movietitle, movierating from ZhangLamHoytsMovies;

• Display Movie Title and Classification

•It allows the user to choose which columns to come up

•The order which they come up as

Page 6: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Restrict - “Where” statement

movieid | movietitle | movieduration | movierating---------+--------------------------------+---------------+------------- 1 | American Dreamz | 107 | M 2 | Darna Zaroor Hai | 111 | M 3 | Eight Below | 120 | PG 11 | Mission Impossible 3 | 126 | M 13 | She's the Man | 106 | PG 16 | The Da Vinci Code | 153 | M 18 | The Inside Man | 129 | MA15+ 19 | The New World | 135 | M 23 | The World's Fastest Indian | 121 | PG 24 | Two for the Money | 122 | M 25 | V for Vendetta | 132 | MA15+ 26 | Water | 116 | M 27 | Zodiac | 158 | MA15+(13 rows)

select * from ZhangLamHoytsmovies WHERE movieduration > 100;

• Display Movies where their movie duration is greater than 100minutes

Page 7: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Project and Restrict

movietitle | movierating--------------------------------+------------- American Dreamz | M Darna Zaroor Hai | M Failure to Launch | M Go For Zucker | M In The Mix | M Kokoda | M Mission Impossible 3 | M Scary Movie 4 | M The Da Vinci Code | M The New World | M The Ringer | M Two for the Money | M Water | M(13 rows)

Select movietitle, movierating from ZhangLamHoytsMovies

where movierating = ‘M’;

Not equal to<>

Greater than or equal to

>=

Greater than>

Less than or equal to

<=

Less than<

Equal to=

MeaningOperator

• Display Movie Title and Classification where the classification is “M”

Page 8: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

IS NULL

To find all the movies that do not have a durationtime recordedSELECT movietitle, moviedurationFROM ZhangLamhoytsmoviesWHERE movieduration IS NULL; movietitle | movieduration

--------------------------------+---------------

The Hills Have Eyes |

(1 row)

Page 9: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

IS NOT NULL

Find all the movies that are rated ‘M’ and have a duration time recordedSELECT movietitle, movieduration FROM ZhangLamhoytsmoviesWHERE movierating = ‘PG’ AND movieduration IS NOT NULL; movietitle | movieduration

--------------------------------+---------------

Eight Below | 120

Ice Age 2: The Meltdown | 90

She's the Man | 106

The Benchwarmers | 85

The Shaggy Dog | 98

The World's Fastest Indian | 121

(6 rows)

Page 10: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

IN

Find all the addresses of cinemas in Blacktown, Broadway and PenrithSELECT * FROM zhanglamhoytscinemas WHERE cinemasuburb IN ('Blacktown','Broadway','Penrith');

cinemaid | cinemaaddress | cinemasuburb----------+-------------------------------------------------------------+-------------- 2 | Level 4, Patrick Street Westpoint Marketown Shopping Centre | Blacktown 3 | Broadway Shopping Centre Cnr Greek & Bay Street | Broadway 13 | Westfield Penrith Cnr Jane and Riley St | Penrith(3 rows)

Can also be done by SELECT * FROM zhanglamhoytscinemas WHERE cinemasuburb = 'Blacktown‘ OR cinemasuburb = 'Broadway‘ OR cinemasuburb = 'Penrith';

Page 11: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

NOT IN

To find all movies that do not have the rating of PG, M or MA15+SELECT * FROM zhanglamhoytsmovies WHERE cinemarating NOT IN (‘PG',’M',’MA15+');

movieid | movietitle | movieduration | movierating---------+--------------------------------+-----------------+------------- 10 | March of the Penguins | 80 mins | G 17 | The Hills Have Eyes | TBC | R18+(2 rows)

Can also be done by SELECT * FROM zhanglamhoytsmoviesWHERE movierating <> ‘PG‘ AND movierating <> ‘M‘ AND movierating <> ‘MA15+';

Page 12: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Ordering Columns

Change the column order from movieid, movietitle, movie rating to movierating, movietitle, movieidSELECT movieid, movietitle, movierating FROM ZhangLamHoytsMovies WHERE movierating = 'G'; movieid | movietitle | movierating---------+--------------------------------+------------- 10 | March of the Penguins | G(1 row)

SELECT movierating, movietitle, movieid FROM ZhangLamHoytsMovies WHERE movierating = 'G'; movierating | movietitle | movieid-------------+--------------------------------+--------- G | March of the Penguins | 10(1 row)

Columns are ordered as they appear after the select statement

Page 13: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Ordering Rows

Order movies rated ‘M’ from shortest to longest in durationSELECT * FROM ZhangLamHoytsMovies WHERE movierating = ‘M’ ORDER BY movieduration ASC; movieid | movietitle | movieduration | movierating---------+--------------------------------+---------------+------------- 12 | Scary Movie 4 | 83 | M 6 | Go For Zucker | 91 | M 9 | Kokoda | 92 | M 22 | The Ringer | 94 | M 4 | Failure to Launch | 96 | M 8 | In The Mix | 96 | M 1 | American Dreamz | 107 | M 2 | Darna Zaroor Hai | 111 | M 26 | Water | 116 | M 24 | Two for the Money | 122 | M 11 | Mission Impossible 3 | 126 | M 19 | The New World | 135 | M 16 | The Da Vinci Code | 153 | M(13 rows)

Page 14: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Calculating

Convert the duration of movie ‘Eight Below’ from minutes to hoursSELECT movietitle, movieduration FROM ZhangLamhoytsmoviesWHERE movietitle = ‘Eight Below’; movietitle | movieduration

--------------------------------+---------------

Eight Below | 120

(1 row)

SELECT movietitle, movieduration/60 as hours FROM ZhangLamhoytsmovies WHERE movietitle = ‘Eight Below’; movietitle | hours

--------------------------------+-------

Eight Below | 2

(1 row)

Page 15: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

COUNT(*) and COUNT(X)

Find the total number of cinemas in ChatwoodSELECT count(*) FROM Zhanglamhoytscinemas WHERE cinema suburb =‘Chatswood’; count

-------

2

(1 row)

Find the total number of cinemas in the databaseSELECT count(cinemasuburb) FROM Zhanglamhoytscinemas ; count

-------

17

(1 row)

Page 16: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Built-in Function (AVG) and (SUM)

Calculate the average time of duration of all moviesSELECT avg(movieduration) as average FROM Zhanglamhoytsmovies;average---------------------- 106.1600000000000000(1 row)

Calculate the total time of duration of all moviesSELECT sum(movieduration) as sum FROM Zhanglamhoytsmovies; sum------ 2654(1 row

Page 17: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Built-in Function (MIN)and (MAX)

Find the shortest time of duration for all moviesSELECT min(movieduration) as minimum FROM Zhanglamhoytsmovies;minimum----- 77(1 row

Find the longest time of duration for all moviesSELECT max(movieduration) as maximum FROM Zhanglamhoytsmovies; maximum--------- 153(1 row)

Page 18: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

LIKE using %

Find movies that screen at 1:50pmSELECT * FROM Zhanglamhoytssessiontimes WHERE sessionscreentime LIKE ‘%1:50pm%’;

movieid | cinemaid | sessionscreentime---------+----------+---------------------------------------------------------- 7 | 10 | 11:10am, 12:30pm, 1:50pm, 3:30pm, 7:00pm, 8:40pm, 9:30pm 11 | 10 | 11:10am, 12:30pm, 1:50pm, 3:30pm, 7:00pm, 8:40pm, 9:30pm 26 | 7 | 11:30am, 1:50pm, 4:20pm, 6:50pm, 9:20pm(3 rows

Page 19: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

LIKE using _

Find movies with an ‘a’ as its second letterSELECT * FROM Zhanglamhoytsmovies WHERE movietitle LIKE ‘_a%’;

movieid | movietitle | movieduration | movierating---------+--------------------------------+---------------+------------- 2 | Darna Zaroor Hai | 111 | M 4 | Failure to Launch | 96 | M 10 | March of the Penguins | 80 | G 26 | Water | 116 | M(4 rows)

Page 20: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

DISTINCT

Find suburbs with cinemas without duplicatesSELECT DISTINCT cinemasuburb FROM Zhanglamhoytscinemas; cinemasuburb----------------------- Bankstown Blacktown Broadway Charlestown Chatswood Cinema Paris Eastgardens Entertainment Quarter Erina Merrylands Mt Druitt Penrith Warrawong Warringah Mall Wetherill Park(15 rows)

Page 21: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Inserting Rows

Add a new movie INSERT INTO ZhangLamhoytsmovies VALUES (27, ‘Zodiac’, 158, ‘MA15+’) ;

This can also be done by

INSERT INTO ZhangLamhoytsmovies (MovieID, MovieTitle, MovieDuration, MovieRating) VALUES (27, ‘Zodiac’, 158, ‘MA15+’) ;

Although this would take longer

Page 22: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Foreign Keys and Natural Joins

Page 23: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Natural Joins

Show the phone numbers of Chatswood cinemasSELECT cinemais, cinemasuburb, contactphoneno FROM ZhangLamHoytscinemas NATURAL JOIN ZhangLamHoytscontacts WHERE cinemasuburb = ‘Chatswood’;

cinemaid | cinemasuburb | contactphoneno----------+--------------+---------------- 5 | Chatswood | (02) 9411 8811 6 | Chatswood | (02) 9884 8588(2 rows)

Page 24: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Cross ProductCross Product

Show the phone numbers of Chatswood cinemasSELECT ZhangLamhoytscinemas.cinemaid, cinemasuburb, contactphoneno FROM ZhangLamHoytscinemas, ZhangLamHoytscontacts WHERE ZhangLamhoytscinemas.cinemaid = ZhangLamhoytscontacts.cinemaid and cinemasuburb = 'Chatswood'; cinemaid | cinemasuburb | contactphoneno----------+--------------+---------------- 5 | Chatswood | (02) 9411 8811 6 | Chatswood | (02) 9884 8588(2 rows)

Page 25: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Foreign Entities and Relationships

Page 26: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

One to Many Relationships

From ER Diagram...

cinemaid | contactphoneno | contactfaxno | contactexpresscode---------+----------------+-----------------+------------------- 1 | (02) 9796 4888 | (02) 9796 4115 | 275 2 | (02) 9671 8000 | (02) 9671 8017 | 290 3 | (02) 9211 1911 | (02) 9211 7805 | 284...etc...| ... | ... | ...(17 rows)

cinemaid | other columns----------+---------------- 1 | ... 2 | ... 3 | ... ...etc... | ...(18 rows)

Primary Key Foreign Key

Page 27: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Many to Many Relationships

This is a many-to-many relationship between movies and cinemasMany-to-many relationships are not ideal therefore it should they should be eliminated If the designers of the database were to allow many-to-many relationships it would mean the database has not been normalised

Page 28: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Many to Many Relationships

From the ER Diagram...This shows sessions times used as an associative entity The designer of the database has broken down the relationshipsThe many-to-many is eliminated

CinemaID foreign key to Cinemas table

MovieID foreign key to Movie table

Page 29: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Group By, Sub-Queries and Complex Joins

Page 30: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Group By

Show the number of cinemas in each suburbSELECT cinemasuburb, count(*) as number FROM ZhangLamHoytscinemas GROUP BY cinemasuburb; cinemasuburb | number-----------------------+-------- Erina | 1 Cinema Paris | 1 Warringah Mall | 1 Entertainment Quarter | 1 Chatswood | 2 Mt Druitt | 1 Bankstown | 1 Merrylands | 1 Blacktown | 1 Broadway | 1 Wetherill Park | 1 Warrawong | 2 Penrith | 1 Eastgardens | 1 Charlestown | 1(15 rows)

Page 31: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Group By with ‘HAVING’

Show the number of cinemas in each suburb with more than 1 cinema in the suburbSELECT cinemasuburb, count(*) as number FROM ZhangLamHoytscinemas GROUP BY cinemasuburb HAVING count(*) >1;

cinemasuburb | number--------------+-------- Chatswood | 2 Warrawong | 2(2 rows

Page 32: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Subqueries

Find a movie that’s duration is less than the average duration of all movies but longer than 100 minutesSELECT movietitle, movieduration FROM zhanglamhoytsmovies WHERE movieduration > 100 and movieduration <(

SELECT avg(movieduration) FROM zhanglamhoytsmovies);

movietitle | movieduration

--------------------------------+---------------

She's the Man | 106

(1 row)

Page 33: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Subquery (MAX)

Show the movie with the longest durationSELECT movietitle, movieduration FROM ZhangLamHoytsmoviesWHERE movieduration = (

SELECT max(movieduration) FROM ZhangLamHoytsmovies);

movietitle | movieduration--------------------------------+--------------- The Da Vinci Code | 153(1 row)

Page 34: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Subquery (MIN)

Show the movie with the shortest durationSELECT movietitle, movieduration FROM ZhangLamHoytsmoviesWHERE movieduration = (

SELECT min(movieduration) FROM ZhangLamHoytsmovies);

movietitle | movieduration--------------------------------+--------------- The Squid and the Whale | 77(1 row)

Page 35: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Subquery (MAX using ALL)

Show the movie with the longest durationSELECT movietitle, movieduration FROM ZhangLamHoytsmoviesWHERE movieduration >= all (

SELECT movieduration FROM ZhangLamHoytsmovies WHERE movieduration >0);

movietitle | movieduration--------------------------------+--------------- The Da Vinci Code | 153(1 row)

Page 36: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Subquery (MIN using ALL)

Show the movie with the shortest durationSELECT movietitle, movieduration FROM ZhangLamHoytsmoviesWHERE movieduration <= all (

SELECT movieduration FROM ZhangLamHoytsmovies WHERE movieduration >0);

movietitle | movieduration--------------------------------+--------------- The Squid and the Whale | 77(1 row))

Page 37: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Subquery (ANY)

Select the movies that screen during the times of 10:30am, 2:30pm, 6:45pmSELECT * FROM ZhangLamHoytsmoviesWHERE movieid = any (SELECT movieid FROM ZhangLamhoytssessiontimesWHERE sessionscreentime = ‘10:30am, 2:30pm, 6:45pm’);

movieid | movietitle | movieduration | movierating---------+--------------------------------+---------------+------------- 5 | Final Destination 3 | 98 | MA15+ 13 | She's the Man | 106 | PG(2 rows)

This can also be done using ‘IN’

Page 38: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Subquery (IN)

Select the movies that screen during the times of 10:30am, 2:30pm, 6:45pmSELECT * FROM ZhangLamHoytsmoviesWHERE movieid in (SELECT movieid FROM ZhangLamhoytssessiontimesWHERE sessionscreentime = ‘10:30am, 2:30pm, 6:45pm’);

movieid | movietitle | movieduration | movierating---------+--------------------------------+---------------+------------- 5 | Final Destination 3 | 98 | MA15+ 13 | She's the Man | 106 | PG(2 rows)

Page 39: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Left Outer Join

Find movie screening times of movies with a MA15+ rating and duration of over 130minsSelection using natural join would give an output of:SELECT * FROM ZhangLamhoytsmovies natural join ZhangLamhoytssessiontimesWHERE movierating = ‘MA15+’ and movieduration >130;

movieid | movietitle | movieduration | movierating | cinemaid | sessionscreentime

---------+------------------+---------------+-------------+----------+------------------------

25 | V for Vendetta | 132 | MA15+ | 3 | 1:00pm, 3:45pm, 8:45pm

25 | V for Vendetta | 132 | MA15+ | 9 | 10:15am

(2 rows)

Page 40: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Left Outer Join

Find movie screening times of movies with a MA15+ rating and duration of over 130minsBut using Left Outer join would give an output of:SELECT * FROM ZhangLamhoytsmovies left join ZhangLamhoytssessiontimes using(movieid)WHERE movierating = ‘MA15+’ and movieduration >130;

movieid | movietitle | movieduration | movierating | cinemaid | sessionscreentime

---------+------------------+---------------+-------------+----------+------------------------

25 | V for Vendetta | 132 | MA15+ | 3 | 1:00pm, 3:45pm, 8:45pm

25 | V for Vendetta | 132 | MA15+ | 9 | 10:15am

27 | Zodiac | 158 | MA15+ | |

(3 rows)

Page 41: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Right Outer Join

Find the contacts of cinemas starting with the letter ‘B’Using natural join would give an output of:SELECT cinemasuburb, contactphoneno FROM ZhangLamhoytscontacts natural join ZhangLamhoytscinemasWHERE cinemasuburb like ‘B%’;

cinemasuburb | contactphoneno

--------------+----------------

Bankstown | (02) 9796 4888

Blacktown | (02) 9671 8000

Broadway | (02) 9211 1911

(3 rows)

Page 42: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Right Outer Join

Find the contacts of cinemas starting with the letter ‘B’But using Right Outer join would give an output of:SELECT cinemasuburb, contactphoneno FROM ZhangLamhoytscontacts right join ZhangLamhoytscinemas using(cinemaid)WHERE cinemasuburb like ‘B%’;

cinemasuburb | contactphoneno

--------------+----------------

Bankstown | (02) 9796 4888

Blacktown | (02) 9671 8000

Broadway | (02) 9211 1911

Belconnen |

(4 rows)

Page 43: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Self Join

Find the movies that have the same duration time as the movie ‘Slither’SELECT m2.movietitle FROM zhanglamhoytsmovies m1, zhanglamhoytsmovies m2 WHERE m1.movietitle = 'Slither' and m1.movieduration = m2.movieduration;

movietitle

--------------------------------

Failure to Launch

In The Mix

Slither

(3 rows)

Page 44: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Data Integrity with SQL

Page 45: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

cinemaid | cinemaaddress | cinemasuburb

----------+-------------------------------------------------------------+------------------

1 | Cnr The Mall & Jacob Streets | Bankstown

2 | Level 4, Patrick Street Westpoint Marketown Shopping Centre | Blacktown

3 | Broadway Shopping Centre Cnr Greek & Bay Street | Broadway

4 | 244 Pacific Highway | Charlestown

5 | Mandarin Centre cnr Albert & Victor Sts | Chatswood

... | ... | ...

(17 rows))

Foreign Keys(Cinemas table joined to contacts table through a

foreign key)

cinemaid | contactphoneno | contactfaxno | contactexpresscode

----------+----------------+-----------------+--------------------

1 | (02) 9796 4888 | (02) 9796 4115 | 275

2 | (02) 9671 8000 | (02) 9671 8017 | 290

3 | (02) 9211 1911 | (02) 9211 7805 | 284

4 | (02) 4942 1811 | (02) 4942 1828 | 280

5 | (02) 9411 8811 | (02) 9411 8276 | 272

... | ... | ... | ...

(17 rows))

CinemaID foreign key to Cinemas table

Page 46: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Foreign Keys(Cinemas table joined to contacts table through a

foreign key)

CREATE TABLE ZhangLamHoytsCinemas

(CinemaID INTEGER NOT NULL,

CinemaAddress VARCHAR(100) NOT NULL,

CinemaSuburb VARCHAR(25) NOT NULL,

CONSTRAINT PKCinemaID PRIMARY KEY (CinemaID));

CREATE TABLE ZhangLamHoytsContacts

(CinemaID INTEGER NOT NULL,

ContactPhoneNo VARCHAR(20) NOT NULL,

ContactFaxNo VARCHAR(20),

ContactExpressCode INTEGER,

CONSTRAINT PKContactPhoneNo PRIMARY KEY (CinemaID, ContactPhoneNo),

CONSTRAINT FKCCinemaID FOREIGN KEY (CinemaID)

REFERENCES ZhangLamHoytsCinemas

ON DELETE RESTRICT

ON UPDATE CASCADE

);

Linked by CinemaID with primary key in Cinemas table and foreign key in contacts table

Page 47: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

“CHECK” Constraints

CREATE TABLE ZhangLamHoytsPrices(……..,

CONSTRAINT FKPCinemaID FOREIGN KEY (CinemaID) REFERENCES ZhangLamHoytsCinemas

ON DELETE RESTRICTON UPDATE CASCADE,

CONSTRAINT ValidType CHECK (PriceType = 'Adult' OR PriceType = 'Child' OR PriceType = 'Student' OR PriceType='Pensioner'));

INSERT INTO ZhangLamHoytsPrices VALUES (1, 1, ‘JILL');

psql:ZhangLamHoytsPrices.txt:320: ERROR: new row for relation "zhanglamhoytsprices" violates check constraint "validtype"

Output – When constraint is violated

• Check that the PriceType can only be Adult, Child, Student or Pensioner

Page 48: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

“ON DELETE RESTRICT” - Constraint

CREATE TABLE ZhangLamHoytsPrices(……..,

CONSTRAINT FKPCinemaID FOREIGN KEY (CinemaID) REFERENCES ZhangLamHoytsCinemas

ON DELETE RESTRICTON UPDATE CASCADE,

CONSTRAINT ValidType CHECK (PriceType = 'Adult' OR PriceType = 'Child' OR

PriceType = 'Student' OR PriceType = Pensioner'));

DELETE FROM ZhangLamHoytsCinemas WHERE CinemaID = 1;

ERROR: update or delete on "zhanglamhoytscinemas" violates foreign key constraint “fkpcinemaid" on “zhanglamhoytsprices"

Output – When constraint is violated

• To prevent delete in ZhangLamHoytsCinemas without deleting data from ZhangLamHoytsPrices Table

Page 49: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

“ON UPDATE CASCADE”

CREATE TABLE ZhangLamHoytsPrices(……..,

CONSTRAINT FKPCinemaID FOREIGN KEY (CinemaID) REFERENCES ZhangLamHoytsCinemas

ON DELETE RESTRICTON UPDATE CASCADE,

CONSTRAINT ValidType CHECK (PriceType = 'Adult' OR PriceType = 'Child' OR

PriceType = 'Student' OR PriceType = 'Pensioner'));

• To update other tables along with the ZhangLamHoytsPrices Table

Page 50: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Normalization

Page 51: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

First Normal Form

cinemaid| cinemasuburb| movietitle--------+-------------+-------------------------------------------------------------- 1 | Bankstown | Eight Below, Ice Age 2, Mission Impossible 3, She's the Man 2 | Blacktown | Eight Below, Failure to Launch, Final Destination 3, Ice Age 2

Un-normalized Form

cinemaid | movieid |cinemasuburb | movietitle----------+---------+-------------+------------------- 1 | 3 | Bankstown | Eight Below 1 | 7 | Bankstown | Ice Age 2 1 | 11 | Bankstown | Mission Impossible 3 1 | 13 | Bankstown | She's the Man 2 | 3 | Blacktown | Eight Below 2 | 4 | Blacktown | Failure to Launch 2 | 5 | Blacktown | Final Destination 3 2 | 7 | Blacktown | Ice Age 2

First Normal Form

• Objective - Remove Repeating Groups

Page 52: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Second Normal FormFirst Normal Form

cinemaid | movieid |cinemasuburb | movietitle----------+---------+-------------+------------------- 1 | 3 | Bankstown | Eight Below 1 | 7 | Bankstown | Ice Age 2 1 | 11 | Bankstown | Mission Impossible 3 1 | 13 | Bankstown | She's the Man 2 | 3 | Blacktown | Eight Below 2 | 4 | Blacktown | Failure to Launch 2 | 5 | Blacktown | Final Destination 3 2 | 7 | Blacktown | Ice Age 2

Primary Key

cinemaid| cinemasuburb--------+----------- 1 | Bankstown

2 | Blacktown

cinemaid| movieid ---------+--------- 1 | 3 1 | 7 1 | 11 1 | 13 2 | 3 2 | 4 2 | 5 2 | 7

movieid| movietitle-------+-------------------- 3 | Eight Below 7 | Ice Age 2 11 | Mission Impossible 3 13 | She's the Man 4 | Failure to Launch 5 | Final Destination 3

Solving Second Normal Form

Page 53: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Third Normal Form

cinemaid | cinemaaddress | postcode | cinemasuburb----------+----------------------------------------------------+----------+-------------- 1 | Cnr The Mall & Jacob Streets | 2200 | Bankstown 5 | Mandarin Centre cnr Albert & Victor Sts | 2067 | Chatswood 6 | Westfield Shoppingtown Level 7, 28 Victor Street | 2067 | Chatswood 13 | Westfield Penrith Cnr Jane and Riley St | 2750 | Penrith

postcode | cinemasuburb ---------+-------------- 2200 | Bankstown 2067 | Chatswood 2750 | Penrith

cinemaid| cinemaaddress |postcode---------+--------------------------------------------------+--------- 1 | Cnr The Mall & Jacob Streets | 2200 5 | Mandarin Centre cnr Albert & Victor Sts | 2067 6 | Westfield Shoppingtown Level 7, 28 Victor Street | 2067 13 | Westfield Penrith Cnr Jane and Riley St | 2750

Second Normal Form

Third Normal Form

Page 54: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Creating a “View”

CREATE VIEW BankstownMovies (Title, Rating, Duration)AS SELECT Movietitle, MovieRating, MovieDuration FROM zhanglamhoytsmovies NATURAL JOIN zhanglamhoytssessiontimes NATURAL JOIN zhanglamhoytscinemas WHERE cinemasuburb = 'Bankstown';

• To show all movies from cinemas in suburb of Bankstown

title | rating | duration--------------------------------+--------+---------- Eight Below | PG | 120 Ice Age 2: The Meltdown | PG | 90 Mission Impossible 3 | M | 126 She's the Man | PG | 106 Slither | MA15+ | 96 The Benchwarmers | PG | 85 The Da Vinci Code | M | 153 Two for the Money | M | 122(8 rows)

Page 55: The ZhangLamHoyts Case Study De Wen Zhong Vincent Tang Distinction Assignment, Autumn 2007

Query a “View”

SELECT * from bankstownmovies WHERE duration > 100;

title | rating | duration--------------------------------+--------+---------- Eight Below | PG | 120 Mission Impossible 3 | M | 126 She's the Man | PG | 106 The Da Vinci Code | M | 153 Two for the Money | M | 122(8 rows)

• To search the View table “bankstownmovies” where duration > 100min