473,796 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

table structure problem

Help :-) I can't see how this should be laid out!

Consider the scenario of a car rental company. They have "cars" and "lots"
(nationwide) and "customers. " So far it is easy to query each of these. A
car is rented by a customer so there would be say a "rentals" table. A car
is rented and returned over and over so there would be many rentals for each
car. One thing to remember is that the car isn't assigned to a specific lot
so either the customer has the car or it is in a lot (somewhere) and
available for rental.

So the question is how do I layout the tables (and query) to find out where
a specific car is? Or where all the cars are for that matter. I don't
think it is as simple as it looks at first glance. Clearly there can be a
column in the car table containing a LocationId but that wouldn't indicate
whether it is a LotId or a CustomerId. There could be a column for both
LotId and CustomerId and with only one filled in at a time but that doesn't
lend itself to an elegant SQL query does it?

I thought there might be a LocationIdType field. It could be set to
indicate whether the LocationId is a "lot" or a "customer" which permits me
to join with the lots or customers table but I wondered if there is another
way.

Is that enough information to go on? I can give more details if it helps.

Thanks,
Tom

Jul 20 '05
25 3391
"rkc" <rk*@yabba.dabb a.do.rochester. rr.com> wrote...
"Tom Leylan" <ge*@iamtiredof spam.com> wrote...
I think I can do all this with with a single LocationId, LocationType
pair kept in the vehicle table. I'm working on that strategy at the
moment.
I think you are on the right track, with one minor suggestion. While
location, (as indicated by a LocationID) is more or less an attribute
of a vehicle, location type is not. All you need in the Vehicle table is
a LocationID. Type can be determined by a join with the Location
table. That makes LocationType (an attribute of Location) in the
Vehicle table redundant.


Hi... I appreciate the response.

While you are describing the classis solution it turns out this isn't the
classic "many-to-many" problem. Each vehicle is in at most one location the
problem is that we don't know which type of location it is and therefore we
don't know which table to use for the query.

There isn't actually a "location" but rather the location is either a "lot"
or a "customer." While it is possible to introduce a location table the Ids
kept there would be a mix of LotId's and CustomerIds. While there won't be
any common IDs in my system there certainly could be in other
implementations .

It is subtle but the LocationType turns out to be an attribute of the
vehicle just as the LocationId is. If the column in the vehicle table were
named CustomerId and the id there pointed to the customer you can see that
you know the Id "type" it is a customer id by virtue of it being stored in
that column. Another column could be LotId and similarly if I saw a value
it's type would be "lot." We know it's type.

So rather than having two columns, one of which is always null (and
importantly instead of adding another column each time another location type
was needed) I figured the LocationId would stand in for _any_ Id and a
LocationType field would identify which table that Id can be found in.

If it makes is easier to accept a LocationType, we can name it
VehicleStatus. Then it is set to "available" when the LocationId is a lot
and "rented" when the LocationId is a customer. It works the same either
way.
Jul 20 '05 #11
rkc

"Tom Leylan" <ge*@iamtiredof spam.com> wrote in message
news:kd******** **********@twis ter.nyc.rr.com. ..
"rkc" <rk*@yabba.dabb a.do.rochester. rr.com> wrote...
"Tom Leylan" <ge*@iamtiredof spam.com> wrote...
I think I can do all this with with a single LocationId, LocationType
pair kept in the vehicle table. I'm working on that strategy at the
moment.
I think you are on the right track, with one minor suggestion. While
location, (as indicated by a LocationID) is more or less an attribute
of a vehicle, location type is not. All you need in the Vehicle table
is a LocationID. Type can be determined by a join with the Location
table. That makes LocationType (an attribute of Location) in the
Vehicle table redundant.


Hi... I appreciate the response.

While you are describing the classis solution it turns out this isn't the
classic "many-to-many" problem. Each vehicle is in at most one location

the problem is that we don't know which type of location it is and therefore we don't know which table to use for the query.

There isn't actually a "location" but rather the location is either a "lot" or a "customer." While it is possible to introduce a location table the Ids kept there would be a mix of LotId's and CustomerIds. While there won't be any common IDs in my system there certainly could be in other
implementations .

