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

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
Advertisements

What is a Database By: Cristian Dubon.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 8-1 David M. Kroenke’s Chapter Eight: Database Redesign Database Processing:
Concepts of Database Management Seventh Edition
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Set Functions.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
CS405G: Introduction to Database Systems Final Review.
Microsoft Access 2010 Chapter 7 Using SQL.
LOGICAL DATABASE DESIGN
Introduction to SQL J.-S. Chou Assistant Professor.
Concepts of Database Management, Fifth Edition
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE) Datalog.
The 2 nd Hand Student Book Database Jon Havier High Distinction Assignment, Autumn 2007.
Concepts and Terminology Introduction to Database.
The Carnie_MyCareer Case Study Peter Ebeid Dane Harris Stephanie Ho Chris Zaharia Distinction Assignment, Autumn 2007 Presented by.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
“National Electoral Division Profiles” Case Study Vincent Tang High Distinction Assignment, Autumn 2007.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
Slide 1 Chapter 7 – Part 1 Data Definition Language & Data Manipulation Language.
Using Special Operators (LIKE and IN)
Database Systems Microsoft Access Practical #3 Queries Nos 215.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Introduction to Database using Microsoft Access 2013 Part 7 November 19, 2014.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Computing & Information Sciences Kansas State University Monday, 17 Sep 2007CIS 560: Database System Concepts Lecture 12 of 42 Monday, 18 February 2008.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 5 Other Relational Languages.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Database Principles Autumn 2007 High Distinction Assignment REBEL Sport Online Author: Joseph Diver.
1 MySQL and SQL. 2 Topics  Introducing Relational Databases  Terminology  Managing Databases MySQL and SQL.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Jenny Jirathammakul High Distinction Assignment Database Principles Autumn, 2007 Melbourne City Toyota.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Databases Introduction - concepts. Concepts of Relational Databases.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
MIDTERM REVIEW IST 210 Organization of Data IST210 1.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Teacher Workshop Database Design Pearson Education © 2014.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
The Dance Fever Case Study
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL – Entire Select.
Chapter 4 Summary Query.
Structured Query Language
Contents Preface I Introduction Lesson Objectives I-2
Presentation transcript:

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

Introduction to ZhangLamHoyts The database refers to the website:  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.

ERD Diagram

SQL Queries on a Single Entity Tables

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

Restrict - “Where” statement movieid | movietitle | movieduration | movierating | 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 | MA | 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 | MA | 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

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”

IS NULL  To find all the movies that do not have a durationtime recorded SELECT movietitle, movieduration FROM ZhangLamhoytsmovies WHERE movieduration IS NULL; movietitle | movieduration The Hills Have Eyes | (1 row)

IS NOT NULL  Find all the movies that are rated ‘M’ and have a duration time recorded SELECT movietitle, movieduration FROM ZhangLamhoytsmovies WHERE 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)

IN  Find all the addresses of cinemas in Blacktown, Broadway and Penrith SELECT * FROM zhanglamhoytscinemas WHERE cinemasuburb IN ('Blacktown','Broadway','Penrith'); cinemaid | cinemaaddress | cinemasuburb | 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';

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 | March of the Penguins | 80 mins | G 17 | The Hills Have Eyes | TBC | R18+ (2 rows)  Can also be done by SELECT * FROM zhanglamhoytsmovies WHERE movierating <> ‘PG‘ AND movierating <> ‘M‘ AND movierating <> ‘MA15+';

Ordering Columns  Change the column order from movieid, movietitle, movie rating to movierating, movietitle, movieid SELECT movieid, movietitle, movierating FROM ZhangLamHoytsMovies WHERE movierating = 'G'; movieid | movietitle | movierating | 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

Ordering Rows  Order movies rated ‘M’ from shortest to longest in duration SELECT * FROM ZhangLamHoytsMovies WHERE movierating = ‘M’ ORDER BY movieduration ASC; movieid | movietitle | movieduration | movierating | 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)

Calculating  Convert the duration of movie ‘Eight Below’ from minutes to hours SELECT movietitle, movieduration FROM ZhangLamhoytsmovies WHERE 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)

COUNT(*) and COUNT(X)  Find the total number of cinemas in Chatwood SELECT count(*) FROM Zhanglamhoytscinemas WHERE cinema suburb =‘Chatswood’; count (1 row)  Find the total number of cinemas in the database SELECT count(cinemasuburb) FROM Zhanglamhoytscinemas ; count (1 row)

