MySQL 视图 存储过程(未完)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | 视图概述 视图( 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,@description); select @description; @description : 这种变量要在变量名称前面加上“@”符号,叫做用户会话变量,代表整个会话过程他都是有作用 的,这个类似于全局变量一样。 @@ global .sort_buffer_size : 这种在变量前加上 "@@" 符号, 叫做 系统变量 |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek “源神”启动!「GitHub 热点速览」
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器