我在建模公司与供应商和客户之间的关系时遇到了问题。基本上在我的系统中供应商和客户也是公司所以我做了这个模式:
table.company:
id
name
//other fields
table.company_suppliers:
company_id FK table.company.id
supplier_id FK table.company.id
table.company_clients:
company_id FK table.company.id
client_id FK table.company.id
这样可以吗?
我将只使用一个表,它将包含所有的公司和一个位字段(由实例供应商调用),它将告诉您哪些是供应商。
Company
Id
Name
IsSupplier (bit)
Fk_IdSupplier --it will relate this supplier to a company on the same table
或者您可以创建一个连接表(多对多)
Company
Id
Name
IsSupplier (bit)
CompanySupplier
fk_IdCompany
fk_IdSupplier
您的基本见解是正确的——您不想要不相关的客户和供应商表。但是你的身份证号太多了。
create table companies (
company_id integer primary key,
company_name varchar(35) not null
);
create table suppliers (
supplier_id integer primary key references companies (company_id)
-- Other supplier columns go here.
);
create table clients (
client_id integer primary key references companies (company_id)
-- Other client columns go here.
);
如果你使用的是MySQL,你需要稍微调整一下语法。MySQL不支持所有声明主键和外键的标准SQL语法。