首页 > 解决方案 > 卖房的SQL数据模型

问题描述

我为房地产经纪人(房地产经纪人)设计了一个出售房屋的应用程序,其中财产交易指的是“卖方”和“买方”,他们都需要在数据库中注册为客户。

我不知道如何将销售表链接到客户表,其中“销售”需要引用同时都是注册客户的买家和卖家。

任何指导将不胜感激。

谢谢

标签: sql

解决方案


在此处输入图像描述

在代理表中注册卖家,在买家表中注册买家。然后使用外键建立关系

create table client
(
    id    int primary key,
    name  varchar(255) not null,
    state char(4)      not null,
    city  varchar(255) not null
);

create table sales
(
    id        int primary key,
    buyer_id  int not null,
    saller_id int not null,
    amout     double(10, 2)
);

alter table sales
    add constraint foreign key (buyer_id) references client (id),
    add constraint foreign key (saller_id) references client (id);

推荐阅读