简体   繁体   中英

Using relationship table's ID in other tables

I have 2 entities which are related using a relationship table (ManyToMany) And then I have a pricing table which has pricing information for a service of a hospital. This table can have different pricing for the same service if the hospital linked to the service is different.

hospitals:
id name 

services:
id name

hospital_service:
hospital_id service_id

service_pricing:
id hospital_id service_id weight_range_id LH_price SH_price

service_duration:
id hospital_id service_id weight_range_id LH_duration SH_duration

Now my question is should I be rewriting the hospital_id vs service_id information in the service_pricing table? Isn't this repetition of data? I already have this information in Hospital_service table.

(Same is the issue I have with service_duration table)

What I have thought of doing, I have decided to add an ID column to the relationship table (hospital_service) and I will be using this as foreign key in service_pricing table.

Is this a good enough solution in my case? I am using Laravel's Eloquent ORM for this, so how do I say "get pricing of service 7 of hospital 2" ?

EDIT: Added additional information to service_pricing and service_duration table, the pricing / duration depends on weight_range_id and whether its LH / SH.

I would use tables like:

hospitals:
    id
    name 

services:
    id
    name

hospital_service:
    hospital_id
    service_id
    price
    duration

There are no repetition of data. hospital_service is many to many table for connectiong services and hospitals.

Hospitals
ID:NAME
1:hostiptal a
2:hospital b

Services
ID:NAME
1:service a
2:service b

hospital_service
HOSPITAL_ID:SERVICE_ID:DURATION(minutes):PRICE(euro)
1:1:30:100
1:1:60:180
1:2:180:500

Service "service a" cost 100 for 30 minutes, and 180 for 60 minutes.

EDIT:

You have more options, two of them are: 1.You can insert more services eg.

service a-lh
service a-sh
service b-lh
service b-sh

and then price for each service or:

2.

HOSPITAL_ID:SERVICE_ID:DURATION(minutes):PRICE(euro):SERVICE TYPE
1:1:30:100:lh
1:1:60:120:sh
1:2:60:180:lh
2:2:180:500:lh

should I be rewriting the hospital_id vs service_id information in the service_pricing table?

You can if you want to. Either way works, it's just a matter of what's most convenient. It's just a bit easier to join with the service_pricing and service_duration tables if the key is one field instead of two.

Isn't this repetition of data? I already have this information in Hospital_service table.

No, it's not considered redundant, as it's the key values that connect the records. You have to have the value also in the foreign key to make a connection at all.

I am using Laravel's Eloquent ORM for this, so how do I say "get pricing of service 7 of hospital 2" ?

I'm not familiar with that framework, and laravel.com seems to be down at the moment. The SQL for that would be something like:

select
  weight_range_id, LH_price, SH_price
from
  service_pricing
where
  hospital_id = 2
  and service_id = 7

If you decide to add another id in the hospital_service table, you would need to join in that table for that query:

select
  p.weight_range_id, p.LH_price, p.SH_price
from
  service_pricing p
  inner join hospital_service hs on hs.is = p.hospical_service_id
where
  hs.hospital_id = 2
  and hs.service_id = 7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM