2017-07-25 45 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. 如何处理服务的清除/失活&洗车等?