-- Create table
create table WDZ1
(
WDZ1ID NUMBER not null,
MEMO VARCHAR2(20)
)
;
alter table WDZ1
add constraint XXXXXX primary key (WDZ1ID);
1。2。从表(没有外健的索引)
-- Create table
create table WDZ2
(
WDZ2ID NUMBER not null,
WDZ1ID NUMBER,
MEMO VARCHAR2(20)
)
;
-- Create/Recreate primary, unique and foreign key constraints
alter table WDZ2
add constraint XXXXX primary key (WDZ2ID)
;
alter table WDZ2
add constraint XXX foreign key (WDZ1ID)
references WDZ1 (WDZ1ID);
1。3。插入数据表到住表
begin
insert into wdz1 values (1,'aa');
insert into wdz1 values(2,'aa2');
insert into wdz1 values (3,'aa3');
insert into wdz2 values(10,3,'wdz3--1');
commit;
end;
1。4。在一个数据库seeesion里面插入数到从表,但是不提交事务
begin
update wdz2 set memo='update wdz2 momo'
where wdz2id=10;
insert into wdz2 values(20,2,'wdz2--1');
end;
对从表进行插入/修改记录,施加的锁也就是行级锁
1。5。在另外一个数据库seeesion里面删除 主表数据
delete from wdz1 where wdz1id=1
这时候 程序会死锁,除非 上面的 对从表的 数据操作提交事务或者回滚事务。
2。具体原因分析
一个数据表的外键主要有3种方式来维护它自己和主表数据的一致性。
(1)delete cascade
例子如下:
alter table WDZ2
add constraint XXX foreign key (WDZ1ID)
references WDZ1 (WDZ1ID) on delete cascade;
(2)Set null
例子如下:
alter table WDZ2
add constraint XXX foreign key (WDZ1ID)
references WDZ1 (WDZ1ID) on delete set null;