Yes, i have it like you say, one nurse has one work order, but during that
work order can stay at up to 3 different accommodations. The only way i can
get it to work is to have 3 exact same accommodation tables which i know is
wrong. I just want to be able to use the accommodation table as a look up so
in my query it would look like this
Nurse WO# Hotel Phone BillingAmt (This
is Work O spec.)
jane workorder 1 Ramada Hotel 555-44444 60
jane workorder 1 Sandman Hotel 111-5555 55
jane workorder 1 Holiday Inn 444-0000 40
I need some advice
I think mscertified's schema needs a little tweaking.
I don't see how WorkOrderID can be an attribute of an accommodation
entity, therefore has no place in the Accommodation table. I think an
additional table is required to model the relationship between work
orders' staffing and accommodation entities.
Similarly, a nurse is an entity in its own right and there is a design
rule of thumb which states that a table models either an entity or a
relationship but not both (otherwise you get 'update anomalies' where
you get into situations where you cannot demonstrate you need to hire
a nurses to fulfil work orders because you cannot create a work orders
without first having nurses etc), therefore another table is required
to model the relationship (1:1 this time) between work orders and
nurses.
Also, the tables are missing even the most basic constraints:
Nurse table
--NurseID <-- unique constraint
--NurseName
WorkOrder table
--WorkOrderID <-- unique constraint
WorkOrderNurse table
--WorkOrderID <-- unique constraint, foreign key constraint referencing Work Order table
--NurseID <-- foreign key constraint referencing Nurse table
--unique constraint on the compound of (NurseID, WorkOrderID)
Accommodation table
--AccomID <-- unique constraint
--Phone etc.
WorkOrderAccommodation table
--WorkOrderID
--NurseID
-- foreign key constraint on the compound of (NurseID, WorkOrderID)
referencing WorkOrderNurse
-- AccomID <-- foreign key constraint referencing Accommodation table
-- unique constraint on the compound of (AccomID, NurseID,
WorkOrderID)
-- table constraint to ensure that WorkOrderID appears no more than
three times
-- BillingAmt
-- etc
For the table constraint to ensure the relationship is 1:1..3, I can
suggest two approaches:
1) Use a sequence column of type integer with a validation rule to
restrict its values to one of three distinct values (e.g. seq_col
BETWEEN 1 AND 3) then create a unique constraint on the compound of
(WorkOrderID, seq_col).
2) Use a table-level CHECK constraint e.g.
CHECK (NOT EXISTS (
SELECT T1.WorkOrderID
FROM WorkOrderAccommodation AS T1
GROUP BY T1.WorkOrderID
HAVING COUNT(*) > 3))
[Aside: I can imagine an auditor examining the above schema and
wondering why there is no temporal element to nurses' accommodation
for work orders e.g. what is there to stop a single night's
accommodation being attributed to more than one work order...? I
assume the auditor would demand additional information to ensure
fraudulent claims for accommodation aren't being made.]
With the schema tweaked, the required query will look something like:
SELECT W1.NurseID, N1.NurseName,
A1.Name AS Hotel, A1.Phone, W1.BillingAmt
FROM (WorkOrderAccommodation AS W1
INNER JOIN Accommodation AS A1 ON
A1.AccomID = W1.AccomID)
INNER JOIN Nurse AS N1
ON N1.NurseID = W1.NurseID;
Jamie.