If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > One relation defines another one?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-14-10, 16:53
AndiHoffi AndiHoffi is offline
Registered User
 
Join Date: Sep 2010
Posts: 2
Question One relation defines another one?

Hi,

I am currently struggling with a redundancy, I cannot get rid of.
I am trying to create a small ticketing-system.
You can see the current design here

There is one redundancy involved:
The relation between Events and Locations defines, which Seats can be booked per Event. But there is no mechanism the enforces the Ticket to belong to a seat that is part of its event.
In other words: It might happen that there is a ticket in the system that belongs to a seat that is not part of the location the event belongs to.

I don't have the slightest idea, how to resolve that. Do you?

Thank you very much for an answer
Andreas
Attached Thumbnails
One relation defines another one?-2010-09-14-22-47-53.png  
Reply With Quote
  #2 (permalink)  
Old 09-14-10, 22:31
futurity futurity is offline
Registered User
 
Join Date: May 2008
Posts: 270
Start with location:

location (location_id*)

Each location has multiple seats, each seats belongs to one location. Since we don't expect seats to ever change locations, the key of location is taken as part of the key of seat:

seat (location_id*, seat_id*)

For the purposes of this example, we'll assume that an event takes place at one location. Obviously, a location will have multiple events. The structure is similar to seats:

event (location_id*, event_id*)

A ticket, then, is basically an association table relating a seat to an event:

ticket (seat_location_id*, seat_id*, event_location_id*, event_id*)

Since the seat location and event location must be the same, you can simplify this to:

ticket (location_id*, seat_id*, event_id*)
Reply With Quote
  #3 (permalink)  
Old 09-15-10, 02:57
AndiHoffi AndiHoffi is offline
Registered User
 
Join Date: Sep 2010
Posts: 2
Quote:
Originally Posted by futurity View Post
Start with location:
location (location_id*)
seat (location_id*, seat_id*)
event (location_id*, event_id*)
ticket (location_id*, seat_id*, event_id*)
Thank you for your answer
I got your idea on the second glance. Took me a while to see that you are creating the event- and seat-ids per location.

I guess, there is no way to solve this with simple "one-column-keys"?

Last edited by AndiHoffi; 09-15-10 at 03:00.
Reply With Quote
  #4 (permalink)  
Old 09-16-10, 06:35
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally Posted by AndiHoffi View Post
I guess, there is no way to solve this with simple "one-column-keys"?
No easy way, no. This is one reason why "simple one-column keys" aren't always such a neat idea.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 09-16-10, 07:10
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Quote:
Originally Posted by andrewst View Post
No easy way, no.
I would be interested how this requirement could be modelled with surrogates - do you have something in mind please Tony? I have come across such a requirement a handful of times and have never been able to model it any way other than futurity's method.

I had considered writing up an abstract of such a problem to present to the "surrogates and only surrogates" brigade and ask them how they would solve it but of course I couldn't be arsed.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #6 (permalink)  
Old 09-16-10, 17:27
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally Posted by pootle flump View Post
I would be interested how this requirement could be modelled with surrogates - do you have something in mind please Tony? I have come across such a requirement a handful of times and have never been able to model it any way other than futurity's method.
Not really. In theory, if any DBMS actually supported ANSI "assertions" you could use one of those - something like:
Code:
create assestion a1 check 
(select count(*)
 from ticket t
 join seat s using (seat_id)
 join event e using (event_id)
 where s.location_id != e.location_id) = 0;
... but of course they don't. Another possibility is materialized views with constraints - but this is an academic solution rather than one to put into production, perhaps.

In practice it would probably be done using triggers, which is a bit sucky.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #7 (permalink)  
Old 09-17-10, 04:43
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Not heard of assertions, but that could be done in SQL Server using a CHECK constraint checking the return of a scalar function.

But all three options you've come up with are, as you imply, inferior to simply designing the thing properly with natural keys. Thanks for the response
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #8 (permalink)  
Old 09-17-10, 09:31
MarkATrombley MarkATrombley is offline
Registered User
 
Join Date: Jul 2009
Location: Michigan
Posts: 125
When you insert a row into the Events table a trigger or stored procedure should insert into a Event/Seat table that contains the EventID, SeatID, and a flag that indicates availability (available, sold, reserved). You may want to add price depending on how you are doing pricing. The Ticket should be linked to rows in this table. This way you know what Seats are still available for each event AND you are sure the Seat actually ties to that Event.

Last edited by MarkATrombley; 09-17-10 at 12:58.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On