473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Relational Database Question

Hello,

I'm pretty new to setting up databases, but so far I'm getting along
swimmingly. But I have one question. I'm setting up a database for a
client who wants to sell tickets to their theater online. There are a
LOT of different parameters and I'd like to set it up so that when a
client selects a play and date the price is automatically generated. I
have already figured out how to get what I want, but I believe there
is a better way, I'm just ignorant to it right now. Here is the deal:

5 plays a year
5 different show times (i.e. sunday matinee, weekday evenings,
friday/sat evenings, etc.)
Each play has a different price for each show time

So I have a table (ticket_price) now that looks like this:

play ticket_type price
----------------------------------
play_1 matinee 20
play_1 week_eve 25
play_1 fri_sat_eve 28
play_2 matinee 19
play_2 week_eve 23
play_2 fri_sat_eve 25

etc....

and the order (ticket_order) table:

order_id play ticket_type meal quantity
-----------------------------------------------------------
01 play_1 matinee yes 2
02 play_2 fri_sat_eve no 5

etc...

So I match the ticket with its price for by:

mysql-> select * from ticket_order, ticket_price
where ticket_order.ticket_type='week_eve'
and ticket_price.ticket_type='week_eve'
and ticket_order.play='play_1'
and ticket_price.play='play_1';

and from there I can use PHP to create a shopping cart type of thing.
But the ticket price table seems redundant to me. But for the life of
me I cannot think of how to fix it up. Or, perhaps, the above SELECT
is more complicated than it needs to be. I've poured over my books and
did searches, but nothing seems to touch on this particular subject. I
have a horrible feeling i'm missing something elementary!

Thanks!
Shane
Jul 20 '05 #1
4 1359
Shane wrote:

5 plays a year
5 different show times (i.e. sunday matinee, weekday evenings,
friday/sat evenings, etc.)
Each play has a different price for each show time But the ticket price table seems redundant to me. But for the life of
me I cannot think of how to fix it up. Or, perhaps, the above SELECT
is more complicated than it needs to be.


The ticket_price table is not redundant, if the ticket price varies per
play and per show. It's okay to generate a few rows -- it sounds like
you're going to have only 25 new rows per year. This isn't an
excessively costly proposition.

You're making it a bit complicated in part because you have two columns
that identify a play, where you could have one. There's no exact right
way to do it, but here's how I would propose to design it:

create table ticket_price (
ticket_id integer not null auto_increment primary key,
play_title varchar(100) not null,
show_datetime datetime not null,
ticket_price numeric(5,2),
offers_meal tinyint not null default 0,
on_sale tinyint not null default 1
);

Create a separate record for ticket_price for each showing. Not only
for each of the five showings during the week, but for every showing for
that play for the entire season. This way, you can print the date &
time on each customer's ticket, you can track when a given showing has
sold out so you can stop selling tickets, and you can make reports at
the end of the season to find out which dates & times were most popular,
which might help plan for next season.

I've also added a proposed column "offers_meal" because some shows like
matinees may not have an option to buy a meal.

The ticket price might be redundant in typical cases, because it's often
going to be one price for a matinee vs. evening, or one day of week vs.
another day of week. But tracking it in this table enables you to vary
it based on date (maybe later in the run of the play, you would want to
reduce prices to keep filling the seats), and also avoids creating yet
another table, assuming you go with the one-record-per-datetime design I
describe above.

Now the ticker_order table:

create table ticket_order (
order_id integer not null primary key,
ticket_id integer not null references ticket_price,
quantity_seats integer not null default 1,
quantity_meals integer not null default 0
);

Now you get the ticket price for a given order with this simple query:

select p.ticket_price * o.quantity_seats
+ (MEAL_PRICE * o.quantity_meals) as order_total
from ticket_price p inner join ticket_order o
on p.ticket_id = o.ticket_id;

Just my suggestions. There are doubtless many ways to design this
solution. The complexity of tables and "redundancy" of data depends on
how many figures can vary independently of one another. There's also a
tradeoff between redundancy and complexity of the data model; sometimes
it's simpler to create a redundant data design (also known as a
non-normalized data model) than to break it down perfectly into tables
and sub-tables, and deal with the programming that would be required by
this.

Regards,
Bill K.
Jul 20 '05 #2
Thank you Bill for your enlightening description. That helps a lot.
Now, though, I'm wondering about populating the tables. I'd like to
set it up so that when we add plays, we can give the run time (ie.
from Feb. 2, 2004 to April 10, 2004) and have the database
automatically filled with a PHP script that will figure out the days,
prices and such. I wonder if there is anything else to take into
consideration with the database when anticipating this feature.

Thank you for your help.

