问题
I have a country table which have a column Countryid I accidently deleted the CountryId Which Had value=1 and now in state table there is a value of column countryId=1 which is used to fetch the records (states) according to country id. I had inserted the country again but it has different id so how can I update the value again in country table of CountryId which primary key from 2 to
回答1:
The direct answer to your question is to use set identity_insert off
. The best place to start is with the documentation.
More important, there is a very simple way to avoid these problems in the future: use explicitly declared foreign key relationships. If you had a foreign key constraint:
alter table state
add constraint fk_state_country foreign key (countryId) references country(countryId);
Then the delete
would not have been allowed.
来源:https://stackoverflow.com/questions/41461412/how-to-update-primary-key-value-in-sql