管理数据库对象
一、 索引
- 作用:加快查询速度
- 创建
1) 创建表同时创建索引
create table student1
(sno char(4),
sname char(20),
index ix_sno(sno asc));
2) 在已经存在的表上创建索引
create index ix_sno on student(sno desc); - 查看 show index from student;
- 删除
drop index ix_sno on student1;
二、 视图 - 定义:从一个或多个基本表或视图中导出的虚表。
- 作用
- 简化对数据的操作。
- 自定义数据。
- 数据集中显示。
- 导入和导出数据。
- 合并分割数据。
- 安全机制。
- 创建(修改)
Create(alter) view 名字
As
Select语句
举例
创建视图v1,其内容包括学生学号、姓名、课程号、课程名、成绩;
create view v1
as
select student.sno,sname,course.cno,cname,grade
from student join sc on student.sno = sc.sno
join course on sc.cno = course.cno; - 查看
- 使用
select *
from v1
where sname = ‘刘晨’;
对视图的增删改是有限制的
出错示例:
insert into v1
values(‘2222’,‘h’,‘c01’,‘java’,80);
create view v3
as
select sno,avg(grade)
from sc
group by sno;
update v3
set avg(grade) = 80
where sno = ‘0001’;
- 删除
drop view v1;
三、 存储过程
1. 定义
2. 作用
3. 创建和调用
delimiter //
例子1
create procedure p1()
begin
update student
set sage = 18
where sno = ‘0001’;
end//
创建存储过程p1,其功能是将1号年龄改为18岁
call p1()//
例子2
create procedure p2(in sid char(4))
begin
update student
set sage = 18
where sno = sid;
end//
call p2(‘0002’)//
例子3
create procedure p3(in sid char(4),in nl int)
begin
update student
set sage = nl
where sno = sid;
end//
create procedure p4(in sid char(4),in cid char(3),in grad int)
begin
insert into sc
values(sid,cid,grad);
end//
创建存储过程p10,其功能是更新学生成绩
create procedure p10(in sid char(4),in cid char(3),in grad int)
begin
update sc
set grade = grad
where sno = sid and cno = cid;
end//
创建存储过程p11,用来为student表添加sno和sname
create procedure p11(in sid char(4),in name char(20))
begin
insert into student(sno,sname)
values(sid,name);
end//
4. 查看
(1) 查看当前数据库下所有的存储过程
show procedure status//
(2) 查看某个存储过程的创建代码
show create procedure p10//
5. 删除
drop procedure p10//
四、 触发器