Oracle Study Notes 003 (constraint)

Here Insert Picture Description

constraint

  • Table-level constraints are mandatory, and depending on the constraints of scope, constraints can be divided into table-level and column-level constraints two kinds. Straightaway say that column-level constraint is the constraint when you define an added definition behind column constraints, only the role of the current column; and the table level only constraint is not only the role of a certain column, the second is acting on a plurality of columns simultaneously, separately defined.

  • Oracle using the keyword CONSTRAINTCONSTRAINT defined constraints.

  • Oracle in six kinds of constraints

     1. PRIMARY KEY                        主键约束(唯一性、非空性)
     2. UNIQOE                             唯一性约束
     3. NOT NULL                           非空约束(只能定义在列上)
     4. FOREIGN KEY                        外键约束(两个表之间建立关系)
     5. DEFAULT                            默认约束(数据的默认值)
     6. CHECK                              检查约束(对数据的格式、范围、长度等等的限制)
    

1. Add constraint when construction of the table

--给表添加约束条件之后创建表
create table pleatuser(
    id varchar2(32),
    --列级非空约束
    user_name varchar2(32) not null,
    --默认约束
    user_addr varchar2(64) default '该用户很懒,没有填写地址',
    --唯一约束
    phone varchar2(11) unique,
    --设置主键
    constraint pleatuser_id_pk primary key(id)
    )tablespace users;

--插入语句报错   ORA-01400: 无法将 NULL 插入 ("ORACLEUSER"."PLEATUSER"."USER_NAME")
insert into pleatuser(id,user_name,phone) values('001',null,'123123456');

--满足非空约束  正确
insert into pleatuser(id,user_name,phone) values('001','Tom001','123123456');

--插入语句报错   ORA-00001: 违反唯一约束条件 (ORACLEUSER.SYS_C006999)
insert into pleatuser(id,user_name,phone) values('002','Tom002','123123123');

--满足唯一约束   正确 
insert into pleatuser (id,user_name,phone) values('002','Tom002','123123123');

--查询结果如下,地址没有填写是默认值

Here Insert Picture Description
Here Insert Picture Description
Check constraints (CHECK)

create table demo(
    id varchar2(8),
    name varchar2(32),
    --检查约束,性别只能填写 'w' 'm'
    sex varchar(32) default 'w'check(sex in ('w','m'))
    ) tablespace users;

--插入语句报错   ORA-02290: 违反检查约束条件 (ORACLEUSER.SYS_C007000)
insert into demo values ('001','python','男');

--满足检查约束  正确
insert into demo values ('001','python','m');

Here Insert Picture Description
Foreign key constraint (foreign key)

--用户表
create table pleatuser(
    id varchar2(32),
    --列级非空约束
    user_name varchar2(32) not null,
    --默认约束
    user_addr varchar2(64) default '该用户很懒,没有填写地址',
    --唯一约束
    phone varchar2(11) unique,
    --设置主键
    constraint pleatuser_id_pk primary key(id)
    )tablespace users;

--订单表
create table indent(
    id varchar2(32),
    user_id varchar2(32) unique not null,
    commodity_id varchar2(32) unique not null,
    create_date date,
    --设置外面约束
    constraint pleatuser_indent_user_id foreign key(user_id) references pleatuser(id)
    ) tablespace users;


--插入数据测试约束条件
insert into pleatuser(id,user_name,phone) values('001','Tom1','123123456');

--插入数据违法外键约束 ORA-02291: 违反完整约束条件 (ORACLEUSER.PLEATUSER_INDENT_USER_ID) - 未找到父项关键字
insert into indent(id,user_id,commodity_id,create_date) values('0001','01','01',to_date('2020-02-19','yyyy-mm-dd'));

--满足外键,插入完成
insert into indent(id,user_id,commodity_id,create_date) values('0001','001','01',to_date('2020-02-19','yyyy-mm-dd'));

--查询结果
select * from pleatuser,indent;

Here Insert Picture Description

2. Add the table after the establishment of the completion of the constraints

--为已经建立完成的表添加主键约束
alter table pleatuser add constraint pleatuser_id_pk primary key(id);

--适用于为已经建立好的表添加外键约束
alter table indent add constraint  pleatuser_indent_user_id foreign ley(user_id) references pleatuser(id);

--添加非空约束
alter table pleatuser modify user_name not null;

--添加唯一约束
alter table pleatuser add constraint uq_pleatuser_user_name unique(user_name);

3. cascade operation

--使用该方式,删除的时候,级联删除掉子表中的所有匹配行,创建外键时,通过 on delete cascade 子句指定该外键列可级联删除

alter table ident add constraint pleatuser_indent_user_id foreign key(user_id) references pleatuser (id) on delete cascade;


--使用该方式,删除的时候,会将对应子表中的所有匹配行的外键约束列置为NULL,通过 on delete set null 子句实现

alter table ident add constraint pleatuser_indent_user_id foreign key(user_id) references pleatuser (id) on delete set null;


While studying records, if any deficiencies welcome message pointing ...

Published 63 original articles · won praise 1 · views 2034

Guess you like

Origin blog.csdn.net/qq_45061361/article/details/104393368