It is subtle but the LocationType turns out to be an attribute of the
vehicle just as the LocationId is. If the column in the vehicle table were named CustomerId and the id there pointed to the customer you can see that
you know the Id "type" it is a customer id by virtue of it being stored in
that column. Another column could be LotId and similarly if I saw a value
it's type would be "lot." We know it's type.

So rather than having two columns, one of which is always null (and
importantly instead of adding another column each time another location type was needed) I figured the LocationId would stand in for _any_ Id and a
LocationType field would identify which table that Id can be found in.

If it makes is easier to accept a LocationType, we can name it
VehicleStatus. Then it is set to "available" when the LocationId is a lot
and "rented" when the LocationId is a customer. It works the same either
way.


I'm sorry. I posted too soon. After thinking about the whole thing for a
while, particularly what Mike Sherrill had to say, I don't think that
LocationID
belongs in the Vehicle table either. My explanation would have to be quite
long winded so I'll only bother if you're interested.


Jul 20 '05 #12
"rkc" <rk*@yabba.dabb a.do.rochester. rr.com> wrote...
I'm sorry. I posted too soon. After thinking about the whole thing
for a while, particularly what Mike Sherrill had to say, I don't think
that LocationID belongs in the Vehicle table either. My explanation
would have to be quite long winded so I'll only bother if you're
interested.


Sure, I'm all ears. If you can, please keep in mind that I'm a little less
interested in a solution that only operates on the latest version of
MS-SqlServer (or any other particular product.) The problem I'm defining
has been around for decades and I have to assume it has been solved before
so a generic rather than proprietary solution would be best.

Tom

Jul 20 '05 #13
rkc

"Tom Leylan" <ge*@iamtiredof spam.com> wrote in message
news:Zp******** ************@tw ister.nyc.rr.co m...
Sure, I'm all ears. If you can, please keep in mind that I'm a little less interested in a solution that only operates on the latest version of
MS-SqlServer (or any other particular product.) The problem I'm defining
has been around for decades and I have to assume it has been solved before
so a generic rather than proprietary solution would be best.


Okey Dokey. Here goes.

Here's a first draft of an approach that makes the answer to the question
'Where are all the vehicles now?" possible.

Other questions that could be answered include

"What vehicles are available now?"
"Where's the yellow Hummer2?"
"How often do we service the Yugos?"
"What vehicles are currently out of service?"
"What is the most frequently rented vehicle type?"
"Where were the step vans on 9/11/2001?"
"Do you think we need more 4 wheel drive vehicles?"

[Tables]

Vehicles (VehicleID*, Type)
Type = Compact, Mini-Van, Full-Size, etc.

Locations (LocationID*, Type)
Type = Rental Lot, Service Station, Body Shop, In Transit, etc.

Rentals (VehicleID*, CustomerID*, DateRented*, DateReturned, TypeRequested)

LocationsVehicl es (LocationID*, VehicleID*, DateArrived*, DateDeparted)

Customers (CustomerID*, CustomerName)

[/Tables]

[Views]

CurrentVehicleL ocations
Question: Where are all the vehicles, disregarding rental status.
Also used in LocationOfUnRen tedVehicles view

SELECT
V.VehicleID,
V.Type,
L.LocationID,
L.LocationType,
LV.DateArrived
FROM Vehicles V INNER JOIN (Locations L
INNER JOIN LocationsVehicl es LV
ON L.LocationID = LV.LocationID)
ON V.VehicleID = LV.VehicleID
WHERE LV.DateDeparted Is Null;

OutStandingRent als
Question: What vehicles are currently out as rentals
and who rented them?
Also used in LocationOfUnRen tedVehicles view.
Also used in CurrentStatusOf Vehicles view.

SELECT
V.VehicleID,
V.Type,
"Rented" AS LocationID,
C.LastName & ", " & C.FirstName AS Customer,
RD.DateRented
FROM Vehicles V INNER JOIN (Customers C
INNER JOIN RentalDetails RD
ON C.EmployeeID = RD.CustomerID)
ON V.VehicleID = RD.VehicleID
WHERE RD.DateReturned Is Null;

