Creating a composite foreign key in SQL Server 2008 -
Creating a composite foreign key in SQL Server 2008 -
i have 2 tables i'd create foreign key for.
primary table
pk - key1 - varchar(20) pk - key2 - date secondary table
pk - autoid fk - key1 - varchar(20) fk - key2 - date when seek create relationship between primary , secondary table, maintain getting message
the columns in primary table not match primary key or unique constraint.
there can many records in secondary table same key1 , key2 made primary key auto created number.
any thoughts on how can set foreign key relationship between these 2 tables?
some of focused, of context others having sort of problem (like searches first?)
the first thing check when have problem creating key create sure did not fubar info types in 2 tables. if have bigint in 1 , int in other, blow. true on keys, more crop if utilize multiple fields. simple math shows reason why chance increases.
the next issue data. if cannot create key due data, have find out exists in kid table not exist in parent table. left bring together tables (secondary on second/left side of join) , include rows primary table null. either have create these records in parent table or rid of them.
one way "around" set new primary key on parent table. create foreign key on new primary key , match many records can in kid table. have bring together set , can go cleaning secondary operation.
which better? new primary key or working composite key? depends on nature of data, more fond of using derived key on natural key or composite key. but, there times work necessary single field derived key lot of work.
sql sql-server-2008
Comments
Post a Comment