2017-07-25 16 views
0

映射表我有這些表:如何在沒有FK

  • 洗車
  • 爲了
  • orderHistory
  • 用戶
  • 服務

連接:一個carWash許多orders ,一個order即可一個orderHistory,一個carWash許多services,一個user許多orders

的問題是:當用戶創造秩序,他選擇通過car_wash例如提供的一些服務(wash_car_body = $ 20,wash_wheels = $ 10)從服務表和當用戶想要查看訂單歷史時,我想向用戶顯示所有選擇的服務,如何更好地執行此操作?

我的服務腳本:

create table services(
    id SERIAL not null, 
    car_wash_id int not null, 
    name text not null, 
    price double precision not null, 
    car_body text, 

    CONSTRAINT "gmoika_service_company_id_fKey" FOREIGN KEY (car_wash_id) 
     REFERENCES car_wash (id) MATCH SIMPLE 
     ON UPDATE NO ACTION ON DELETE NO ACTION 
) 

洗車腳本:

create table services(
     id SERIAL not null, 
     name text not null) 

例如

insert into services (car_wash_id, price , name car_body) values (1,20,wheels_wash,sedan) 

當用戶創建以便與id來car_wash = "id"我用下面的腳本給他所有的服務,

select * from services s where s.car_wash_id = "id". 

然後用戶選擇服務。我想將選擇的服務保存到訂單歷史中。 OrderHistory腳本

CREATE TABLE order_history(
id SERIAL not null, 
order_id int, 
wash_price double precision, 
car_body text, 
box_number int, 
car_wash_name text, 
currency text, 
time timestamp with time zone NOT NULL, 
status text, 
CONSTRAINT "gmoika_wash_history_id_fKey" FOREIGN KEY (order_id) 
     REFERENCES order (id) MATCH SIMPLE 
     ON UPDATE NO ACTION ON DELETE NO ACTION 
) 
+0

您是否試圖查詢它?你必須有記錄之間的某種聯繫,否則它是不可能的..也嘗試提供一些示例數據和一個關於如何平板電腦附加的圖表,而不是在文本..這清除了很多 –

+0

添加一些解釋 –

回答

2

你需要添加另一個表,例如其將是order_idservice_id的映射。這兩個都將是order & services表的外鍵。然後,您可以使用它來存儲從歷史洗車訂單中提取的服務。

但是,我建議你考慮一下你的模式。幾件事情要考慮把我的頭頂部:

  1. 爲什麼的單數和複數形式的組合(例如order VS services
  2. 您還沒有定義主鍵
  3. 是什麼目的order & orderHistory之間的一對一映射?
  4. 服務價格變化時你將如何處理?
  5. 如何處理服務的清除/失活&洗車等?