Mysql 表格的标准化
如何规范化这些表:因为我无法在复合键的一部分上创建外键。strong文本表示数据库中的主键 产品(项目代码,项目名称,销售价格) 供应商(供应商名称、电话、地址)Mysql 表格的标准化,mysql,sql,database,relational-database,normalization,Mysql,Sql,Database,Relational Database,Normalization,如何规范化这些表:因为我无法在复合键的一部分上创建外键。strong文本表示数据库中的主键 产品(项目代码,项目名称,销售价格) 供应商(供应商名称、电话、地址) SupplierProducts(SupplierName,ItemCode,SupplyPrice)撇开您对供应商的选择不谈。SupplierName作为主键,这些表看起来像是产品和供应商表之间完美的正常(化)m-to-n关系。 在SupplierProducts.SupplierName和SupplierProducts.Item
SupplierProducts(SupplierName,ItemCode,SupplyPrice)撇开您对供应商的选择不谈。SupplierName作为主键,这些表看起来像是
产品
和供应商
表之间完美的正常(化)m-to-n关系。
在
SupplierProducts.SupplierName
和SupplierProducts.ItemCode
对上添加一个唯一的索引,您就可以开始了这里的表对我来说很好。但是,如果您现在尝试在另一个表中引用供应商产品
,您可能非常适合为其提供自己的唯一主键。mysql不允许我将itemcode或suppliername链接到SupplierTable。因为不能仅在组合键的一部分上创建外键。那么,如何将表链接到一起呢?由于组合键很好,所以您可能不需要名称。没有什么说你不能在一个列上有一个键,除了它请提供你的DDL表,我可以帮助你更多。
create table Products
( ItemCode int auto_increment primary key,
ItemName varchar(100) not null,
SellingPrice decimal(10,2) not null,
updateDt datetime not null
-- throw in some indexes
);
create table Suppliers
( SupplierId int auto_increment primary key,
SupplierName varchar(100) not null -- allows you to change a supplier name
-- throw in some indexes once you add more columns
);
create table SupplierPhone
( id int auto_increment primary key,
SupplierId int not null,
PhoneNum varchar(20) not null,
PhoneType int not null, -- have a FK somewhere
-- FK back to Suppliers
-- throw in some indexes
key (SupplierId)
);
create table SupplierAddr
( id int auto_increment primary key,
SupplierId int not null,
Addr1 varchar(100) not null,
Addr2 varchar(100) not null,
-- etc all that jazz
AddrType int not null, -- have a PK somewhere
-- FK back to Suppliers
-- throw in some indexes
key (SupplierId)
);
create table SupplierProducts
( id int auto_increment primary key,
SupplierId int not null,
ItemCode int not null,
SupplyPrice decimal(10,2) not null,
updateDt datetime not null,
-- FK back to Suppliers
-- FK back to Products
-- throw in some indexes
unique key (SupplierId,ItemCode) -- won't allow dupes on this combo
);