Oracle数据库的约束、维护、序列、索引、视图

创建表并同时添加约束

1.主键约束
2.非空约束
3.检查约束
4.唯一约束
5.外键约束


--简单的表创建和字段类型
--简单的创建语句:
create table student(
sno number(10) ,--primary key
sname varchar2(100) ,--not null
sage number(3), --check(sage<150 and sage>0)
ssex char(4) ,--check(ssex='男' or ssex='女')
sfav varchar2(500),
sbirth date,
sqq varchar2(30) --unique
--constraints pk_student_sno primary key(sno)--添加主键约束
--constraints ck_student_sname check(sname is not null)--非空约束
--constraints ck_student_sage check(sage<150 and sage>0)--检查约束
--constraints ck_student_ssex check(ssex='男' or ssex='女')--检查约束
--constraints un_student_sqq unique(sqq)--唯一约束
)

--添加主键约束和删除
alter table student add constraints pk_student_sno primary key(sno);
alter table student drop constraints pk_student_sno;
--添加非空约束和删除
alter table student add constraints ck_student_sname check(sname is not null);
alter table student drop constraints ck_student_sname;
--添加检查约束和删除
alter table student add constraints ck_student_sage check(sage<150 and sage>0)
alter table student drop constraints ck_student_sage;
--添加检查约束校验性别和删除
alter table student add constraints ck_student_ssex check(ssex='男' or ssex='女')
alter table student drop constraints ck_student_ssex;

--添加唯一约束和删除
alter table student add constraints un_student_sqq unique(sqq)
alter table student drop constraints un_student_sqq


二维表创建 外键约束学习:

--创建学生表
create table student(
sno number(10) primary key,
sname varchar2(100) not null,
sage number(3) check(sage>0 and sage<150),
ssex char(4) check(ssex='男' or ssex='女'),
sfav varchar2(500),
sqq varchar2(30) unique,
cid number(10) --references clazz(cno)
--constraints fk_student_cid foreign key(cid) references clazz(cno)--外键
)
--添加外键和删除
alter table student add constraints fk_student_cid foreign key(cid) references clazz(cno) on delete set null
alter table student drop constraints fk_student_cid

--创建班级表
create table clazz(
cno number(10) primary key,
cname varchar2(100) not null,
cdesc varchar2(300)
)

--使用外键:
--作用:当在子表中插入的数据在父表中不存在,则会自动报错。
--概念:当一张表的某个字段的值需要依赖另外一张表的某个字段的值,则使用外键约束。
--其中主动依赖的表称为子表,被依赖的表称为父表。外键加在子表中。
--使用:
--在子表中的字段后直接使用 references 父表名(字段) 例如: cid number(10) references clazz(cno)
--在创建表语句的最后面使用 constraints fk_子表名_字段名 foreign key(字段名) references 父表名(字段名)
--在创建表后使用:alter table 表名 add constraints fk_子表名_字段名 foreign key(字段名) references 父表名(字段名)
--删除外键:alter table 表名 drop constraints 外键约束名
--外键选取:
--一般选取父表的主键作为子表的外键。
--外键的缺点:
--无法直接删除父表数据,除非级联删除
--级联删除:在添加外键约束时,使用关键字 on delete cascade
--使用:当删除父表数据时,自动删除子表相关所有数据。
--缺点:无法保留子表历史数据。
--级联置空  on delete set null
--删除父表数据时,将子表中的依赖字段的值设置为null。
--注意:子表依赖字段不能添加非空约束。


二维表的维护

--添加新的字段:
--alter table 表名 add 字段名 类型
alter table student add sphone number(11)--在学生表中添加新的字段


--修改原有字段
--修改字段类型
--alter table 表名 modify 字段名 新的类型
alter table student modify sphone varchar2(11)


--修改字段名
--alter table 表名 rename column 字段名 to 新的字段名
alter table student rename column sphone to phone


--删除字段
--alter table 表名 drop column 字段名
alter table student drop column phone


--修改表名
--rename 原有表名 to 新的表名
rename student to student2
rename student2 to student


--删除表
--drop table 表名
drop table student


oracle的序列的学习
--创建序列
--使用 create sequence 序列名
--特点1:默认开始是没有值的,也就是指针指在了没有值的位置。
--特点2:序列名.nextval每次执行都会自增一次,默认步长为1
--特点3:序列名.currval查看当前序列的值。开始是没有的。
--作用:作为主键使用,动态的获取之间的值,这样新增数据的时候极大的避免了主键冲突
--使用的是 序列名.nextval作为主键
--注意:主键是非空唯一就可以,不需要主键的值是连续的值。
--创建默认序列
create sequence cc;--创建序列cc
select cc.currval from dual--查看序列当前值
select cc.nextval from dual--查看序列的自增后的值。
--创建自定义序列
create sequence aa--创建序列
start with 5 --设置开始位置
increment by 2 --设置步长
select aa.currval from dual
select aa.nextval from dual
--创建测试表
create table teacher(
tid number(10) primary key,
tname varchar(100) not null
)
insert into teacher values(cc.nextval,'张三');
insert into teacher values(cc.nextval,'张三');

select * from teacher
--删除序列
--drop sequence 序列名
drop sequence aa


索引学习:
--作用:提升查询效率
--使用索引:
--创建
create index 索引名 on 表名(字段名)
--删除索引
drop index 索引名
--特点:
--显示的创建,隐式的执行
--注意:
--oracle会自动给表的主键创建索引。

create index index_teacher_tname on teacher(tname)--创建索引
drop index index_teacher_tname--删除索引
select * from teacher where tname='张三'
select * from teacher where tid=8


视图学习:
--创建视图
create view 视图名 as select 对外提供的内容 from 真实表名
--删除视图
drop view 视图名
--视图特点:
--特点1:保护真实表,隐藏重要字段的数据。保护数据。
--特点2:在视图中的操作会映射执行到真实表中
--特点3:可以手动开启只读模式 使用关键字 with read only
--注意:视图的创建必须拥有dba权限
create view stu as select sno,sname,sage from bjsxt.student       //可以增删改
create view stu2 as select sno,sname,sage from student with read only       //只可以读

select * from student
select * from stu
update stu2 set sname='wollo' where sno=1
grant dba to scott(账号名)

原文地址:https://www.cnblogs.com/liuyi13535496566/p/12462668.html