网站首页 > 文章中心 > 其它

oracle如何更新主键

作者:小编 更新时间:2023-08-30 14:13:14 浏览量:191人看过

oracle解决主键冲突

参考:

第一段:

①insert? into table as select from......

?insert into table(field) values();

? ? ?主键冲突:在数据插入的时候,如果主键对应的值已经存在,则插入失败,此为主键冲突.此刻可以进行选择性处理,忽略、更新或替换.

----------------------------------------------------------------------------------

? ? ? insert ignore into ......? ? ?此方法遇到主键冲突时,不更改原记录,也不报错.

? replace into table as select? from ......

? 数据不存在则insert,若存在则replace掉,而且在列不全的情况下,未指定value的列会被设为默认值.

注意values括号里只简写一个id即可,最终生效的是update的内容.

第二段:关键时刻,如何使主键失效

? ? alter table tablename disable primary key;

? ? alter table tablename enable primary key;

? ? alter? table tablename drop?primary key;

? ? 使外键失效或生效:

? ? ?alter table tablenamee disable constraint? foreign_key_name;

oracle 如果存在主键则更新(update),否则添加新纪录(insert)怎么写.

Create Or Replace Procedure p_Update_Add(Pn_Id ? ? In Number, --传入的id

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Ln_Code ? Number, --返回码

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Lr_Reinfo Number) Is

Ln_Flags Number;

Begin

Select Count(1) Into Ln_Flags From t_Test Where Id = Pn_Id; --t_test为测试表

If Ln_Flags 0 Then

--有一条或多条记录存在,表示主键已经存在,进行更新操作

Update t_Test Set Xxx = Xxx;

Lr_Code ? := 1;

Lr_Reinfo := '进行更新';

Elsif Ln_Flags = 0 Then

--没有记录,进行添加操作

Insert Into t_Test Values (Xx, Xxx, Xxx, Xxx);

Lr_Reinfo := '进行新增';

Else

Lr_Code ? := -1;

Lr_Reinfo := '操作失败';

End If;

Exception

When Others Then

Dbms_Output.Put_Line('出现异常');

Rollback;

End;

ORACLE 更改主键怎么改?

以上就是土嘎嘎小编为大家整理的oracle如何更新主键相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章