Built-in Function (AVG) and (SUM)  Calculate the average time of duration of all movies SELECT avg(movieduration) as average FROM Zhanglamhoytsmovies; average (1 row)  Calculate the total time of duration of all movies SELECT sum(movieduration) as sum FROM Zhanglamhoytsmovies; sum (1 row

Built-in Function (MIN) and (MAX)  Find the shortest time of duration for all movies SELECT min(movieduration) as minimum FROM Zhanglamhoytsmovies; minimum (1 row  Find the longest time of duration for all movies SELECT max(movieduration) as maximum FROM Zhanglamhoytsmovies; maximum (1 row)

LIKE using %  Find movies that screen at 1:50pm SELECT * FROM Zhanglamhoytssessiontimes WHERE sessionscreentime LIKE ‘%1:50pm%’; movieid | cinemaid | sessionscreentime | 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

LIKE using _  Find movies with an ‘a’ as its second letter SELECT * FROM Zhanglamhoytsmovies WHERE movietitle LIKE ‘_a%’; movieid | movietitle | movieduration | movierating | Darna Zaroor Hai | 111 | M 4 | Failure to Launch | 96 | M 10 | March of the Penguins | 80 | G 26 | Water | 116 | M (4 rows)

DISTINCT  Find suburbs with cinemas without duplicates SELECT 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)

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

Foreign Keys and Natural Joins

Natural Joins  Show the phone numbers of Chatswood cinemas SELECT cinemais, cinemasuburb, contactphoneno FROM ZhangLamHoytscinemas NATURAL JOIN ZhangLamHoytscontacts WHERE cinemasuburb = ‘Chatswood’; cinemaid | cinemasuburb | contactphoneno | Chatswood | (02) | Chatswood | (02) (2 rows)

Cross Product  Show the phone numbers of Chatswood cinemas SELECT ZhangLamhoytscinemas.cinemaid, cinemasuburb, contactphoneno FROM ZhangLamHoytscinemas, ZhangLamHoytscontacts WHERE ZhangLamhoytscinemas.cinemaid = ZhangLamhoytscontacts.cinemaid and cinemasuburb = 'Chatswood'; cinemaid | cinemasuburb | contactphoneno | Chatswood | (02) | Chatswood | (02) (2 rows)

Foreign Entities and Relationships

One to Many Relationships From ER Diagram... cinemaid | contactphoneno | contactfaxno | contactexpresscode | (02) | (02) | | (02) | (02) | | (02) | (02) | etc...|... |... |... (17 rows) cinemaid | other columns |... 2 |... 3 |......etc... |... (18 rows) Primary KeyForeign Key

Many to Many Relationships  This is a many-to-many relationship between movies and cinemas  Many-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

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 relationships  The many-to-many is eliminated CinemaID foreign key to Cinemas table MovieID foreign key to Movie table

Group By, Sub-Queries and Complex Joins

Group By  Show the number of cinemas in each suburb SELECT 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)

Group By with ‘HAVING’  Show the number of cinemas in each suburb with more than 1 cinema in the suburb SELECT cinemasuburb, count(*) as number FROM ZhangLamHoytscinemas GROUP BY cinemasuburb HAVING count(*) >1; cinemasuburb | number Chatswood | 2 Warrawong | 2 (2 rows

Subqueries  Find a movie that’s duration is less than the average duration of all movies but longer than 100 minutes SELECT movietitle, movieduration FROM zhanglamhoytsmovies WHERE movieduration > 100 and movieduration <( SELECT avg(movieduration) FROM zhanglamhoytsmovies); movietitle | movieduration She's the Man | 106 (1 row)

Subquery (MAX)  Show the movie with the longest duration SELECT movietitle, movieduration FROM ZhangLamHoytsmovies WHERE movieduration = ( SELECT max(movieduration) FROM ZhangLamHoytsmovies); movietitle | movieduration The Da Vinci Code | 153 (1 row)

Subquery (MIN)  Show the movie with the shortest duration SELECT movietitle, movieduration FROM ZhangLamHoytsmovies WHERE movieduration = ( SELECT min(movieduration) FROM ZhangLamHoytsmovies); movietitle | movieduration The Squid and the Whale | 77 (1 row)

Subquery (MAX using ALL)  Show the movie with the longest duration SELECT movietitle, movieduration FROM ZhangLamHoytsmovies WHERE movieduration >= all ( SELECT movieduration FROM ZhangLamHoytsmovies WHERE movieduration >0); movietitle | movieduration The Da Vinci Code | 153 (1 row)

Subquery (MIN using ALL)  Show the movie with the shortest duration SELECT movietitle, movieduration FROM ZhangLamHoytsmovies WHERE movieduration <= all ( SELECT movieduration FROM ZhangLamHoytsmovies WHERE movieduration >0); movietitle | movieduration The Squid and the Whale | 77 (1 row))

