oracl创建父表子表关联关系约束键使用

2022-07-31,,

直接来一个小例子
–建表
CREATE TABLE T1(id int,name varchar2(20),primary key(id));
CREATE TABLE T2(id int,t1_id int,name varchar2(20),primary key(id));
–增加约束条件
ALTER TABLE T2 ADD CONSTRAINT T2_CONS FOREIGN KEY(t1_id) REFERENCES T1;
–说明bai:若T1的id=x不存在,则T2不能du插入zhit1_id=x的记录;dao
– 若T2的t1_id=x存在,则T1不能删除t1_id=x的记录;
– 否则报错!
–删除约束条件
ALTER TABLE T2 DROP CONSTRAINT T2_CONS;

查看T2表sql语句:
create table T2
(
id INTEGER not null,
t1_id INTEGER,
name VARCHAR2(20)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64
next 1
minextents 1
maxextents unlimited
);
– Create/Recreate primary, unique and foreign key constraints
alter table T2
add primary key (ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
alter table T2
add constraint T2_CONS foreign key (T1_ID)
references T1 (ID);

本文地址:https://blog.csdn.net/dhf66812/article/details/107654454

《oracl创建父表子表关联关系约束键使用.doc》

下载本文的Word格式文档,以方便收藏与打印。