Normalization of Many to Many relation in sql -
i have product table contains 2 column
productid desc 1 fan 2 table 3 bulb
i have table contains supplier information
supplierid desc 1 abc 2 xyz 3 hjk
now 1 supplier can supply multiple products , 1 product can supplied multiple suppliers achieve have created table tbl_supplier_product
supplierid productid 1 1 1 2 2 1 2 2 2 3
is way link table supplier , product table through primary composite key. in table primary key composite key (supplierid , productid) or should add column row id each record , use primary key , add unique constraint columns supplierid , productid
supplierid productid row id 1 1 1 1 2 2 2 1 3 2 2 4 2 3 5 unique constraint(supplierid, productid)
what relationship of table supplier table? bit confused here because added table resolve many many relationship , redundant data still seems table has many many relationship both tables??
you don't need column: composite key need
i create unique index reverse of pk too: useful many queries , provides fk index productid
after comment:
create table supplierproduct ( supplierid int not null, productid int not null, primary key (supplierid, productid) ); go create unique nonclustered index ixu_reversepk on supplierproduct (productid, supplierid); go
for more
- sql: need auto-incremental primary key many-many tables? more (see comments)
- difference between 2 indexes columns defined in reverse order
and use ensure fks have indexes
select fk.name [missing fk index] sys.foreign_keys fk exists ( select * sys.foreign_key_columns fkc fkc.constraint_object_id = fk.object_id , not exists ( select * sys.index_columns ic ic.object_id = fkc.parent_object_id , ic.column_id = fkc.parent_column_id , ic.index_column_id = fkc.constraint_column_id ) ); go
in erd (random 1 powerpoint have):