Subquery (ANY)  Select the movies that screen during the times of 10:30am, 2:30pm, 6:45pm SELECT * FROM ZhangLamHoytsmovies WHERE movieid = any ( SELECT movieid FROM ZhangLamhoytssessiontimes WHERE sessionscreentime = ‘10:30am, 2:30pm, 6:45pm’); movieid | movietitle | movieduration | movierating | Final Destination 3 | 98 | MA | She's the Man | 106 | PG (2 rows)  This can also be done using ‘IN’

Subquery (IN)  Select the movies that screen during the times of 10:30am, 2:30pm, 6:45pm SELECT * FROM ZhangLamHoytsmovies WHERE movieid in ( SELECT movieid FROM ZhangLamhoytssessiontimes WHERE sessionscreentime = ‘10:30am, 2:30pm, 6:45pm’); movieid | movietitle | movieduration | movierating | Final Destination 3 | 98 | MA | She's the Man | 106 | PG (2 rows)

Left Outer Join  Find movie screening times of movies with a MA15+ rating and duration of over 130mins  Selection using natural join would give an output of: SELECT * FROM ZhangLamhoytsmovies natural join ZhangLamhoytssessiontimes WHERE movierating = ‘MA15+’ and movieduration >130; movieid | movietitle | movieduration | movierating | cinemaid | sessionscreentime | V for Vendetta | 132 | MA15+ | 3 | 1:00pm, 3:45pm, 8:45pm 25 | V for Vendetta | 132 | MA15+ | 9 | 10:15am (2 rows)

Left Outer Join  Find movie screening times of movies with a MA15+ rating and duration of over 130mins  But 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 | 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)

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 ZhangLamhoytscinemas WHERE cinemasuburb like ‘B%’; cinemasuburb | contactphoneno Bankstown | (02) Blacktown | (02) Broadway | (02) (3 rows)

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) Blacktown | (02) Broadway | (02) Belconnen | (4 rows)

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)

Data Integrity with SQL

cinemaid | cinemaaddress | cinemasuburb | 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 | (02) | (02) | | (02) | (02) | | (02) | (02) | | (02) | (02) | | (02) | (02) | |... |... |... (17 rows)) CinemaID foreign key to Cinemas table

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

“CHECK” Constraints CREATE TABLE ZhangLamHoytsPrices (…….., CONSTRAINT FKPCinemaID FOREIGN KEY (CinemaID) REFERENCES ZhangLamHoytsCinemas ON DELETE RESTRICT ON 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

“ON DELETE RESTRICT” - Constraint CREATE TABLE ZhangLamHoytsPrices (…….., CONSTRAINT FKPCinemaID FOREIGN KEY (CinemaID) REFERENCES ZhangLamHoytsCinemas ON DELETE RESTRICT ON 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

“ON UPDATE CASCADE” CREATE TABLE ZhangLamHoytsPrices (…….., CONSTRAINT FKPCinemaID FOREIGN KEY (CinemaID) REFERENCES ZhangLamHoytsCinemas ON DELETE RESTRICT ON 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

Normalization

First Normal Form cinemaid| cinemasuburb| movietitle | 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 | 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

Second Normal Form First Normal Form cinemaid | movieid |cinemasuburb | movietitle | 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 | Bankstown 2 | Blacktown cinemaid| movieid | 3 1 | 7 1 | 11 1 | 13 2 | 3 2 | 4 2 | 5 2 | 7 movieid| movietitle | 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

Third Normal Form cinemaid | cinemaaddress | postcode | cinemasuburb | 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 | Bankstown 2067 | Chatswood 2750 | Penrith cinemaid| cinemaaddress |postcode | Cnr The Mall & Jacob Streets | | Mandarin Centre cnr Albert & Victor Sts | | Westfield Shoppingtown Level 7, 28 Victor Street | | Westfield Penrith Cnr Jane and Riley St | 2750 Second Normal Form Third Normal Form

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)

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