how to update primary key value in sql?

萝らか妹 提交于 2020-01-05 08:38:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!