数据库管理对象

管理数据库对象
一、 索引

  1. 作用:加快查询速度
  2. 创建
    1) 创建表同时创建索引
    create table student1
    (sno char(4),
    sname char(20),
    index ix_sno(sno asc));
    2) 在已经存在的表上创建索引
    create index ix_sno on student(sno desc);
  3. 查看 show index from student;
  4. 删除
    drop index ix_sno on student1;
    二、 视图
  5. 定义:从一个或多个基本表或视图中导出的虚表。
  6. 作用
  1. 简化对数据的操作。
  2. 自定义数据。
  3. 数据集中显示。
  4. 导入和导出数据。
  5. 合并分割数据。
  6. 安全机制。
  1. 创建(修改)
    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;
  2. 查看
  3. 使用
    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’;

  1. 删除
    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//
四、 触发器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值