sql server - Clustered primary key and relations -


i'm struggeling primary keys in database:

example 1

as can see use clustered index. when try insert like: example 2

i exception primary key duplicated, not expected. combination of idquestionaire , version needs unique insert script work.

i tried following: in "keys" folder of servey there 4 keys(the primary, foreign key parkinglottype, survey$idquestionnaire_unique , survey$version_unique)

after removing unique keys insert script works fine foreign key surveyquestion not work anymore...

this code of "survey$idquestionnaire_unique":

    alter table [dbo].[survey] add  constraint [survey$idquestionnaire_unique] unique nonclustered  (     [idquestionnaire] asc )with (pad_index = off, statistics_norecompute = off, sort_in_tempdb = off, ignore_dup_key = off, online = off, allow_row_locks = on, allow_page_locks = on) go 

and here primary key:

alter table [dbo].[survey] add  constraint [pk_survey_idquestionnaire] primary key clustered  (     [idquestionnaire] asc,     [version] asc )with (pad_index = off, statistics_norecompute = off, sort_in_tempdb = off, ignore_dup_key = off, online = off, allow_row_locks = on, allow_page_locks = on) go 

how can make clustered primary key unique 2 column , still able use foreign key?

if primary key of "survey" two columns "idquestionnaire" , "version", adding unique index "idquestionnaire" mistake. column alone not unique, obvious both primary key constraint , insert statement.

sql server builds index on primary key. additional indexes on primary key columns not necessary.

your foreign key constraint needs reference pair of columns, ...(survey_idquestionnaire, survey_version) references [dbo].[survey] (idquestionnaire, version).


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -