视图概述
视图(View)是一种虚拟存在的表。视图并不在数据库中实际存在,行和列数据来自定义视图的查询中使用的表,
并且是在使用视图时动态生成的。通俗的讲,视图就是一条SELECT语句执行后返回的结果集。所以我们在创建视
图的时候,主要的工作就落在创建这条SQL查询语句上。
select * from city c, country t where c.country_id = t.country_id;
select c.*,t.country_name from city c, country t where c.country_id = t.country_id;
create view view_name as select语句;
create view view_city_country as select c.*,t.country_name from city c, country t where c.country_id = t.country_id;
select * from view_city_country;
update view_city_country set city_name = ‘重庆市‘ where city_id = 1; //不建议更新视图;
select * from view_city_country;
alter view ivew_name as select语句;
show tables;
show table status\G
show create table table_name;
show create view view_name;
show create view view_city_country;
drop view view_name;
drop view if exists view_city_country;
drop index idx_city_name on city;
存储过程和函数
存储过程和函数是 事先经过编译并存储在数据库中的一段 SQL 语句的集合,调用存储过程和函数可以简化应用开
发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提高数据处理的效率是有好处的。
存储过程和函数的区别在于函数必须有返回值,而存储过程没有。
函数 : 是一个有返回值的过程 ;
过程 : 是一个没有返回值的函数 ;
存储过程:
create procedure procedure_name
begin
--SQL语句
end ;
create procedure pro_test1()
begin
select ‘Hello World‘
end ;
delimiter $ //声明 结束符为 $符号了
调用存储过程:
call procedure_name();
call pro_test1();
查看存储过程
-- 查询db_name数据库中的所有的存储过程
select name from mysql.proc where db=‘db_name‘;
select name from mysql.proc where db=‘demo_01‘;
-- 查询存储过程的状态信息
show procedure status;
-- 查询某个存储过程的定义
show create procedure test.pro_test1 \G;
show create procedure pro_test1 \G;
删除 存储过程
drop procedure procedure_name;
drop procedure pro_test1;
select name from mysql.proc where db=‘demo_01‘;
存储过程的语法:
存储过程是可以编程的,意味着可以使用变量,表达式,控制结构 , 来完成比较复杂的功能。
1)变量
declare: declare var_name type default
create procedure pro_test1()
begin
declare num int default 10;
select concat(‘num的值为:‘, num);
end;
call pro_test1();
set命令赋值, select...into...赋值操作:
create procedure pro_test2()
begin
declare num int default 0;
set num = num + 10;
select num;
end;
call pro_test2;
create procedure pro_test3()
begin
declare num int;
select count(*) into num from city;
select concat(‘city表中的记录数为:‘, num);
end;
call pro_test3;
2)if 条件判断
create procedure pro_test4()
begin
declare height int default 175;
declare description varchar(50) default ‘‘;
if height >= 180 then
set description=‘身材高挑‘;
elseif height >= 170 and height < 180 then
set description=‘标准身材‘;
else
set description=‘一般身材‘;
end if;
select concat(‘身高‘,height,‘对应的身材类型为:‘, description);
end;
call pro_test4;
3)传递参数
IN : 该参数可以作为输入,也就是需要调用方传入值 , 默认
OUT: 该参数作为输出,也就是该参数可以作为返回值
INOUT: 既可以作为输入参数,也可以作为输出参数
create procedure procedure_name([in/out/inout] 参数名 参数类型)
...
IN 参数:
create procedure pro_test5( in height int)
begin
declare description varchar(50) default ‘‘;
if height >= 180 then
set description=‘身材高挑‘;
elseif height >= 170 and height < 180 then
set description=‘标准身材‘;
else
set description=‘一般身材‘;
end if;
select concat(‘身高‘,height,‘对应的身材类型为:‘, description);
end;
call pro_test5(190);
OUT 参数:
create procedure pro_test6( in height int,out description varchar(100))
begin
if height >= 180 then
set description=‘身材高挑‘;
elseif height >= 170 and height < 180 then
set description=‘标准身材‘;
else
set description=‘一般身材‘;
end if;
end;
call pro_test6(190);
原文:https://www.cnblogs.com/walkersss/p/15119895.html