LocationOfUnRen tedVehicles
Question: Where are the unrented vehicles?
Also use in CurrentStatusOf Vehicles view

SELECT CurrentVehicleL ocations.*
FROM CurrentVehicleL ocations
WHERE CurrentVehicleL ocations.Vehicl eID NOT IN
(SELECT VehicleID FROM OutStandingRent als);

CurrentStatusOf Vehicles

SELECT LocationsOfUnRe ntedVehicles.*
FROM LocationsOfUnRe ntedVehicles
UNION
SELECT OutStandingRent als.*
FROM OutStandingRent als;

[/Views]
Jul 20 '05 #14
"rkc" <rk*@yabba.dabb a.do.rochester. rr.com> wrote...

Gosh you did a lot of work... thanks.
"What vehicles are available now?"
"Where's the yellow Hummer2?"
"How often do we service the Yugos?"
"What vehicles are currently out of service?"
"What is the most frequently rented vehicle type?"
"Where were the step vans on 9/11/2001?"
"Do you think we need more 4 wheel drive vehicles?"
And those are exactly the types of things one would want to know... even if
nobody is asking me for it now, they will! I'm going to have to give your
reply a better look but I've read it a few times now and it looks good.

Clearly we need a vehicles table.
Vehicles (VehicleID*, Type)
Type = Compact, Mini-Van, Full-Size, etc.
And a customers table.
Customers (CustomerID*, CustomerName)
And a historic record of the rentals.
Rentals (VehicleID*, CustomerID*, DateRented*, DateReturned, TypeRequested)

This table is interesting. Let me try to recap... renting a vehicle creates
a new "rental" record, updates the "change in location" record (departing
the place it was at) and adds a new "change in locations" record (arriving
at the customer.) And returning a rental updates an existing rental record
(it's been returned), updates the "change in location" (departing the
customer) and adds a new "change in location" record (arriving at the lot)
again.
LocationsVehicl es (LocationID*, VehicleID*, DateArrived*, DateDeparted)
And just to confirm, you are operating under the rule that CustomerId (along
with all the other locations) are unique to the system right? No big deal
but I don't think "In Transit" is a location.
Locations (LocationID*, Type)
Type = Rental Lot, Service Station, Body Shop, In Transit, etc. Question: Where are all the vehicles, disregarding rental status. SELECT
V.VehicleID,
V.Type,
L.LocationID,
L.LocationType,
LV.DateArrived
FROM Vehicles V INNER JOIN (Locations L
INNER JOIN LocationsVehicl es LV
ON L.LocationID = LV.LocationID)
ON V.VehicleID = LV.VehicleID
WHERE LV.DateDeparted Is Null;


That query can be further limited by VehicleId, VehicleType, LocationId and
LocationType which makes it very useful. I think it can do the
LocationOfUnRen tedVehicles by limiting the type to "Rental Lot" or by
excluding the "Customer" type. And it can list OutStandingRent als as well
by limiting the type to "Customer" and joining the results to the rental
details and customer tables.

The "key" is the DateDeparted column. The Null indicates this is the last
location the vehicle was transferred to. That and the composite Locations
table which must contain every location a vehicle can be at.

I think this works... thanks again for all your work.

Tom

Jul 20 '05 #15
rkc

"Tom Leylan" <ge*@iamtiredof spam.com> wrote in message
news:Kb******** **********@twis ter.nyc.rr.com. ..
"rkc" <rk*@yabba.dabb a.do.rochester. rr.com> wrote... And just to confirm, you are operating under the rule that CustomerId (along with all the other locations) are unique to the system right? No big deal
but I don't think "In Transit" is a location.


The attributes with the asterisks make up the primary key. I envisioned
customerID and locationID as system generated unique values.

Say a customer rents the yellow hummer in Syracuse, NY and drives it
to Chicago Illinois. One way. The people in Syracuse want it back
because it's a big money maker.

Somebody needs to transport it back to Syracuse.

