Download presentation
Presentation is loading. Please wait.
1
The TicketekCase Study
Romana Chandra Jason Murphy Shivam Sinha Distinction Assignment, Autumn 2007
2
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.
3
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
4
SQL Queries on a Single Entity/ Table
5
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 “;”
6
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
7
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
8
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
9
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)
10
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
11
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
12
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 | QLD (1 row) Select ShahTicketekLocationState, ShahTicketekLocationLocationID from ShahTicketekLocation where ShahTicketekLocationState = ‘QLD’; shahticketeklocationstate | shahticketeklocationlocationid QLD | (1 row)
13
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
14
Calculating Select ShahTicketekMembersMemberID, ShahTicketekTicketsTicketPrice, ShahTicketekTicketsTicketPrice*0.5 as discount from ShahTicketekTickets; shahticketekmembersmemberid | shahticketekticketsticketprice | discount 100 | | 102 | | 103 | | 104 | | 105 | | 106 | | 107 | | 108 | | 109 | | 110 | | 111 | | 112 | | 113 | | 114 | | 115 | | 101 | | (17 rows)
15
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 (1 row)
16
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
17
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’
18
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)
19
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)
20
AVG and SUM Functions… Give the average price of all tickets Select avg(ShahTicketekTicketsTicketPrice) from ShahTicketekTickets; avg (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)
21
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)
22
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)
23
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)
24
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)
25
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', ); 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', ); INSERT INTO ShahTicketekTickets VALUES (12745, 101, 43, '63Z', 'Child', 'Bronze', 15.00);
26
Foreign Keys and Natural Joins
27
Foreign keys Primary key Location, Event
shahticketeklocationlocationid | shahticketeklocationvenuename | shahticketeklocationstate | shahticketeklocationcity 1 | Hordern Pavilion | NSW | Sydney 2 | The Besen Centre | VIC | Melbourne | 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 | | 2 | | 2 | | 2 | | 2 | | 2 | | 2 | | 2 | | …| … | … Primary key
28
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. | | 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | | 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | | 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | | 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | | 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | |
29
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. | | 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | | 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | | 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | | 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | | 10 | Wil Anderson | Politics, pop and the banal come together in a brand new show. | |
30
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.
31
Entities and Relationships
32
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
33
Group by, sub-queries and complex joins
34
Shows and Tickets showno | starttime | showdate 1 | 19: | 16/06/2006 2 | 20: | 20/05/2006 3 | 14: | 21/05/2006 4 | 20: | 23/05/2006 5 | 20: | 24/05/2006 6 | 20: | 25/05/2006 7 | 20: | 26/05/2006 8 | 20: | 27/05/2006 9 | 20: | 20/05/2006 10 | 19: | 21/05/2006 11 | 18: | 16/06/2006 12 | 21: | 16/06/2006 13 | 20: | 20/05/2006 14 | 13: | 21/05/2006 15 | 20: | 24/05/2006 16 | 20: | 25/05/2006 17 | 20: | 26/05/2006 18 | 14: | 27/05/2006 19 | 20: | 27/05/2006 20 | 13: | 28/05/2006 barcode | memberid | showno | seatnumber | tickettype | ticketclass | ticketprice 78473 | | | 24A | Adult | Gold | 82084 | | | 24B | Adult | Gold | 29495 | | | | Concession | Bronze | 12345 | | | | Adult | Silver | 98765 | | | 60Z | Concession | Bronze | 89898 | | | | Concession | VIP | 34462 | | | | Child | Bronze | 39482 | | | 8A | Adult | Bronze
35
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/ | 10/07/ | 16/06/ | 21/05/ | 27/05/ | 24/05/ | 9/06/ |
36
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/ | 16/06/ |
37
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 | Crazy For you | Daniel Kitson | David Strassman | EuroBeat | Faith Prince | Lano and Woodley | Paresh Rawal | Russell Peters | Wil Anderson |
38
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 | Crazy For you | Daniel Kitson | David Strassman | EuroBeat | Faith Prince | Lano and Woodley | Paresh Rawal | Russell Peters | Wil Anderson |
39
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 | Crazy For you | Daniel Kitson | David Strassman | EuroBeat | Faith Prince | Lano and Woodley | Paresh Rawal | Russell Peters | Wil Anderson |
40
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 | Crazy For you | Daniel Kitson | David Strassman | EuroBeat | Faith Prince | Lano and Woodley | Paresh Rawal | Russell Peters | Wil Anderson |
41
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 | Crazy For you | Daniel Kitson | David Strassman | EuroBeat | Faith Prince | Lano and Woodley | Paresh Rawal | Russell Peters | Wil Anderson |
42
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 | Crazy For you | Daniel Kitson | David Strassman | EuroBeat | Faith Prince | Lano and Woodley | Paresh Rawal | Russell Peters | Wil Anderson |
43
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 | Crazy For you | Daniel Kitson | David Strassman | EuroBeat | Faith Prince | Lano and Woodley | Paresh Rawal | Russell Peters | Wil Anderson |
44
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 | Crazy For you | Daniel Kitson | David Strassman | EuroBeat | Faith Prince | Lano and Woodley | Paresh Rawal | Russell Peters | Wil Anderson |
45
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 | Crazy For you | Daniel Kitson | David Strassman | EuroBeat | Faith Prince | Lano and Woodley | Paresh Rawal | Russell Peters | Wil Anderson | 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.
46
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 | 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 | | 2 | | 2 | | 2 | | 2 | | 2 | | 2 | | 2 | |
47
Left Outer join locationid | tseventid | showno | venuename | shahticketeklocationstate | locationcity 1 | | | Hordern Pavilion | NSW | Sydney 2 | | | The Besen Centre | VIC | Melbourne 2 | | | The Besen Centre | VIC | Melbourne 2 | | | The Besen Centre | VIC | Melbourne 2 | | | The Besen Centre | VIC | Melbourne 2 | | | The Besen Centre | VIC | Melbourne 2 | | | The Besen Centre | VIC | Melbourne 2 | | | The Besen Centre | VIC | Melbourne 3 | | | NSW | Sydney
48
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);
49
Right Outer join locationid | eventid | showno | venuename | locationstate | locationcity 1 | | | Hordern Pavilion | NSW | Sydney 2 | | | The Besen Center | VIC | Melbourne 2 | | | The Besen Center | VIC | Melbourne 2 | | | The Besen Centre | VIC | Melbourne 2 | | | The Besen Centre | VIC | Melbourne 2 | | | The Besen Centre | VIC | Melbourne 2 | | | The Besen Centre | VIC | Melbourne 2 | | | The Besen Centre| VIC | Melbourne 3 | | | NSW | Sydney 3 | | | NSW | Sydney 3 | | | NSW | Sydney 3 | | | NSW | Sydney 3 | | | NSW | Sydney 3 | | | NSW
50
Self-Join Join a table to itself
Usually involve a self-referencing relationship Useful to find relationships among rows of the same table
51
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 | | Concession | Bronze 3 | | Adult | Bronze 8 | | Adult | Gold 11 | | Adult | Silver 11 | | Adult | Silver
52
Data Integrity with SQL
53
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
54
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,
55
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,
56
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
57
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)
58
Normalization
59
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.
60
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
61
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
62
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';
63
Views Continued… The following output is obtained: 87 Azy Gold 109 567
Hol 108 Seatnumber Username Ticket Class Member ID
64
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.
65
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';
66
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
67
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.