如何更新sql中的主键值? [英] how to update primary key value in sql?

查看:57
本文介绍了如何更新sql中的主键值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个国家表,其中有一列 Countryid 我不小心删除了 CountryId 的值=1,现在在状态表中有列 countryId=1 的值,用于获取根据国家 ID 记录(州).我再次插入了国家,但它有不同的 id那么如何在 CountryId 的国家表中再次更新值,主键从 2 到

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

推荐答案

直接回答您的问题是使用 set identity_insert off.最好的起点是文档.

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);

那么 delete 就不允许了.

这篇关于如何更新sql中的主键值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