sql - Contains no primary or candidate keys that match the referencing -


sql71516 :: referenced table '[dbo].[mstransaction]' contains no primary or candidate keys match referencing column list in foreign key. if referenced column computed column, should persisted.

this error receiving. solutions? here sql code both tables:

create table [dbo].[msorderline]  (     [purchaseid] nchar (200) not null,     [productid]  nchar (200) not null,     [quantity]   int         null,      constraint [doublems_pk]          primary key clustered ([purchaseid] asc, [productid] asc),     foreign key ([purchaseid]) references [dbo].[mstransaction] ([purchaseid]),     foreign key ([productid]) references [dbo].[msproducts] ([productid]) );  create table [dbo].[mstransaction]  (     [transactionid] nchar (200) not null,     [employeeid]    nchar (200) null,     [customerid]    nchar (200) null,     [purchaseid]    nchar (200) not null,     [amount]        int         null,     [totalamount]   int         null,     [timeofsale]    nchar (200) null,     [discountid]    nchar (200) null,      primary key clustered ([transactionid] asc),      foreign key ([discountid]) references [dbo].[msdiscount] ([discountid]),     foreign key ([employeeid]) references [dbo].[msemployee] ([employeeid]),     foreign key ([customerid]) references [dbo].[mscustomer] ([customerid]) ); 

i know has been asked before, either didn't work in situation or couldn't work situation.

as gordon linoff said, these nchar(200) horrible choice clustering keys.

you have create table referenced first. use purchaseid key, must unique.

example in sql server:

create table [dbo].[mstransaction] (     [transactionid] nchar (200) not null,     [employeeid]    nchar (200) null,     [customerid]    nchar (200) null,     [purchaseid]    nchar (200) not null unique,     [amount]        int         null,     [totalamount]   int         null,     [timeofsale]    nchar (200) null,     [discountid]    nchar (200) null,     primary key clustered ([transactionid] asc)     --,foreign key ([discountid]) references [dbo].[msdiscount] ([discountid]),     --foreign key ([employeeid]) references [dbo].[msemployee] ([employeeid]),     --foreign key ([customerid]) references [dbo].[mscustomer] ([customerid]) ); create table [dbo].[msorderline] (     [purchaseid] nchar (200) not null,     [productid]  nchar (200) not null,     [quantity]   int         null,     constraint [doublems_pk] primary key clustered ([purchaseid] asc, [productid] asc),     foreign key ([purchaseid]) references [dbo].[mstransaction] ([purchaseid]),     --foreign key ([productid]) references [dbo].[msproducts] ([productid]) ); 

rextester demo: http://rextester.com/zvzd33282

reference making better choices on clustering keys:


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -