表的规范化
Normalisation of Tables
如何规范化这些表:由于我无法在复合键的部分部分上创建外键。 强文本表示数据库中的主键。
Product(ItemCode, ItemName, SellingPrice)
供应商(供应商名称,Phone,地址)
SupplierProducts(SupplierName, ItemCode, SupplyPrice)
撇开你选择 Supplier.SupplierName
作为 PK,这些表看起来像 Product
之间完美的 正常 (化)m 对 n 关系和 Supplier
表。
在 SupplierProducts.SupplierName
和 SupplierProducts.ItemCode
对上添加一个 unique 索引,就可以了
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
);
如何规范化这些表:由于我无法在复合键的部分部分上创建外键。 强文本表示数据库中的主键。
Product(ItemCode, ItemName, SellingPrice)
供应商(供应商名称,Phone,地址)
SupplierProducts(SupplierName, ItemCode, SupplyPrice)
撇开你选择 Supplier.SupplierName
作为 PK,这些表看起来像 Product
之间完美的 正常 (化)m 对 n 关系和 Supplier
表。
在 SupplierProducts.SupplierName
和 SupplierProducts.ItemCode
对上添加一个 unique 索引,就可以了
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
);