Where is it while it's on it's way back? In Transit.
Jul 20 '05 #16
"rkc" <rk*@yabba.dabb a.do.rochester. rr.com> wrote...
The attributes with the asterisks make up the primary key. I envisioned
customerID and locationID as system generated unique values.
They are system generated but in order to work they have to be "system"
unique not simply unique within a particular Id type. In order to be a
possible location the CustomerId has to be found in the Location table
right? It isn't enough to simply have a generic "customer" location.
Somebody needs to transport it back to Syracuse.
Where is it while it's on it's way back? In Transit.


Oh definitely but in order to track it in the location table it has to be
somewhere An "InTransit" type has to be assigned to a LocationId. It was
at a "rental lot" it is currently on it's way to another "rental lot" there
isn't any LocationId with an InTransit type.

It could be handled as a "transfer" much as a rental is but in a Transfers
table. A rental is an exchange between a lot and a customer and transfer
is an exchange between two lots. A repair (details in the Repairs table)
would be an exchange between a lot and a "repair station." The
RepairStationId would be found in the Locations table in the LocationId
column.

It works out well also because the LocationType can restrict meaningless
transactions... you can't create a rental with a RepairStationId , or create
a repair record using a CustomerId, etc.


Jul 20 '05 #17
On Fri, 19 Sep 2003 16:29:49 GMT, "Tom Leylan"
<ge*@iamtiredof spam.com> wrote:

[snip]
Hi Mike... I don't follow this, when did libraries stop tracking which books
you've borrowed?
They didn't. Libraries record who checked out the books; they don't
record where the books are. The books *might* be at the home address
of the person who checked them out, but they might not be.
You can't think I mean "where" in terms of latitude and
longitude and in the case of a library book they don't care which shelve (if
any) you have it on. That you have it is it's "location" right?
No. If I check out a book, it means I have the book, and the library
doesn't. It doesn't mean the library knows where the book is.
A customer is not a lot.


That seems obvious. I'm still don't get your point.


What do database designers do with different things? We make them
into different tables, or we put them into different columns.
You can rent it, lease it, sell it, send it to a garage for service,
loan it to your brother-in-law, have it stolen, misplace it, and so
on. Rentals require one set of attributes, sales require another set,
service requires yet another, and so on.

[snip]Similarly
(should you ask) inching it forward on Main Street or backing out of the
driveway doesn't need to be tracked either... and I still don't get your
point. :-)
You probably need to store different information about rentals than
you need to store about service. This suggests that information about
rentals belongs in a different table than information about service.
Nope. I can create tables designed to hold information about your
brother-in-law, the garage, the rental and the sale okay? Now how do I know
which table to check

[snip]

Check the vehicle disposition table, of course.

--
Mike Sherrill
Information Management Systems
Jul 20 '05 #18
"Mike Sherrill" <MS*******@comp userve.com> wrote...
On Fri, 19 Sep 2003 16:29:49 GMT, "Tom Leylan" wrote:
You can't think I mean "where" in terms of latitude and
longitude and in the case of a library book they don't care which shelve (ifany) you have it on. That you have it is it's "location" right?
No. If I check out a book, it means I have the book, and the library
doesn't. It doesn't mean the library knows where the book is.


Mike your time is much to valuable to spend devising semantic arguments...
everybody over the age of two pretty much knows that "you have it" is all
the library cares about.
Check the vehicle disposition table, of course.

Well that's why you're in charge of things.

I always admire how newsgroup conversations can be carried on in such an
adult manner... like your general assumption that I could be confused about
the way a library book works. So many people wouldn't take the time to
explain how you could put a book on a shelf in the basement (for instance)
and how (contrary to what the average human might expect) the librarian
wouldn't "know."

Your correctly surmised the real topic of my original message. Thanks so
much for sharing.

Jul 20 '05 #19
rkc

"Tom Leylan" <ge*@iamtiredof spam.com> wrote in message
news:fb******** ************@tw ister.nyc.rr.co m...
"rkc" <rk*@yabba.dabb a.do.rochester. rr.com> wrote...
The attributes with the asterisks make up the primary key. I envisioned
customerID and locationID as system generated unique values.
They are system generated but in order to work they have to be "system"
unique not simply unique within a particular Id type. In order to be a
possible location the CustomerId has to be found in the Location table
right? It isn't enough to simply have a generic "customer" location.
Somebody needs to transport it back to Syracuse.
Where is it while it's on it's way back? In Transit.


