学习黑马MySQL课程,记录笔记,用于复习。
视图
语法:
#创建
create [or replace] view 视图名称[(列名列表)] as select [ with[ cascaded | local ] check option ]
#查询
查看创建视图语句:show create view 视图名称;
查看视图数据:select * from 视图名称 ...... ;
#修改
方式一: create [ or replace ] view 视图名称[(列名列表)] as selsect语句 [ with[ cascaded | local ] check option ]
方式二: alter view 视图名称[(列名列表)] as selsect语句 [ with[ cascaded | local ] check option ]
#删除
drop view [ if exists ] 视图名称
示例:
#创建视图
create or replace view stu_v_1 as select id,name from student where id <= 10;
#查询视图
show create view stu_v_1;
select * from stu_v_1;
select * from stu_v_1 where id < 3;
#修改视图
create or replace view stu_v_1 as select id,name,no from student where id <= 10;
alter view stu_v_1 as select id,name from student where id <= 10;
#删除视图
drop view if exists stu_v_1;
cascaded 会传递父类检查,local不会传递
如果视图包含以下任何一项,则该视图不可更新:
- 聚合函数或窗口函数(SUM()、 MIN()、 MAX()、 COUNT()等)
- distinct
- group by
- having
- union 或者 union all
存储过程
存储过程是事先经过编译并存储在数据库中的一段 SQL 语句的集合。
存储过程思想上就是数据库 SQL 语言层面的代码封装与重用。
基本语法
#创建
create procedure 存储过程名称 ([ 参数列表 ])
begin
-- SQL语句
end;
#调用
call 名称 ([ 参数列表 ]);
#查看
#-- 查询指定数据库的存储过程及状态信息
select * from information_schema.ROUNTINES where ROUNTINES_SCHEMA = 'xxx'
#-- 查询某个存储过程的定义
show create procedure p1;
#删除
drop procedure [ if exists ] 存储过程名称;
在命令行中,执行创建存储过程的SQL时,需要通过关键字 delimiter 指定SQL语句的 结束符。
例如 delimiter $$之后需将多行语句的最后一行结束的;修改为$$
变量
在MySQL中变量分为三种类型: 系统变量、用户定义变量、局部变量。
系统变量
系统变量是MySQL服务器提供,不是用户定义的,属于服务器层面。分为全局变量(GLOBAL
)、会话变量(SESSION
)。
(1)查看系统变量
show [ session | global ] variables; -- 查看所有系统变量
show [ session | global ] variables like '......'; -- 可以通过LIKE模糊匹配方式查找变量
select @@[ session | global ] 系统变量名; -- 查看指定变量的值
select @@global.autocommit;
(2)设置系统变量
set [ session | global ] 系统变量名 = 值 ;
set @@[session | global] 系统变量名 = 值 ;
注意:
如果没有指定SESSION/GLOBAL,默认是SESSION,会话变量。
A. 全局变量(GLOBAL): 全局变量针对于所有的会话。
B. 会话变量(SESSION): 会话变量针对于单个会话,在另外一个会话窗口就不生效了。
用户定义变量
#赋值
set @myname = 'itcast';
set @myage := 10;
set @mygender := '男',@myhobby := 'java';
select @mycolor := 'red';
select count(*) into @mycount from tb_user;
#使用
select @myname,@myage,@mygender,@myhobby;
select @mycolor , @mycount;
select @abc;
局部变量
局部变量 是根据需要定义的在局部生效的变量,访问之前,需要declare声明。可用作存储过程内的局部变量和输入参数,局部变量的范围是在其内声明的begin … end块。
#声明局部变量 - declare
#赋值
create procedure p2()
begin
declare stu_count int default 0;
select count(*) into stu_count from student;
select stu_count;
end;
call p2();
if
根据定义的分数score变量,判定当前分数对应的分数等级。
score >= 85分,等级为优秀。
score >= 60分 且 score < 85分,等级为及格。
score < 60分,等级为不及格。
create procedure p3()
begin
declare score int default 58;
declare result varchar(10);
if score >= 85 then
set result := '优秀';
elseif score >= 60 then
set result := '及格';
else
set result := '不及格';
end if;
select result;
end;
call p3();
参数
类型 | 含义 | 备注 |
---|---|---|
IN | 该类参数作为输入,也就是需要调用时传入值 | 默认 |
OUT | 该类参数作为输出,也就是该参数可以作为返回值 | |
INOUT | 既可以作为输入参数,也可以作为输出参数 |
create procedure p4(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;
#定义用户变量 @result来接收返回的数据, 用户变量可以不用声明
call p4(18, @result);
select @result;
case
根据传入的月份,判定月份所属的季节(要求采用case结构)。
1-3月份,为第一季度
4-6月份,为第二季度
7-9月份,为第三季度
10-12月份,为第四季度
create procedure p6(in month int)
begin
declare result varchar(10);
case
when month >= 1 and month <= 3 then
set result := '第一季度';
when month >= 4 and month <= 6 then
set result := '第二季度';
when month >= 7 and month <= 9 then
set result := '第三季度';
when month >= 10 and month <= 12 then
set result := '第四季度';
else
set result := '非法参数';
end case ;
select concat('您输入的月份为: ',month, ', 所属的季度为: ',result);
end;
call p6(16);
while
计算从1累加到n的值,n为传入的参数值。
– A. 定义局部变量, 记录累加之后的值;
– B. 每循环一次, 就会对n进行减1 , 如果n减到0, 则退出循环
create procedure p7(in n int)
begin
declare total int default 0;
while n>0 do
set total := total + n;
set n := n - 1;
end while;
select total;
end;
call p7(100);
repeat
repeat是有条件的循环控制语句, 当满足until声明的条件的时候,则退出循环 。
create procedure p8(in n int)
begin
declare total int default 0;
repeat
set total := total + n;
set n := n - 1;
until n <= 0
end repeat;
select total;
end;
call p8(10);
call p8(100);
loop
LOOP可以配合一下两个语句使用:
LEAVE :配合循环使用,退出循环。
ITERATE:必须用在循环中,作用是跳过当前循环剩下的语句,直接进入下一次循环。
计算从1到n之间的偶数累加的值,n为传入的参数值。
-- A. 定义局部变量, 记录累加之后的值;
-- B. 每循环一次, 就会对n进行-1 , 如果n减到0, 则退出循环 ----> leave xx
-- C. 如果当次累加的数据是奇数, 则直接进入下一次循环. --------> iterate xx
create procedure p10(in n int)
begin
declare total int default 0;
sum:loop
if n<=0 then
leave sum;
end if;
if n%2 = 1 then
set n := n - 1;
iterate sum;
end if;
set total := total + n;
set n := n - 1;
end loop sum;
select total;
end;
call p10(100);
游标
游标(CURSOR)是用来存储查询结果集的数据类型 , 在存储过程和函数中可以使用游标对结果集进行循环的处理。
游标就是容器中的迭代器 应该是指针实现的
#声明游标
declare 游标名称 cursor for 查询语句 ;
#打开游标
open 游标名称 ;
#获取游标记录
fetch 游标名称 into 变量 [, 变量 ] ;
#关闭游标
close 游标名称 ;
根据传入的参数uage,来查询用户表tb_user中,所有的用户年龄小于等于uage的用户姓名
(name)和专业(profession),并将用户的姓名和专业插入到所创建的一张新表
(id,name,profession)中。
-- 逻辑:
-- A. 声明游标, 存储查询结果集
-- B. 准备: 创建表结构
-- C. 开启游标
-- D. 获取游标中的记录
-- E. 插入数据到新表中
-- F. 关闭游标
create procedure p11(in uage int)
begin
declare uname varchar(100);
declare upro varchar(100);
declare u_cursor cursor for select name,profession from tb_user where age <= uage;
drop table if exists tb_user_pro;
create table if not exists tb_user_pro(
id int primary key auto_increment,
name varchar(100),
profession varchar(100)
);
open u_cursor;
while true do
fetch u_cursor into uname,upro;
insert into tb_user_pro values (null, uname, upro);
end while;
close u_cursor;
end;
call p11(30);
触发器
触发器类型 | NEW 和 OLD |
---|---|
insert 型触发器 | NEW 表示将要或者已经新增的数据 |
update 型触发器 | OLD 表示修改之前的数据 , NEW 表示将要或已经修改后的数据 |
delete 型触发器 | OLD 表示将要或者已经删除的数据 |
语法: |
#插入触发器
create trigger tb_user_insert_trigger
after insert on tb_user for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params)
VALUES(null, 'insert', now(), new.id, concat('插入的数据内容为:
id=',new.id,',name=',new.name, ', phone=', NEW.phone, ', email=', NEW.email, ',
profession=', NEW.profession));
end;
# 查看
show triggers ;