The TicketekCase Study Romana Chandra Jason Murphy Shivam Sinha Distinction Assignment, Autumn 2007
Introduction to the Ticketek Database This database is based on a ticketing system. It contains records of ticketing, events, customers, locations and ticket dispatch As frequent customers to ticketek and fans of comedy, we have decided to create a database that displays comedy events which include show times, locations, members and tickets purchased.
The ERD for Ticketek Database Barcode ShowNo* MemberID* SeatNumber TicketType TicketClass TicketPrice Member MemberID MemFirstName MemLastName UserName Password Shows ShowNo ShowStartTime ShowDate Events EventID EventName EventDescription EventShowLocation ShowNo* LocationID* EventID* Location LocationID VenueName State City
SQL Queries on a Single Entity/ Table
Project (using “select”) Showing the Event ID with its corresponding Event Name SELECT shahticketekeventseventid, shahticketekeventseventname from ShahTicketekEvents; The columns you list between “SELECT” and “FROM” are displayed. Wil Anderson 10 Russell Peters 9 Paresh Rawal 8 Lano and Wooney 7 Faith Prince 6 EuroBeat 5 David Strassman 4 Daniel Kitson 3 Crazy For You 2 Canned Laughter 1 ShahTicketekEventsEventName ShahTicketekEventEventId By the way: SQL “keywords” are not case sensitive (other things are). All SQL commands end with a semi-colon “;”
Restrict (using “where”) Choosing rows A horizontal slice Brisbane QLD Brisbane Convention & Exhibition Centre 13 Melbourne VIC Comedy Theatre 12 Dallas Brooks Centre 11 Sydney NSW WIN Entertainment Centre 10 Regent Theatre 9 Enmore Theatre 8 Newcastle Civic Theatre 7 Sofitel Melbourne 6 The Palms at Crown 5 Revesby Workers Club 4 @ Newtown 3 The Besen Centre 2 Horden Pavillion 1 ShahTicketekLocationCity ShahTicketekLocationState ShahTicketekLocationVenueName ShahTicketekLocationLocationID
Restrict (using “where”) Get all shows who’s location is in NSW Select * from ShahTicketekLocation where shahticketeklocationstate = 'NSW'; Sydney NSW WIN Entertainment Centre 10 Enmore Theatre 8 Newcastle Civic Theatre 7 Revesby Workers Club 4 @Newtown 3 Horden Pavillion 1 ShahTicketekLocationCity ShahTicketekLocationState ShahTicketekLocationVenueName ShahTicketekLocationLocationID
Project and restrict combo You can specify any combination of columns and rows e.g. List the Member ID, show number and Ticket type for show numbers that are < 30. Select ShahTicketekMembersMemberID, ShahTicketekShowsShowNo, ShahTicketekTicketsTicketType from ShahTicketekTickets where ShahTicketekShowsShowNo <30; Adult 1 115 11 114 3 113 12 112 15 110 Concession 29 109 8 108 28 107 Child 24 106 18 105 16 104 2 102 ShahTicketekTicketsTicketType ShahTicketekShowsShowNo ShahTicketekMembersMemberID
Is not Null CREATE Table ShahTicketekLocation ( ShahTicketekLocationLocationID Integer not null, -- A unique number given to each location an event may be held. ShahTicketekLocationVenueName Varchar(100) not null, -- A unique number given to each venue. ShahTicketekLocationState Varchar(5) not null, -- The state in which the show is taking place. ShahTicketekLocationCity Varchar(15) not null, -- The city in which the show is taking place. CONSTRAINT PKLocationID PRIMARY KEY (ShahTicketekLocationLocationID), CONSTRAINT ShahTicketekLocationLocationID CHECK (ShahTicketekLocationLocationID IS NOT NULL), CONSTRAINT ShahTicketekLocationVenueName CHECK (ShahTicketekLocationVenueName IS NOT NULL), CONSTRAINT ShahTicketekLocationState CHECK (ShahTicketekLocationState IS NOT NULL), CONSTRAINT ShahTicketekLocationCity CHECK (ShahTicketekLocationCity IS NOT NULL)
IN Used with a list of values Report data on tickets with classes of Gold and VIP Select * from ShahTicketekTickets where ShahTicketekTicketsTicketClass IN ('Gold', 'VIP'); … is equivalent to … SELECT * FROM ShahTicketekTickets WHERE ShahTicketekTicketsTicketClass = ‘Gold’ OR ShahTicketekTicketsTicketClass = ‘VIP’; 100.00 VIP Adult 52B 1 115 29475 75.00 46C 12 112 75638 70.00 Gold Concession 87 29 109 28873 567 8 108 28483 110.00 4 18 105 89898 49.50 24B 44 100 82084 24A 78473 shahticketekticketsticketprice shahticketektic ketsticketclass shahticketekt icketsticketty pe shahticketekticke tsseatnumber shahticketeksho wsshowno shahticketekme mbersmemberid shahticketekti cketsbarcode
NOT IN Not in a list of values Report data on all tickets except those with TicketClass ‘Silver’ or ‘Bronze’ Select * from ShahTicketekTickets where ShahTicketekTicketsTicketClass NOT IN ('Bronze', 'Silver'); … is equivalent to … SELECT * FROM ShahTicketekTickets WHERE ShahTicketekTicketsTicketClass <> ‘Bronze’ AND ShahTicketekTicketsTicketClass <> ‘Silver’; 100.00 VIP Adult 52B 1 115 29475 75.00 46C 12 112 75638 70.00 Gold Concession 87 29 109 28873 567 8 108 28483 110.00 4 18 105 89898 49.50 24B 44 100 82084 24A 78473 shahticketektick etsticketprice shahticketektick etsticketclass shahticketektick etstickettype shahticketektick etsseatnumber shahticketeksho wsshowno shahticketekme mbersmemberid shahticketektick etsbarcode
Ordering columns Columns are reported in the order specified after “SELECT” in the SQL command Select ShahTicketekLocationLocationID, ShahTicketekLocationState from ShahTicketekLocation where ShahTicketekLocationState ='QLD'; shahticketeklocationlocationid | shahticketeklocationstate --------------------------------+--------------------------- 13 | QLD (1 row) Select ShahTicketekLocationState, ShahTicketekLocationLocationID from ShahTicketekLocation where ShahTicketekLocationState = ‘QLD’; shahticketeklocationstate | shahticketeklocationlocationid ---------------------------+-------------------------------- QLD | 13 (1 row)
Ordering rows: “order by” List all tickets with price of at least 70$ and order the report in descending Ticket Price. Where Ticket Prices are identical, list Ticket Class in alphabetical order. SELECT * FROM ShahTicketekTickets WHERE ShahTicketekTicketsTicketPrice >= 70.00 ORDER BY ShahTicketekTicketsTicketPrice DESC, ShahTicketekTicketsTicketClass; “DESC”is short for “DESCENDING”. 70.00 Gold Concession 87 29 109 28873 Adult 567 8 108 28483 75.00 VIP 46C 12 112 75638 100.00 52B 1 115 29475 110.00 4 18 105 89898 shahticketekticketsticketprice shahticketektic ketsticketclass shahticketektic ketstickettype shahticketektic ketsseatnumb er shahticketeksh owsshowno shahticketekm embersmembe rid shahticketektic ketsbarcode
Calculating Select ShahTicketekMembersMemberID, ShahTicketekTicketsTicketPrice, ShahTicketekTicketsTicketPrice*0.5 as discount from ShahTicketekTickets; shahticketekmembersmemberid | shahticketekticketsticketprice | discount -----------------------------+--------------------------------+---------- 100 | 49.50 | 24.750 102 | 25.00 | 12.500 103 | 67.00 | 33.500 104 | 35.00 | 17.500 105 | 110.00 | 55.000 106 | 25.00 | 12.500 107 | 55.00 | 27.500 108 | 70.00 | 35.000 109 | 70.00 | 35.000 110 | 35.00 | 17.500 111 | 30.00 | 15.000 112 | 75.00 | 37.500 113 | 32.00 | 16.000 114 | 40.00 | 20.000 115 | 100.00 | 50.000 101 | 15.00 | 7.500 (17 rows)
Built-in functions e.g. Find the average price. COUNT, AVG, SUM, MIN, and MAX e.g. Find the average price. SELECT AVG(ShahTicketekTicketsTicketPrice) AS avgprice FROM ShahTicketekTickets; avgprice --------------------- 51.9411764705882353 (1 row)
Built-in functions COUNT(*) – the number of rows e.g. How many members are there? SELECT COUNT(*) AS members FROM ShahTicketekMembers; members --------- 16 (1 row) COUNT(name of a column) – the number of non-null values in that column
Continued….. Determine the total number of rows in the Ticket Events Table Select count(*) from ShahTicketekEvents; count ------- 10 (1 row) How many shows are showing in NSW? Select count(*) from ShahTicketekLocation where ShahTicketekLocationState = ‘NSW’; count ------- 6 (1 row) Note: (*) means ‘The Whole Row’
COUNT Function- Continued…. Give the number of values recorded in the MemberID column. Select count(ShahTicketekMembersMemberID) from ShahTicketekMembers; Note: We do not use (*) this time count ------- 16 (1 row) How many different states have shows recorded in the Location table? Select count(distinct ShahTicketekLocationState) as no_of_states from ShahTicketekLocation; Note: no_of_states has been created as The output heading instead of count by using “AS” no_of_states -------------- 3 (1 row)
Min and Max Functions… Give the price of the cheapest Adult ticket Select min(ShahTicketekTicketsTicketPrice) from ShahTicketekTickets where ShahTicketekTicketsTicketType = ‘Adult’; min ------- 32.00 (1 row) Give the price of the most expensive ticket Select max(ShahTicketekTicketsTicketPrice) from ShahTicketekTickets; max -------- 110.00 (1 row)
AVG and SUM Functions… Give the average price of all tickets Select avg(ShahTicketekTicketsTicketPrice) from ShahTicketekTickets; avg --------------------- 51.9411764705882353 (1 row) Give the total price for Gold and VIP tickets Select sum(ShahTicketekTicketsTicketPrice) from ShahTicketekTickets where ShahTicketekTicketsTicketClass in (‘Gold’, ‘VIP’); sum -------- 524.00 (1 row)
LIKE - Pattern matching List all Shows that has title starting with ‘C’. Select ShahTicketekEventsEventName from ShahTicketekEvents where ShahTicketekEventsEventName like 'C%'; List all Shows containing ‘and’ in their description. SELECT ShahTicketekEventsEventDescription from ShahTicketekEvents WHERE ShahTicketekEventsEventDescription LIKE '%and%'; shahticketekeventseventname ----------------------------- Canned Laughter Crazy For you (2 rows) “%” matches any character, one or more times. shahticketekeventseventdescription ---------------------------------------------------------------------------------------------------------------- A show about leaves, contraryism, hope and heroism. 12 countries go head to head battling in song and dance to win the coveted Eurobeat Award. Musical, physical, hilarious and always surprising. With Asian roots, Russell explores attitudes towards race in a way that is fresh, surprising and hard-hitting. Politics, pop and the banal come together in a brand new show. (5 rows)
LIKE - Pattern matching Find members with ‘h’ as the third letter of their first name. SELECT ShahTicketekMembersMemFirstName FROM ShahTicketekMembers WHERE ShahTicketekMembersMemFirstName LIKE '__h%'; Find members not containing an ‘s’ in their password. SELECT ShahTicketekMembersPassword FROM ShahTicketekMembers WHERE ShahTicketekMembersPassword NOT LIKE '%S%' AND ShahTicketekMembersPassword NOT LIKE '%s%'; shahticketekmembersmemfirstname --------------------------------- John Sahar Johnny (3 rows) “_” matches any character, only once. shahticketekmemberspassword ----------------------------- Lamp JF123 Laden Faraz Alba Beyonce 93456 481644 Kate Thriller DBrockz (11 rows)
DISTINCT Eliminating duplicate rows Report the different venues of the LocationID (“ShahTicketekLocationLocationID”). SELECT DISTINCT ShahTicketekLocationLocationID FROM ShahTicketekEventShowLocation; shahticketeklocationlocationid -------------------------------- 1 2 3 4 5 6 7 8 9 10 11 12 13 (13 rows)
DISTINCT Eliminating duplicate rows Find the number of different Location ID’s. SELECT COUNT(DISTINCT ShahTicketekLocationLocationID) AS Different_Location_ID FROM ShahTicketekEventShowLocation; different_location_id ----------------------- 13 (1 row)
Inserting rows INSERT INTO ShahTicketekTickets VALUES (78473, 100, 44, '24A', 'Adult', 'Gold', 49.50); INSERT INTO ShahTicketekTickets VALUES (82084, 100, 44, '24B', 'Adult', 'Gold', 49.50); INSERT INTO ShahTicketekTickets VALUES (29495, 102, 2, '68', 'Concession', 'Bronze', 25.00); INSERT INTO ShahTicketekTickets VALUES (12345, 103, 34, '133', 'Adult', 'Silver', 67.00); INSERT INTO ShahTicketekTickets VALUES (98765, 104, 16, '60Z', 'Concession', 'Bronze', 35.00); INSERT INTO ShahTicketekTickets VALUES (89898, 105, 18, '4', 'Concession', 'VIP', 110.00); INSERT INTO ShahTicketekTickets VALUES (34462, 106, 24, '55D', 'Child', 'Bronze', 25.00); INSERT INTO ShahTicketekTickets VALUES (39482, 107, 28, '8A', 'Adult', 'Bronze', 55.00); INSERT INTO ShahTicketekTickets VALUES (28483, 108, 8, '567', 'Adult', 'Gold', 70.00); INSERT INTO ShahTicketekTickets VALUES (28873, 109, 29, '87', 'Concession', 'Gold', 70.00); INSERT INTO ShahTicketekTickets VALUES (12093, 110, 15, '33', 'Adult', 'Bronze', 35.00); INSERT INTO ShahTicketekTickets VALUES (14856, 111, 49, '16D', 'Concession', 'Silver', 30.00); INSERT INTO ShahTicketekTickets VALUES (75638, 112, 12, '46C', 'Adult', 'VIP', 75.00); INSERT INTO ShahTicketekTickets VALUES (29374, 113, 3, '99X', 'Adult', 'Bronze', 32.00); INSERT INTO ShahTicketekTickets VALUES (92837, 114, 11, '456', 'Adult','Silver', 40.00); INSERT INTO ShahTicketekTickets VALUES (29475, 115, 1, '52B', 'Adult', 'VIP', 100.00); INSERT INTO ShahTicketekTickets VALUES (12745, 101, 43, '63Z', 'Child', 'Bronze', 15.00);
Foreign Keys and Natural Joins
Foreign keys Primary key Location, Event shahticketeklocationlocationid | shahticketeklocationvenuename | shahticketeklocationstate | shahticketeklocationcity --------------------------------+-----------------------------------------+---------------------------+-------------------------- 1 | Hordern Pavilion | NSW | Sydney 2 | The Besen Centre | VIC | Melbourne 3 | @Newtown | NSW | Sydney 4 | Revesby Workers Club | NSW | Sydney 5 | The Palms at Crown | VIC | Melbourne 6 | Sofitel Melbourne | VIC | Melbourne 7 | Civic Theatre | NSW | Newcastle 8 | Enmore Theatre | NSW | Sydney 9 | Regent Theatre | VIC | Melbourne 10 | WIN Entertainment Centre | NSW | Sydney 11 | Dallas Brooks Centre | VIC | Melbourne 12 | Comedy Theatre | VIC | Melbourne 13 | Brisbane Convention & Exhibition Centre| QLD | Brisbane Location, Event Foreign keys shahticketekeventseventid | shahticketekshowsshowno | shahticketeklocationlocationid ---------------------------+-------------------------+-------------------------------- 1 | 1 | 1 2 | 2 | 2 2 | 3 | 2 2 | 4 | 2 2 | 5 | 2 2 | 6 | 2 2 | 7 | 2 2 | 8 | 2 …| … | … Primary key
The Natural Join (1) Create a new table from two existing tables by matching on common columns select * from ShahTicketekEvents natural join ShahTicketekEventShowLocation where shahticketekeventseventname='Wil Anderson'; eventid | eventname | eventdescription | sshowno | locationid ------------------+-----------------------------+----------------------------------------------------------------+-------------------------+-------------------------------- 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | 47 | 13 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | 48 | 3 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | 49 | 3 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | 50 | 3 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | 51 | 3 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | 52 | 3
The Natural Join (2) select * from ShahTicketekEvents, ShahTicketekEventShowLocation where ShahTicketekEvents. ShahTicketekEventsEventID =ShahTicketekEventShowLocation. ShahTicketekEventsEventID and shahticketekeventseventname='Wil Anderson'; eventid | eventname | eventdescription | sshowno | locationid ------------------+-----------------------------+----------------------------------------------------------------+-------------------------+-------------------------------- 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | 47 | 13 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | 48 | 3 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | 49 | 3 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | 50 | 3 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | 51 | 3 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | 52 | 3
The Natural Join (2) . The same thing as using “natural join” on the previous slide, but using the alternate (“cross product”) notation where ShahTicketekEvents. ShahTicketekEventsEventID =ShahTicketekEventShowLocation. ShahTicketekEventsEventID Notice the use of the dot ... a table name, followed by “.”, followed by the name of a column in the table. This disambiguates which column we mean.
Entities and Relationships
Tickets − Mission m:1 relationship Barcode ShowNo* MemberID* SeatNumber TicketType TicketClass TicketPrice Member MemberID MemFirstName MemLastName UserName Password Shows ShowNo ShowStartTime ShowDate The table Tickets Member Id ShowNo Barcode OtherColu mns
Group by, sub-queries and complex joins
Shows and Tickets showno | starttime | showdate -------------------------+--------------------------------+--------------------------- 1 | 19:30 | 16/06/2006 2 | 20:00 | 20/05/2006 3 | 14:00 | 21/05/2006 4 | 20:00 | 23/05/2006 5 | 20:00 | 24/05/2006 6 | 20:00 | 25/05/2006 7 | 20:00 | 26/05/2006 8 | 20:00 | 27/05/2006 9 | 20:00 | 20/05/2006 10 | 19:00 | 21/05/2006 11 | 18:30 | 16/06/2006 12 | 21:00 | 16/06/2006 13 | 20:00 | 20/05/2006 14 | 13:00 | 21/05/2006 15 | 20:00 | 24/05/2006 16 | 20:00 | 25/05/2006 17 | 20:00 | 26/05/2006 18 | 14:00 | 27/05/2006 19 | 20:00 | 27/05/2006 20 | 13:00 | 28/05/2006 barcode | memberid | showno | seatnumber | tickettype | ticketclass | ticketprice ----------+-----------------------------+-----------------+-------------------------------------------------- 78473 | 100 | 44 | 24A | Adult | Gold | 49.50 82084 | 100 | 44 | 24B | Adult | Gold | 49.50 29495 | 102 | 2 | 68 | Concession | Bronze | 25.00 12345 | 103 | 34 | 133 | Adult | Silver | 67.00 98765 | 104 | 16 | 60Z | Concession | Bronze | 35.00 89898 | 105 | 18 | 4 | Concession | VIP | 110.00 34462 | 106 | 24 | 55 | Child | Bronze | 25.00 39482 | 107 | 28 | 8A | Adult | Bronze
GROUP BY shahticketekshowsshowdate | avgprice Report the Avg Price of an Adult Ticket on all of the Events on a Particular Day select ShahTicketekShowsShowDate, AVG(ShahTicketekTicketsTicketPrice) AS AvgPrice from ShahTicketekShows natural join ShahTicketekTickets where ShahTicketekTicketsTicketType='Adult' group by ShahTicketekShowsShowDate; shahticketekshowsshowdate | avgprice -----------------------------------+--------------------- 20/05/2006 | 67.0000000000000000 10/07/2006 | 49.5000000000000000 16/06/2006 | 71.6666666666666667 21/05/2006 | 32.0000000000000000 27/05/2006 | 70.0000000000000000 24/05/2006 | 35.0000000000000000 9/06/2006 | 55.0000000000000000
HAVING – like WHERE, but after the grouping Report the Avg Price of an Adult Ticket on all of the Events on a Particular Day, where the shows happen more than once a day select ShahTicketekShowsShowDate, AVG(ShahTicketekTicketsTicketPrice) AS AvgPrice from ShahTicketekShows natural join ShahTicketekTickets where ShahTicketekTicketsTicketType='Adult' group by ShahTicketekShowsShowDate HAVING COUNT (*) >1; shahticketekshowsshowdate | avgprice ------------------------------------+--------------------- 10/07/2006 | 49.5000000000000000 16/06/2006 | 71.6666666666666667
Subqueries max -------- 110.00 A query within a query Show the Maximum price for VIP Class Tickets select ShahTicketekEventsEventName, ShahTicketekTicketsTicketPrice from ShahTicketekEvents natural join ShahTicketekTickets where ShahTicketekTicketsTicketPrice >= ( Select max(ShahTicketekTicketsTicketPrice) from ShahTicketekTickets where ShahTicketekTicketsTicketClass ='VIP'); max -------- 110.00 shahticketekeventseventname | shahticketekticketsticketprice -----------------------------+-------------------------------- Canned Laughter | 110.00 Crazy For you | 110.00 Daniel Kitson | 110.00 David Strassman | 110.00 EuroBeat | 110.00 Faith Prince | 110.00 Lano and Woodley | 110.00 Paresh Rawal | 110.00 Russell Peters | 110.00 Wil Anderson | 110.00
Using subqueries to find the maximum Show the Maximum price for VIP Class Tickets select ShahTicketekEventsEventName, ShahTicketekTicketsTicketPrice from ShahTicketekEvents natural join ShahTicketekTickets where ShahTicketekTicketsTicketPrice = ( Select max(ShahTicketekTicketsTicketPrice) from ShahTicketekTickets where ShahTicketekTicketsTicketClass ='VIP'); max -------- 110.00 shahticketekeventseventname | shahticketekticketsticketprice -----------------------------+-------------------------------- Canned Laughter | 110.00 Crazy For you | 110.00 Daniel Kitson | 110.00 David Strassman | 110.00 EuroBeat | 110.00 Faith Prince | 110.00 Lano and Woodley | 110.00 Paresh Rawal | 110.00 Russell Peters | 110.00 Wil Anderson | 110.00
Using subqueries to find the minimum Show the Minimum price for VIP Class Tickets select ShahTicketekEventsEventName, ShahTicketekTicketsTicketPrice from ShahTicketekEvents natural join ShahTicketekTickets where ShahTicketekTicketsTicketPrice = ( Select min(ShahTicketekTicketsTicketPrice) from ShahTicketekTickets where ShahTicketekTicketsTicketClass ='VIP'); min ------- 75.00 shahticketekeventseventname | shahticketekticketsticketprice -----------------------------+-------------------------------- Canned Laughter | 75.00 Crazy For you | 75.00 Daniel Kitson | 75.00 David Strassman | 75.00 EuroBeat | 75.00 Faith Prince | 75.00 Lano and Woodley | 75.00 Paresh Rawal | 75.00 Russell Peters | 75.00 Wil Anderson | 75.00
Alternate way to find the maximum “ALL” Show the Maximum price for all VIP Class Tickets select ShahTicketekEventsEventName, ShahTicketekTicketsTicketPrice from ShahTicketekEvents natural join ShahTicketekTickets where ShahTicketekTicketsTicketPrice >= ALL ( Select ShahTicketekTicketsTicketPrice from ShahTicketekTickets where ShahTicketekTicketsTicketClass ='VIP'); shahticketekticketsticketprice -------------------------------- 110.00 75.00 100.00 shahticketekeventseventname | shahticketekticketsticketprice -----------------------------+-------------------------------- Canned Laughter | 110.00 Crazy For you | 110.00 Daniel Kitson | 110.00 David Strassman | 110.00 EuroBeat | 110.00 Faith Prince | 110.00 Lano and Woodley | 110.00 Paresh Rawal | 110.00 Russell Peters | 110.00 Wil Anderson | 110.00
Alternate way to find the minimum: “ALL” Show the Minimum price for all VIP Class Tickets select ShahTicketekEventsEventName, ShahTicketekTicketsTicketPrice from ShahTicketekEvents natural join ShahTicketekTickets where ShahTicketekTicketsTicketPrice <= ALL( Select ShahTicketekTicketsTicketPrice from ShahTicketekTickets where ShahTicketekTicketsTicketClass ='VIP') and ShahTicketekTicketsTicketClass = 'VIP' ; shahticketekticketsticketprice -------------------------------- 110.00 75.00 100.00 shahticketekeventseventname | shahticketekticketsticketprice -----------------------------+-------------------------------- Canned Laughter | 75.00 Crazy For you | 75.00 Daniel Kitson | 75.00 David Strassman | 75.00 EuroBeat | 75.00 Faith Prince | 75.00 Lano and Woodley | 75.00 Paresh Rawal | 75.00 Russell Peters | 75.00 Wil Anderson | 75.00
Alternate way to find the Maximum “ANY” Show the Minimum price for ANY VIP Class Tickets select ShahTicketekEventsEventName, ShahTicketekTicketsTicketPrice from ShahTicketekEvents natural join ShahTicketekTickets where ShahTicketekTicketsTicketPrice > ANY ( select ShahTicketekTicketsTicketPrice from ShahTicketekTickets where ShahTicketekTicketsTicketClass ='VIP') and ShahTicketekTicketsTicketClass ='VIP'; shahticketekticketsticketprice -------------------------------- 110.00 75.00 100.00 shahticketekeventseventname | shahticketekticketsticketprice -----------------------------+-------------------------------- Canned Laughter | 110.00 Crazy For you | 110.00 Daniel Kitson | 110.00 David Strassman | 110.00 EuroBeat | 110.00 Faith Prince | 110.00 Lano and Woodley | 110.00 Paresh Rawal | 110.00 Russell Peters | 110.00 Wil Anderson | 110.00
Alternate way to find the minimum: “ANY” Show the Minimum price for any VIP Class Tickets select ShahTicketekEventsEventName, ShahTicketekTicketsTicketPrice from ShahTicketekEvents natural join ShahTicketekTickets where ShahTicketekTicketsTicketPrice < ANY( Select ShahTicketekTicketsTicketPrice from ShahTicketekTickets where ShahTicketekTicketsTicketClass ='VIP') and ShahTicketekTicketsTicketClass = 'VIP' ; shahticketekticketsticketprice -------------------------------- 110.00 75.00 100.00 shahticketekeventseventname | shahticketekticketsticketprice -----------------------------+-------------------------------- Canned Laughter | 75.00 Crazy For you | 75.00 Daniel Kitson | 75.00 David Strassman | 75.00 EuroBeat | 75.00 Faith Prince | 75.00 Lano and Woodley | 75.00 Paresh Rawal | 75.00 Russell Peters | 75.00 Wil Anderson | 75.00
Alternate way to find the Maximum “IN” Show the Minimum price for ANY VIP Class Tickets select ShahTicketekEventsEventName, ShahTicketekTicketsTicketPrice from ShahTicketekEvents natural join ShahTicketekTickets where ShahTicketekTicketsTicketPrice > IN ( select ShahTicketekTicketsTicketPrice from ShahTicketekTickets where ShahTicketekTicketsTicketClass ='VIP') and ShahTicketekTicketsTicketClass ='VIP'; ?? shahticketekticketsticketprice -------------------------------- 110.00 75.00 100.00 shahticketekeventseventname | shahticketekticketsticketprice -----------------------------+-------------------------------- Canned Laughter | 110.00 Crazy For you | 110.00 Daniel Kitson | 110.00 David Strassman | 110.00 EuroBeat | 110.00 Faith Prince | 110.00 Lano and Woodley | 110.00 Paresh Rawal | 110.00 Russell Peters | 110.00 Wil Anderson | 110.00
Alternate way to find the Minimum “IN” Show the Minimum price for ANY VIP Class Tickets select ShahTicketekEventsEventName, ShahTicketekTicketsTicketPrice from ShahTicketekEvents natural join ShahTicketekTickets where ShahTicketekTicketsTicketPrice > IN ( select ShahTicketekTicketsTicketPrice from ShahTicketekTickets where ShahTicketekTicketsTicketClass ='VIP') and ShahTicketekTicketsTicketClass ='VIP'; ?? shahticketekticketsticketprice -------------------------------- 110.00 75.00 100.00 shahticketekeventseventname | shahticketekticketsticketprice -----------------------------+-------------------------------- Canned Laughter | 110.00 Crazy For you | 110.00 Daniel Kitson | 110.00 David Strassman | 110.00 EuroBeat | 110.00 Faith Prince | 110.00 Lano and Woodley | 110.00 Paresh Rawal | 110.00 Russell Peters | 110.00 Wil Anderson | 110.00 Self-Join: a join between a table and itself (two copies of the same table). Self- joins are useful for finding relationships among rows of the same table. Problems involving self-referencing (unary) relationships are part of tree- structured queries. In tree-structured queries, a table can be visualized as a structure such as a tree or hierarchy. For example, the Faculty table has a structure showing an organization hierarchy. At the top, the college dean resides. At the bottom, faculty members without subordinates reside. Similar structures apply to the chart of accounts in accounting systems, part structures in manufacturing systems, and route networks in transportation systems. A more difficult problem than a self-join is to find all subordinates (direct or indirect) in an organization hierarchy. This problem can be solved in SQL if the number of subordinate levels is known. One join for each subordinate level is needed. Without knowing the number of subordinate levels, this problem cannot be done in SQL2 although it can be solved in SQL3 and with proprietary extensions of SQL2. In SQL2, tree-structured queries can be solved by using SQL inside a programming language.
Left Outer join The purpose of an outer join is to include non-matching rows, and the outer join returns these missing columns as NULL values. A natural join plus those rows from t1 not included in the natural join select * from ShahTicketekEventShowLocation left join ShahTicketekLocation using (ShahTicketekLocationLocationID); locationid | venuename | locationstate | locationcity --------------------------------+-----------------------------------------+------ 1 | Hordern Pavilion | NSW | Sydney 2 | The Besen Centre | VIC | Melbourne 3 | @Newtown | NSW | Sydney 4 | Revesby Workers Club | NSW | Sydney 5 | The Palms at Crown | VIC | Melbourne 6 | Sofitel Melbourne | VIC | Melbourne 7 | Civic Theatre | NSW | Newcastle eventid | showno | locationid -----------------------+-------------------------+-------------------------------- 1 | 1 | 1 2 | 2 | 2 2 | 3 | 2 2 | 4 | 2 2 | 5 | 2 2 | 6 | 2 2 | 7 | 2 2 | 8 | 2
Left Outer join locationid | tseventid | showno | venuename | shahticketeklocationstate | locationcity -----------------------+---------------------------+-------------------------+-----------------------------------------+---------------------------+ 1 | 1 | 1| Hordern Pavilion | NSW | Sydney 2 | 2 | 2 | The Besen Centre | VIC | Melbourne 2 | 2 | 3 | The Besen Centre | VIC | Melbourne 2 | 2 | 4 | The Besen Centre | VIC | Melbourne 2 | 2 | 5 | The Besen Centre | VIC | Melbourne 2 | 2 | 6 | The Besen Centre | VIC | Melbourne 2 | 2 | 7 | The Besen Centre | VIC | Melbourne 2 | 2 | 8 | The Besen Centre | VIC | Melbourne 3 | 3 | 9 | @Newtown | NSW | Sydney
Right Outer join A natural join plus those rows from t2 not included in the natural join select * from ShahTicketekEventShowLocation right join ShahTicketekLocation using (ShahTicketekLocationLocationID);
Right Outer join locationid | eventid | showno | venuename | locationstate | locationcity --------------------------------+---------------------------+-------------------------+-------- 1 | 1 | 1 | Hordern Pavilion | NSW | Sydney 2 | 2 | 2 | The Besen Center | VIC | Melbourne 2 | 2 | 3 | The Besen Center | VIC | Melbourne 2 | 2 | 4 | The Besen Centre | VIC | Melbourne 2 | 2 | 5 | The Besen Centre | VIC | Melbourne 2 | 2 | 6 | The Besen Centre | VIC | Melbourne 2 | 2 | 7 | The Besen Centre | VIC | Melbourne 2 | 2 | 8 | The Besen Centre| VIC | Melbourne 3 | 3 | 9 | @Newtown | NSW | Sydney 3 | 3 | 10 | @Newtown | NSW | Sydney 3 | 6 | 26 | @Newtown | NSW | Sydney 3 | 6 | 27 | @Newtown | NSW | Sydney 3 | 6 | 28 | @Newtown | NSW | Sydney 3 | 10 | 48 | @Newtown | NSW
Self-Join Join a table to itself Usually involve a self-referencing relationship Useful to find relationships among rows of the same table
Querying a recursive relationship Find the Minimum Cost for A ticket for show for all ticket Types SELECT t1. ShahTicketekShowsShowNo ,t1.ShahTicketekTicketsTicketPrice, t1.ShahTicketekTicketsTicketType, t1.ShahTicketekTicketsTicketClass FROM ShahTicketekTickets t1, ShahTicketekTickets t2 WHERE t1.ShahTicketekTicketsTicketType = t2.ShahTicketekTicketsTicketType AND t1.ShahTicketekTicketsTicketPrice < t2.ShahTicketekTicketsTicketPrice Order By t1. ShahTicketekShowsShowNo; shahticketekshowsshowno | shahticketekticketsticketprice | shahticketekticketstickettype | shahticketekticketsticketclass -------------------------+--------------------------------+-------------------------------+-------------------------------- 2 | 25.00 | Concession | Bronze 3 | 32.00 | Adult | Bronze 8 | 70.00 | Adult | Gold 11 | 40.00 | Adult | Silver 11 | 40.00 | Adult | Silver
Data Integrity with SQL
Tickets and Member Tables linked by a foreign key JF123 Johnny Fox John 101 Lamp Lava Gopikrishna Lavanya 100 password username memlastname memfirstname memberid foreign key 67.00 Silver Adult 133 34 103 12345 110.00 VIP Concession 4 18 105 9898 ticketprice ticketclass tickettype seatnumber showno memberid barcode
Creating the linked tables CREATE Table ShahTicketekMembers ( ShahTicketekMembersMemberID Integer not null, ShahTicketekMembersMemFirstName VarChar(50) not null, ShahTicketekMembersMemLastName VarChar(50) not null, ShahTicketekMembersUserName VarChar(15) not null, ShahTicketekMembersPassword VarChar(15) not null, CONSTRAINT PKMembership PRIMARY KEY (ShahTicketekMembersMemberID) ); CREATE Table ShahTicketekTickets ShahTicketekTicketsBarcode Integer not null, ShahTicketekMembersMemberID Integer not null, ShahTicketekShowsShowNo Integer not null, ShahTicketekTicketsSeatNumber VarChar(15) not null, ShahTicketekTicketsTicketType VarChar(15) not null, ShahTicketekTicketsTicketClass VarChar(15), ShahTicketekTicketsTicketPrice Decimal(5,2) not null, CONSTRAINT FKMemberNo FOREIGN KEY (ShahTicketekMembersMemberID) REFERENCES ShahTicketekMembers ON DELETE CASCADE,
The Table for the Constraints CREATE Table ShahTicketekTickets ( ShahTicketekTicketsBarcode Integer not null, ShahTicketekMembersMemberID Integer not null, ShahTicketekShowsShowNo Integer not null, ShahTicketekTicketsSeatNumber VarChar(15) not null, ShahTicketekTicketsTicketType VarChar(15) not null, ShahTicketekTicketsTicketClass VarChar(15), ShahTicketekTicketsTicketPrice Decimal(5,2) not null,
SQL Syntax for Actions CONSTRAINT PKBarcodeNo PRIMARY KEY (ShahTicketekTicketsBarcode), CONSTRAINT FKMemberNo FOREIGN KEY (ShahTicketekMembersMemberID) REFERENCES ShahTicketekMembers ON DELETE CASCADE, CONSTRAINT FKShowID FOREIGN KEY (ShahTicketekShowsShowNo) REFERENCES ShahTicketekShows NO ACTION means restrict; Most DBMS do not allow all options: - Access permits restrict (default) and cascade - Oracle does not have the ON UPDATE clause - Oracle only permits CASCADE for the ON DELETE clause; default is restrict
Check Constraints CONSTRAINT ShahTicketekTicketsTicketType CHECK ((ShahTicketekTicketsTicketType = 'Adult') OR (ShahTicketekTicketsTicketType = 'Concession') OR (ShahTicketekTicketsTicketType = 'Child')), CONSTRAINT ShahTicketekTicketsTicketClass CHECK ((ShahTicketekTicketsTicketClass = 'Gold') OR (ShahTicketekTicketsTicketClass = 'Silver') OR (ShahTicketekTicketsTicketClass = 'Bronze') OR (ShahTicketekTicketsTicketClass = 'VIP')), CONSTRAINT ShahTicketekShowsShowNo CHECK (ShahTicketekShowsShowNo IS NOT NULL), CONSTRAINT ShahTicketekTicketsBarcode CHECK (ShahTicketekTicketsBarcode IS NOT NULL), CONSTRAINT ShahTicketekTicketsSeatNumber CHECK (ShahTicketekTicketsSeatNumber IS NOT NULL), CONSTRAINT ShahTicketekTicketsTicketPrice CHECK (ShahTicketekTicketsTicketPrice IS NOT NULL)
Normalization
First Normal Form OtherColumns EventID ShowNo The primary key ( ShowNo, EventID, SeatNumber,TicketType,TicketClass,TicketPrice,MemberID, MemFirstName MemLastName UserName,Password,ShowStartTime,ShowDate,EventNa me,EventDescription,VenueName,StateCity) Solution: split into two or more Tables.
Second Normal Form Contains The respective primary keys of each table ShowDate ShowStartTime ShowNo Customer details City State VenueName LocationID EventDescription EventName EventID Contains The respective primary keys of each table MemberID, MemberFirstName,MemLastName,UserName,Password
3rd Normalised form ShowDate ShowStartTime ShowNo City State VenueName ShowDate ShowStartTime ShowNo City State VenueName LocationID EventDescription EventName EventID MemberID Member FirstName MemLastName UserName Password
Using a View in SQL… Suppose we want to obtain a comprehensive report such as the following: Show the Member ID, Ticket class, Username and Seat number of all the members with member id greater than 103 and have gold class tickets. The query would be: Select ShahTicketekMembersMemberID, ShahTicketekTicketsTicketClass, ShahTicketekMembersUserName, ShahTicketekTicketsSeatNumber from ShahTicketekTickets natural join ShahTicketekMembers where ShahTicketekMembersMemberID>103 and ShahTicketekTicketsTicketClass='Gold';
Views Continued… The following output is obtained: 87 Azy Gold 109 567 Hol 108 Seatnumber Username Ticket Class Member ID
Views continued… Suppose this query is required again at a later time, It would be rather inconvenient to type out such a long query all the time. Here is where the use of views come in. A view is basically saving a particular query on the database as a particular name.
Views Continued… To create the view: Applying this to our query, The syntax to create a view is CREATE VIEW name AS query. This will save the query string query to the database under the name name. Applying this to our query, Create View ticketekview as Select ShahTicketekMembersMemberID, ShahTicketekTicketsTicketClass, ShahTicketekMembersUserName, ShahTicketekTicketsSeatNumber from ShahTicketekTickets natural join ShahTicketekMembers where ShahTicketekMembersMemberID>103 and ShahTicketekTicketsTicketClass='Gold';
Views Continued… Now the view has been created, we can check using \dv that it exists List of relations Name | Type | Owner ---------------------------------+------+---------- ticketekview | view | rochandr
Select * from ticketekview; Views continued… We can now use the view at any time simply by using a normal select query: Select * from ticketekview; 87 Azy Gold 109 567 Hol 108 Seatnumber Username Ticket Class Member ID