Shane
Jul 20 '05 #3
Shane wrote:
Thank you Bill for your enlightening description. That helps a lot.
Now, though, I'm wondering about populating the tables. I'd like to
set it up so that when we add plays, we can give the run time (ie.
from Feb. 2, 2004 to April 10, 2004) and have the database
automatically filled with a PHP script that will figure out the days,
prices and such. I wonder if there is anything else to take into
consideration with the database when anticipating this feature.


Hmm. If the times are pretty uniform, for instance if matinees are
always at 2pm and do not include meals, you could create another table
that has some of the common show times, with any other relevant
attributes like that. For example:

create table show_times (
show_time_id integer not null primary key,
day_of_week enum(0,1,2,3,4,5,6) not null, # Sunday = 0
time_of_day time not null,
ticket_price numeric(5,2),
offers_meal tinyint not null default 0
);

Then use this to create a pick-list in your user interface, so the
person entering data for a new play can select which days & times the
play will be shown. Then based on the show times selected, your code
can generate the full list of showings between the play opening and closing.

Basically this is just a way to move some hard-coded values out of the
PHP code and into the database. That's often a good thing to do,
because it makes it easier to add more choices later.

Regards,
Bill K.
Jul 20 '05 #4
Bill Karwin <bi**@karwin.com> wrote in message news:<cb*********@enews3.newsguy.com>...
create table show_times (
show_time_id integer not null primary key,
day_of_week enum(0,1,2,3,4,5,6) not null, # Sunday = 0
time_of_day time not null,
ticket_price numeric(5,2),
offers_meal tinyint not null default 0
);
ERROR 1064: You have an error in your SQL syntax.

http://dev.mysql.com/doc/mysql/en/ENUM.html
"An ENUM is a string object.."


Ok, we try this instead (column names shortened to avoid line wrap):

mysql> create table show_times (
-> show_time_id integer not null primary key,
-> day_wk enum('0','1','2','3','4','5','6') not null,
-> tm_of_dy time not null,
-> tck_prc numeric(5,2),
-> off_ml tinyint not null default 0
-> );
Query OK, 0 rows affected (0.00 sec)

mysql> insert into show_times values (3.14, -1, 'never enough time..',
'way too expensive!!', 128);
Query OK, 1 row affected (0.00 sec)

mysql> select * from show_times where day_wk=0;
+--------------+--------+----------+---------+--------+
| show_time_id | day_wk | tm_of_dy | tck_prc | off_ml |
+--------------+--------+----------+---------+--------+
| 3 | | 00:00:00 | 0.00 | 127 |
+--------------+--------+----------+---------+--------+
1 row in set (0.00 sec)

mysql> select version();
+------------+
| version() |
+------------+
| 4.0.14-Max |
+------------+
1 row in set (0.00 sec)

Note:

* Select on an enum column with a numeric value returns rows matching
the corresponding index, not data.
* 3.14 is accepted as an integer and rounded to 3.
* -1 is not the set of valid enums. The default empty string is
inserted instead .
* Illegal time values are converted to '00:00:00'.
* Illegal numeric values are converted to zero.
* Out-of-bounds numeric values are clipped down to the appropriate
endpoint of the range.

MySQL behaves as documented, but probably not as expected.

Lesson learnt: *NEVER* trust MySQL to enforce data integrity unless
you are absolutely sure it performs as you expect.
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. ...
5
by: Markus Seibold | last post by:
Dear NG, I am working on a student project about a mobile tourism information system and among others I have to answer the question whether to use: - a relational database - a XML-native database...
0
by: Mike | last post by:
Hello, I'm trying to understand how to map hierarchical XML data to relational database tables, but I seem to be missing something. I'm not a database expert, but I know the basics. XML seems...
0
by: Stylus Studio | last post by:
DataDirect XQuery(TM) is the First Embeddable Component for XQuery That is Modeled after the XQuery API for Java(TM) (XQJ) BEDFORD, Mass.--Sept. 20, 2005--DataDirect Technologies...
49
by: Mike MacSween | last post by:
I frequently hear that there isn't a commercially available dbms that fully implements the relational model. Why not? And which product comes closest. Mike MacSween
3
by: cassandra.flowers | last post by:
I'm designing a database because I have to do it for the preperation work for my A-Level ICT exam. The database is for a building company. It has to store information on building projects...
3
by: Robert Abi Saab | last post by:
Hi everyone. I just finished a course on PostgreSQL and I found out that PostgreSQL doesn't provide any object relational features (as claimed in the official documentation), except table...
6
by: Shwetabh | last post by:
Hi, I have a very simple question. In what cases are relational databases necessary? Are they really necessary in cases where only a single type of query is to be performed based on one unique...
5
by: sh | last post by:
I am working on a database project, and I'm trying to think "objectively". Are there any tools that will map my "objects" to relational tables? I'd prefer a freebie, or something faily low-cost to...
7
by: Pradeep | last post by:
Hello, I need to take a set of input tables and create an XML output file. The format of the XML output must be user-definable and must be intuitive enough for non-techies to use. input...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.