资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

oracle怎么更新主键,oracle如何修改主键

在oracle中怎样用一条sql语句更新两张有主外键关系的表的记录

作者表 A (ID,NAME,AGE,SEX)其中ID为主键,文章表B(ID,AID,ARTICLE)其中ID为主键,AID为外键。如果你希望更新表A中某个作者的ID,并同时更新文章表B中对应的数据,那么写一个触发器:

网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了勐海免费建站欢迎大家使用!

create or replace trigger "A"

after update

of ID

on A_author

for each row

begin

if (:new.ID:old.ID ) then

update B_article set AID = :new.ID where AID = :old.ID;

end if;

end A;

Oracle如何添加主键约束

工具/材料

SQL Developer

01

首先打开SQL Developer软件,找一个没有主键约束的表,如下图所示

02

然后我们新建一个查询,在界面中输入如下的约束修改语句,如下图所示,主要通过add constranint来添加约束

03

编写完约束添加语句以后,点击工具栏中的执行按钮,如下图所示,如果输出栏显示已变更则证明主键约束创建成果

04

然后我们进入STUDENT表的约束添加页中可以看到,我们加的主键约束已经添加进去了,如下图所示

05

另外,创建表的时候可以直接添加主键约束,如下图所示,直接在表创建语句中添加constraint即可

06

表创建完以后,记得在左侧刷新数据库信息,如下图所示,因为你创建的信息不刷新的话不会更新

07

最后打开你所创建的表,看到你添加的主键约束已经加入进来了,这种方式的效果alter的方式是一样的

oracle 外键如何更新

用scott用户打开两个窗口

1、外键无索引时,子表更新外键未提交,主表更新非子表引用的主键时被阻塞

会话1:

create table t1 (x int primary key);

insert into t1 values(1);

insert into t1 values(2);

insert into t1 values(3);

commit;

create table t2(y int references t1);

insert into t2 values(1);

commit;

update t2 set y=2 where y=1;

会话2:

update t1 set x=4 where x=3; //会话被阻塞

2、外键有索引时,子表更新外键未提交,主表更新非子表引用的主键时不会被阻塞

会话1:

create index t2_index on t2(y) ; //创建外键索引

update t2 set y=2 where y=1;

会话2:

update t1 set x=4 where x=3;

已更新 1 行;//可以正常更新

3、外键有无索引,对于子表更新外键未提交,主表更新相对应的主键无影响,更新主键的session都会被阻塞

会话1:

update t2 set y=2 where y=1;

会话2:

update t1 set x=4 where x=1; //更新子表已引用的

会话被阻塞。

会话1:

update t2 set y=2 where y=1;

会话2:

update t1 set x=4 where x=2 ; //更新子表将要引用的

会话被阻塞。――很好理解,主表要判断是否违反约束

二、更新子表非外键列未提交

1、外键无索引,更新主表已被外键引用的主键时,更新主键的session被阻塞

会话1:

create table t1 (x int primary key,x1 int);

insert into t1 values(1,1);

insert into t1 values(2,2);

insert into t1 values(3,3);

commit ;

create table t2(y int references t1,y1 int);

insert into t2 values(1,1);

commit ;

update t2 set y1=2 where y1=1;

会话2:

update t1 set x=4 where x=1; //更新外键引用的主键

会话被阻塞。

2、外键有索引,更新主表已被外键引用的主键时,更新主键的session不会被阻塞而报约束错误

会话1:

create index t2_index on t2(y);

update t2 set y1=2 where y1=1;

会话2:

update t1 set x=4 where x=1

*

ERROR 位于第 1 行:

ORA-02292: 违反完整约束条件 (SCOTT.SYS_C001607) - 已找到子记录日志

3、外键无索引,更新主表未被外键引用的主键时,更新主键的session被阻塞

会话1:

drop index t2_index;

update t2 set y1=2 where y1=1

会话2:

update t1 set x=4 where x=2;

会话被阻塞。

4、外键有索引,更新主表未被外键引用的主键时,更新主键的session不会被阻塞

会话1:

create index t2_index on t2(y);

update t2 set y1=2 where y1=1;

会话2:

update t1 set x=4 where x=2;

已更新 1 行。

另外在一个主表有on delete cascade,子表没有外键索引时,对主表操作会级联到子表,子表将进行全表扫描。

总结:在需要更新主键的情况下,最好是创建子表的外键索引。

oracle 表新增一个LOG_ID字段,Update更新LOG_ID主键,表有2000W数据,耗时很久,有什么办法快速更新。

业务不忙时可开启并行,另外也不知道具体怎么更新,注意绑定变量什么的

可试试有索引或主键先就先让其失效,更新完再维护下

ORACLE 更改主键怎么改?

1、首先应该删除已有的主键约束\x0d\x0a ①若已知道该主键命名\x0d\x0a\x0d\x0a alter table 表名 drop constraint 主键名;\x0d\x0a\x0d\x0a ②若不知道朱建命名\x0d\x0a\x0d\x0a SELECT * from user_cons_columns c where c.table_name = '表名';\x0d\x0a\x0d\x0a 找到主键字段column对应的主键名,再执行①\x0d\x0a\x0d\x0a2、增加新的主键约束\x0d\x0a alter table 表名 add constraint 主键名 primary key(字段名);

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_Code   := 2;

Lr_Reinfo := '进行新增';

Else

Lr_Code   := -1;

Lr_Reinfo := '操作失败';

End If;

Exception

When Others Then

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

Rollback;

End;


分享题目:oracle怎么更新主键,oracle如何修改主键
文章URL:http://cdkjz.cn/article/hssjsc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220