简体   繁体   English

表格归一化

[英]Normalisation of Tables

How do I normalize these tables: Owing to the fact that I cant create a foreign key on a partial part of a Composite Key. 如何对这些表进行规范化:由于无法在复合键的一部分上创建外键,因此我要对这些表进行规范化。 The strong text signify the primary keys in the database. 强文本表示数据库中的主键。

Product( ItemCode , ItemName, SellingPrice) 产品( ItemCode ,ItemName,SellingPrice)

Supplier( SupplierName , Phone, Address) 供应商( SupplierName ,电话,地址)

SupplierProducts( SupplierName , ItemCode , SupplyPrice) SupplierProducts( SupplierNameItemCode ,SupplyPrice)

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
);

Leaving aside your choice of Supplier.SupplierName as a PK, the tables look like a perfect normal (ized) m to n relationship between the Product and the Supplier tables. 撇开你的选择Supplier.SupplierName作为PK,所有表看起来像一个完美的正常的 (源化)米至n之间关系ProductSupplier表。 Add a unique index on the pair of SupplierProducts.SupplierName and SupplierProducts.ItemCode and you're good to go SupplierProducts.SupplierNameSupplierProducts.ItemCode对上添加唯一索引,您就可以开始了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM