参考:
第一段:
①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;
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如何更新主键相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!