视图,存储过程,变量

1.视图
创建视图,修改视图
-- or replace:视图不存在时创建,存在时修改
create or replace view v_user_1 as select id,name from tb_user where id<=10;
-- 修改视图
alter view v_user_1 as select id,name,profession from tb_user where id<=10;
-- 查询视图
select * from v_user_1;
-- 删除视图
drop view v_user_1;
use db01;
create or replace view stu_v_1 as select id,name from student where id<=10; #with cascaded check option ;
select * from stu_v_1;
select * from student;
-- 通过视图插入数据,也会影响表,表中也会插入
insert into stu_v_1 values (6,'田伯光');
-- 数据污染,即通过视图操作的数据对视图不可见,对表可见,因为创建的视图是id<=10的
-- 在创建表时后面加上检查选项with cascaded check option会防止出现数据污染
insert into stu_v_1 values (16,'田伯光');
-- 基于视图的视图
create or replace view stu_v_2 as select id,name
from stu_v_1 where id>=5 with local check option ;#cascaded check option ;
-- cascaded具有级联性,即如果要给视图2插入数据不光要判断2的条件还要判断1的条件
-- 因为2是在1的视图上创建的
-- 检查选项变更为local check option则只判断本视图1的条件,不用判断2视图的条件
select * from stu_v_2;
insert into stu_v_2 values (18,'尹志平');
insert into stu_v_2 values (9,'尹志平');


2.存储过程
-- 创建存储过程,可同时执行多条sql语句
create procedure p1()
begin
    select * from student;
    select * from emp;
end;
-- 使用
call p1();
-- 查看有哪些存储过程
select * from information_schema.routines where routine_schema='db01';
-- 删除存储过程
drop procedure p1;


3.变量
-- 查看系统变量
show variables ;
-- 模糊查询系统变量
show variables like 'auto%';
-- 查看系统变量的值,事务里提到过
select @@autocommit;
-- 设置系统变量的值,可以不加@@
set @@autocommit=0;
set autocommit =0;
-- 设置自定义变量,三种方法
set @a=1,@b=2;
set @c :=3,@d :=4;
select id into @e from student where name='谢逊';
-- 使用
select @e;
-- 局部变量
create procedure p2()
begin
    declare num int default 0;
    select id into num from student where name='殷天正';
    select num;
end;
call p2();

create procedure p3(in score int,out result varchar(10))
begin
    if score>85 then
        set result='秀';
    elseif score>=60 then
        set result='及格';
    else
        set result='不及格';
    end if;

end;
call p3(59,@res);
select @res;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值