Oh definitely but in order to track it in the location table it has to be
somewhere An "InTransit" type has to be assigned to a LocationId. It was
at a "rental lot" it is currently on it's way to another "rental lot"

there isn't any LocationId with an InTransit type.

It could be handled as a "transfer" much as a rental is but in a Transfers
table. A rental is an exchange between a lot and a customer and transfer
is an exchange between two lots. A repair (details in the Repairs table)
would be an exchange between a lot and a "repair station." The
RepairStationId would be found in the Locations table in the LocationId
column.

It works out well also because the LocationType can restrict meaningless
transactions... you can't create a rental with a RepairStationId , or create a repair record using a CustomerId, etc.


Well, I'm not as happy with the structure I outlined as you seem to be.
There are some fundamental things I would change.

That's great though. If things are more clear for you now, go with it.

Jul 20 '05 #20

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

Similar topics

0
2442
by: Randall Sell | last post by:
Hello all, I am migrating a Paradox application to SQL Server. My problem is that the existing Paradox table structure is limited. Correcting it will mean a re-write of the application (Delphi app). Although the record and column limits are higher in SQL Server, it will eventually hit the wall. So I was wondering if anyone could offer advise on how we might do this best.... The application is essentially an evaluation tool. It is very...
1
4063
by: mksql | last post by:
As an example, I am building an authentication mechanisim that will use data in the 3 left tables to determine rights to objects in a destination table, diagrammed below. In this structure, multiple records in the left tables will point to multiple records in the right table. Normally, I would approach this problem using junction tables (LeftID, RightID) to create many-to-many joins. However, given the structure of each table is nearly...
4
3260
by: Laphan | last post by:
Hi All Wonder if you could help, I have a bog standard table called STOCKPRICES that has served me well for a while, but now I need to change the structure of it and because a number of users have used it in it's present form I need to so the following in SQL script: a) Grab a snapshot of the current SQL data. b) Re-structure the STOCKPRICES table.
5
1900
by: brett valjalo | last post by:
Hey Gang! SORRY ABOUT THE LENGTH! Nice to see some of the same faces around this place from way back when ... Whatta buncha CDMA addicts some o' y'all are ;) Don't get me wrong, I understand, believe me! Took me a lot of therapy, various 12-step programmes, interventions by loved ones, etc, but I finally managed to excavate, er, extricate myself from the
4
5859
by: dough | last post by:
I have a hash table with seperate chaining with a bunch of words in it. Here is my declaration: typedef struct word *word; struct word { int count; char *s; word next; };
3
1571
by: Pavan | last post by:
Hi All, I am having a wierd problem while displaying my panel. My code is something like this <table> <tr> <td width=100>label</td> <td width=100>label</td> <td width=100>label</td> <td width=100>button in this cell</td>
0
2016
by: The Frog | last post by:
Hello Everyone, I have been asked to try and create a single SQL query to retrieve product information from a database. The way that data is arranged is that in some tables there are user defined "attributes" or "dimensions" that in turn connect to the actual product table via a many-many (using a linking table). In each linking table there is a combination of the "dimension", the productID, and the "fact" that is stored against the...
12
9140
by: Michael.Z | last post by:
Anyone who can help: Given a Table.h file I am writing a Table.c file. I keep getting the compile error: previous declaration of Table was here / conflicting types for I think the problem was the result of two pieces of code. First: typedef struct Table; /* in Table.c*/
7
3894
by: billelev | last post by:
I'm building a database and am a bit stumped about how to construct/link tables. I will describe the current configuration, then present the problem I am trying to solve. Currently: I currently have a table called tblSeries. This table holds information about a time series of data, and how that data should be displayed. It has a SeriesDataID field that will link to another table (tblSeriesData) that contains information about the name...
6
26334
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this one table. My problem is I have two buttons I want to use this code for for the two buttons would put the data in different tables. I have tried copying and changing a few things and nothing will work for me. The code is set up in a module and then I...
0
10465
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.