表的规范化

时间:2015-08-24 19:43:48

标签: mysql sql database relational-database normalization

如何规范化这些表:由于我无法在复合键的部分部分创建外键。 强文表示数据库中的主键。

产品( ItemCode ,ItemName,SellingPrice)

供应商(供应商名称,电话,地址)

SupplierProducts( SupplierName ItemCode ,SupplyPrice)

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

除了您选择Supplier.SupplierName作为PK之外,这些表看起来像Product和{{1}之间的完美正常(ized)m到n关系表格。 在SupplierSupplierProducts.SupplierName对上添加唯一索引,您就可以了