oracle 基本操作三:DML语句,数据完整性之约束,约束的维护

--DML语句:
--对于oracle,所有的DML语句,会存在缓存中,需要手动提交或回滚。其它数据库会自动提交
--事务:commit和commit之间的多条语句称为一个事务,多条sql语句可以一次性提交或回滚。
--插入数据
insert into tb_class(id,code) values (1,'npl01');
commit;
--建表:从它表中创建表
create table tb_class2 as select * from tb_class;
--插入数据:从表中取数据然后插入表中
insert into tb_class3(id,code) select id,code from tb_class
--修改:若不加条件,所有符合条件的数据都会被修改
update tb_class set id=4,code='npl04' where id=1;
--删除:删除所有符合条件的数据
delete from tb_class where id=4
--删除:删除所有数据,保留表结构,可以回滚
delete from tb_class
--删除:是DDL语言,截断表:不可以回滚,效率高
truncate table tb_class2
--删除:是DDL语句,删除数据和表结构,不可回滚
drop table tb_class2


--数据完整性:数据值为正确状态。方式:外键,约束,规则,触发器 ,应用代码(过程,函数)
--约束--概念:强制校验规则,有列级约束和表级约束,约束类型:非空,唯一,主键,外键,检查约束
--约束--列级约束,直接在列后定义。
create table tb_student(--学生表
id  number primary key,--主键约束,必须的非空且唯一,唯一标识一条数据,且与业务无关,一张表只有一个主键
name varchar(18) not null,--非空约束
sex varchar(6) not null check(sex='男' or sex='女'),--检查约束
age number not null check (age>18 and age <60),--检查约束
email varchar2(50) unique,--唯一约束,允许多个空,允许多列组合,默认为其建索引,默认名字与列名相同
address varchar2(100) default '郑州',--赋予默认值
iphone4s  varchar2(11),
class_id number not null references tb_class(id) --外键 foreign key
--外键1:子表(学生表)的class_id 必须在主表(班级表)中存在,并且只能是主表的主键或唯一键
--外键2:主表记录被子表参照时,主表记录不能被删除,需要先删子表数据
--外键3:on delete cascade 主表删除记录时,同时删除子表相关的数据。
--外键4:on delete set null 将依赖的外键值转为空值
);
create table tb_class( --班级表
id number primary key,
code varchar2(18) not null
);
--外键:一个表的两个字段或两个表的两个字段之间的关系
--插入数据-学生表:会对约束进行检查
insert into tb_student(id,name,sex,age,email,address,iphone4s,class_id) 
       values(1,'npl01','男',27,'npl01@163.com','上海01','18339993666',1);
insert into tb_student(id,name,sex,age,email,address,iphone4s,class_id) 
       values(2,'npl02','男',27,'npl02@163.com','上海01','18339993666',2);
insert into tb_student(id,name,sex,age,email,address,iphone4s,class_id) 
       values(3,'npl03','男',25,'npl03@163.com','上海03','18339993666',2);
insert into tb_class (id,code) values (1,'class01');
insert into tb_class (id,code) values (2,'class02');


--约束--表级约束:放在所有列建立之后,显式地对哪些列建立列级约束,用,隔开。不能设非空约束
create table tb_student01(
id  number,
name varchar(18) not null,
sex varchar(6) not null,
age number not null,
email varchar2(50),
address varchar2(100) default '郑州',
iphone4s  varchar2(11),
class_id number not null,
constraints tb_student_pk primary key (id), --主键约束
constraints tb_student_unique unique (email),--唯一约束
constraints tb_student_check_sex check (sex='男' or sex='女'),--检查约束
constraints tb_student_check_age check (age>18 and age <60),--检查约束
constraints tb_student_fk foreign key (class_id) references tb_class01(id)--外键约束
);
create table tb_class01( --班级表
id number,
code varchar2(18) not null,
constraints tb_class_pk primary key (id)--主键约束
);




--维护约束:增加,删除,启用和禁用,不能直接修改约束,非空约束必须使用modify
create table tb_student02(
id  number,
name varchar(18) not null,
sex varchar(6) not null,
age number not null,
email varchar2(50),
address varchar2(100) default '郑州',
iphone4s  varchar2(11),
class_id number not null
);
create table tb_class02( --班级表
id number,
code varchar2(18) not null
);
--维护约束:增加,删除,启用和禁用,不能直接修改约束,非空约束必须使用modify
--增加约束--学生表
alter table tb_student02 add constraints tb_student02_pk primary key (id);--主键约束
alter table tb_student02 add constraints tb_student02_unique unique (email);--唯一约束
alter table tb_student02 add constraints tb_student02_check_sex check (sex='男' or sex='女');
alter table tb_student02 add constraints tb_student02_check_age check(age>18 and age <60);
alter table tb_student02 add constraints tb_student02_fk foreign key (class_id) references tb_class02(id);--外键约束
--增加约束--班级表
alter table tb_class02 add constraints tb_class02_pk primary key (id);
--删除约束:根据约束名进行删除,不会影响数据,删除被外键参照的主键时,用cascade级联删除
alter table tb_student02 drop constraints tb_student02_pk;
alter table tb_student02 drop constraints tb_student02_unique;
alter table tb_student02 drop constraints tb_student02_fk;
alter table tb_student02 drop constraints tb_student02_check_sex;
alter table tb_student02 drop constraints tb_student02_check_age;
--禁止约束
alter table tb_student02 disable constraints tb_student02_fk
--激活约束
alter table tb_student02 enable constraints tb_student02_fk
--查看约束: where 条件中表名区分大小写
select constraint_name,constraint_type,search_condition from user_constraints where table_name='TB_STUDENT02';
select constraint_name,column_name from user_cons_columns where table_name='TB_STUDENT02';
--复合约束:联合主键,基于多列定义,只能在表级中定义
create table emp01(
lastname varchar2(25),
firstname varchar2(25),
code varchar2(25),
constraint emp01_pf primary key(lastname,firstname)
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值