管理数据库对象
一、 索引
- 作用:加快查询速度
- 创建
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’;
create procedure p5(in sid char(4),in cid char(3),in grad int)
begin
insert into sc
values(sid,cid,grad);
end//
- 删除
drop view v1;
三、 存储过程
1. 定义
2. 作用
3. 创建和调用
delimiter //
例子1
create procedure p1()
begin
update student
set sage = 18
where sno = ‘0001’;
end//
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//
call p3(‘0001’,20)//
4. 查看
5. 删除
四、 触发器
1. 定义
2. 创建
举例1
create trigger tr1
after update on student for each row
begin
if new.sno != old.sno then
update sc
set sno = new.sno
where sno = old.sno;
end if;
end//
举例2