Need table configuration advice

This is a discussion on Need table configuration advice within the MySQL Database forums, part of the Database Forums category; I want to set up a database to track parts, what I know about the parts are: PartNumber Length Material ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008
mstorkamp@yahoo.com
 
Posts: n/a
Default Need table configuration advice

I want to set up a database to track parts, what I know about the
parts are:

PartNumber
Length
Material
Description
Revision
Price

The Revision can change, with it the Length and Price, and the Price
can change over time even if the Revision doesn't. On top of that I
want to keep historical data. I was thinking of setting up three
tables:

TABLE1:
PartNumber
Material
Description

TABLE2:
PartNumber
Revision
Length
Date

TABLE3:
PartNumber
Price
Date

Does that seem like the best arrangement? I'm new to SQL not sure just
how to extract the information for a particular date. I could return
any record who's Date is older than a given date, but that could
return multiple revisions of a single part. How then would I select
only the most recent record from before that date? Thanks for any
ideas.
Reply With Quote
  #2 (permalink)  
Old 04-17-2008
Rik Wasmus
 
Posts: n/a
Default Re: Need table configuration advice

On Thu, 17 Apr 2008 18:00:48 +0200, <mstorkamp@yahoo.com> wrote:

> I want to set up a database to track parts, what I know about the
> parts are:
>
> PartNumber
> Length
> Material
> Description
> Revision
> Price
>
> The Revision can change, with it the Length and Price, and the Price
> can change over time even if the Revision doesn't. On top of that I
> want to keep historical data. I was thinking of setting up three
> tables:
>
> TABLE1:
> PartNumber
> Material
> Description
>
> TABLE2:
> PartNumber
> Revision
> Length
> Date
>
> TABLE3:
> PartNumber
> Price
> Date
>
> Does that seem like the best arrangement?


Seems OK to me, glad you haven't fallen for the trap of adding a Price to
TABLE2. If Length doesn't always change with Revision you could split
TABLE2 in two other tables like TABLE3.

> I'm new to SQL not sure just
> how to extract the information for a particular date. I could return
> any record who's Date is older than a given date, but that could
> return multiple revisions of a single part. How then would I select
> only the most recent record from before that date? Thanks for any
> ideas.


Current:
SELECT t1.PartNumber, t2.Revision, t2.Length, t3.Price
FROM TABLE1 t1
JOIN TABLE2 t2
ON t1.PartNumber = t2.PartNumber
AND t2.Date <= NOW()
LEFT JOIN TABLE2 t2j
ON t1.PartNumber = t2j.PartNumber
AND t2j.Date <= NOW()
AND t2j.Date > t2.Date
JOIN TABLE2 t3
ON t1.PartNumber = t3.PartNumber
AND t3.Date <= NOW()
LEFT JOIN TABLE2 t3j
ON t1.PartNumber = t3j.PartNumber
AND t3j.Date <= NOW()
AND t3j.Date > t3.Date
WHERE t2j.PartNumber IS NULL AND t3j.PartNumber IS NULL


Latest (date possibly in future?):
SELECT t1.PartNumber, t2.Revision, t2.Length, t3.Price
FROM TABLE1 t1
JOIN TABLE2 t2
ON t1.PartNumber = t2.PartNumber
LEFT JOIN TABLE2 t2j
ON t1.PartNumber = t2j.PartNumber
AND t2j.Date > t2.Date
JOIN TABLE2 t3
ON t1.PartNumber = t3.PartNumber
LEFT JOIN TABLE2 t3j
ON t1.PartNumber = t3j.PartNumber
AND t3j.Date > t3.Date
WHERE t2j.PartNumber IS NULL AND t3j.PartNumber IS NULL

Of a particular date:
Just substitute the NOW() calls in the first example with a desired date..

Be sure to set proper indexes to make the fairly quick.
--
Rik Wasmus
Reply With Quote
  #3 (permalink)  
Old 04-18-2008
mstorkamp@yahoo.com
 
Posts: n/a
Default Re: Need table configuration advice

On Apr 17, 12:05 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> SELECT t1.PartNumber, t2.Revision, t2.Length, t3.Price
> FROM TABLE1 t1
> JOIN TABLE2 t2
> ON t1.PartNumber = t2.PartNumber
> LEFT JOIN TABLE2 t2j
> ON t1.PartNumber = t2j.PartNumber
> AND t2j.Date > t2.Date
> JOIN TABLE2 t3
> ON t1.PartNumber = t3.PartNumber
> LEFT JOIN TABLE2 t3j
> ON t1.PartNumber = t3j.PartNumber
> AND t3j.Date > t3.Date
> WHERE t2j.PartNumber IS NULL AND t3j.PartNumber IS NULL
>
> Be sure to set proper indexes to make the fairly quick.


Thanks. I had to play with that for a while to figure out the how and
why to the way it works. (does anybody use the term 'grok' anymore?)
The next Stupid Newbie question is: What would the proper indexes be?
Just PartNumber or Date as well?
Reply With Quote
  #4 (permalink)  
Old 04-19-2008
Michael Austin
 
Posts: n/a
Default Re: Need table configuration advice

mstorkamp@yahoo.com wrote:
> On Apr 17, 12:05 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
>> SELECT t1.PartNumber, t2.Revision, t2.Length, t3.Price
>> FROM TABLE1 t1
>> JOIN TABLE2 t2
>> ON t1.PartNumber = t2.PartNumber
>> LEFT JOIN TABLE2 t2j
>> ON t1.PartNumber = t2j.PartNumber
>> AND t2j.Date > t2.Date
>> JOIN TABLE2 t3
>> ON t1.PartNumber = t3.PartNumber
>> LEFT JOIN TABLE2 t3j
>> ON t1.PartNumber = t3j.PartNumber
>> AND t3j.Date > t3.Date
>> WHERE t2j.PartNumber IS NULL AND t3j.PartNumber IS NULL
>>
>> Be sure to set proper indexes to make the fairly quick.

>
> Thanks. I had to play with that for a while to figure out the how and
> why to the way it works. (does anybody use the term 'grok' anymore?)
> The next Stupid Newbie question is: What would the proper indexes be?
> Just PartNumber or Date as well?



Re: indexes - yes - and it could be a compound index (partnumber,Date or
(date, partnumber).

When designing databases you will want to refrain from using reserved
words as table and/or column names. Date is a reserved word in just
about every database engine out there... For every db - just search for
"reserved word <db engine>"
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:56 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0