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:
- ever-increasing clustering key – clustered index debate……….again! - kimberly tripp
- the clustered index debate continues… - kimberly tripp
- more considerations clustering key – clustered index debate continues! - kimberly tripp
- how key cost? (plus sp_helpindex9) - kimberly tripp
- disk space cheap, not point - kimberly tripp
- 101 things wish knew sql server - thomas larock
- sql server: natural key verses surrogate key - database journal - gregory a. larsen
- ten common database design mistakes - simple talk - louis davidson
Comments
Post a Comment