PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Need table configuration advice
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Need table configuration advice

Réponse
 
LinkBack Outils de la discussion
Vieux 17/04/2008, 17h00   #1
mstorkamp@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 17/04/2008, 18h05   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 18/04/2008, 19h08   #3
mstorkamp@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?
  Réponse avec citation
Vieux 19/04/2008, 03h18   #4
Michael Austin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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>"
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h19.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10219 seconds with 12 queries