当前位置:网站首页>MySQL数据库(五)视 图 、 存 储 过 程 和 触 发 器
MySQL数据库(五)视 图 、 存 储 过 程 和 触 发 器
2022-07-06 09:25:00 【手可摘鑫晨】
1,视图
概念:视图是从一个或几个表中所导出的虚表
语法:create view view_name as select...
说明:
view_name:自己定义的视图名
as:后面是这个视图说用到的查询结果
# 视图
-- 创建视图
-- CREATE VIEW 试图名 AS 查询sql语句;
CREATE VIEW v_stu_class1 AS SELECT * FROM student WHERE classid =1;
-- 使用试图
SELECT * FROM v_stu_class1;2,视图的操作
(1)视图的查询
查询所有的视图:select * from 视图名 where 数据库名;
查看刚刚建立的视图:show create view 视图名
(2)删除储存过程:drop view 视图名
-- 查看所有视图
SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = 'day002';
-- 查看视图名称
SHOW CREATE VIEW v_stu_class1;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost`
SQL SECURITY DEFINER VIEW `v_stu_class1` AS
select `student`.`Sid` AS `Sid`,
`student`.`Sname` AS `Sname`,
`student`.`birthday` AS `birthday`,
`student`.`Ssex` AS `Ssex`,
`student`.`classid` AS `classid`
from `student` where (`student`.`classid` = 1)
-- 创建视图
CREATE VIEW v_vstu_class AS SELECT v_stu_class1.*,classname FROM v_stu_class1 LEFT JOIN class ON v_stu_class1.classid = class.classid;
-- 使用视图
SELECT * FROM v_vstu_class;
-- 删除一个试图
DROP VIEW v_stu_class1;3,视图的作用
(1)简化查询
(2)重写格式化数据
(3)频繁访问数据库
(4)过滤数据
4,储存过程的作用:
(1)业务流复杂:业务复杂时,SQL语句相互依赖,顺序执行。
(2)频繁访问数据库:每条SQL语句都需要单独连接和访问数据库
(3)先编译后执行:SQL语句的执行需要先编译
语法:
create procedure (
[[in 或 out 或 inout] 参数名 参数类型...] );
begin
declare 变量 变量类型
end
5,储存过程的操作
(1)调用储存过程 call 储存过程名 [参数名]
(2)查看储存过程 select * from 储存过程名 where 数据库名
(3)删除储存过程 drop procedure 储存过程名
# 储存过程
-- 定界符
-- delimiter $$
-- CREATE PROCEDURE 储存过程名(形参列表)
-- BEGIN
-- sql 语句集; 分号说明每条sql语句的结束 失效
-- END $$
-- delimiter
-- {
-- }
-- 创建存储过程
delimiter $$
CREATE PROCEDURE proc_stu()
BEGIN
SELECT * FROM student;
END $$
delimiter;
-- 使用存储过程
CALL proc_stu();
-- 有参储存过程
-- 创建有参过程
delimiter $$
CREATE PROCEDURE proc_test(
IN x INT, -- IN 只入参数
OUT y INT, -- OUT 只出参数
INOUT z INT -- INOUT 进出参数
)
BEGIN
SET x = x+1;
SET y = y+10;
SET z = z+100;
END $$
delimiter ;
-- @xx环境变量 @@xx 全局变量
SET @a = 10;
SET @b = 20;
SET @c = 30;
SELECT @a,@b,@c;
CALL proc_test(@a,@b,@c);
-- 案例分页
delimiter $$
CREATE PROCEDURE proc_stu_page(
IN curpage INT,
IN sizepage INT,
OUT sumcount INT,
OUT sumpage INT
)
BEGIN
-- 定义一个变量
DECLARE x INT;
SET x = (curpage-1) * sizepage;
SELECT COUNT(*) FROM student INTO sumcount;
SET sumpage = ceiling(sumcount / sizepage);
SELECT * FROM student LIMIT x,sizepage;
END $$
delimiter;
SET @a = 1;
SET @b = 3;
SET @c = 0;
SET @d = 0;
SELECT @a,@b,@c,@d;
CALL proc_stu_page(@a,@b,@c,@d)6,储存过程与函数的区别
| 储存过程 | 函数 | |
|---|---|---|
| 语法 | 关键字:procedure | 关键字:function |
| 执行 | 可以独立执行 | 必须依赖函数表达式的调用 |
| 返回值 | 可以定义多个返回值 | 只能有一个返回值 |
| 功能 | 可以做复杂的业务逻辑 | 不易做复杂的业务逻辑 |
7,储存过程的缺陷
(1)移植性:大多数关系型数据库的储存过程存在的细微差异。
(2)维护性:储存过程的维护成本高,修改调试较为麻烦。
(3)协作性:没有相关的版本控制或者IDE,团队中对于存储过程的使用大多是依赖文档。
8,触发器
概念:触发器是数据库中针对数据库表操作触发的特殊的存储过程。是一种自动执行切与表相关联的储存过程。
语法:
create trigger 数据库.触发器
before/after -- 触发顺序
insert/update/delete -- 触发事件
on each bow
begin
触发器内容 -- 事件出发后要写的语句
end $$
# 触发器
delimiter $$
CREATE TRIGGER 触发器名称
执行顺序 事件 ON 表名 FOR EACH ROW
BEGIN
触发的语句
END $$
delimiter;
BEFORE 事件 ON student;
删除一个学生 删除成绩成绩 sid
delimiter $$
CREATE TRIGGER trig_delstu_delsc
BEFORE DELETE ON student FOR EACH ROW
BEGIN
-- old 原来就有的数据 new 原来没有新来的数据
delete from sc where sid = old.sid;
END $$
delimiter;
-- 删除语句
DELETE FROM student WHERE sname = '赵雷';
select * from student;
select * from sc;
-- 查看库中的触发器
SELECT * FROM information_schema.`TRIGGERS`
WHERE trigger_schema = 'myschool';
-- 查看触发器的sql语句
show create trigger trig_delstu_delsc;
-- 删除触发器
drop trigger trig_delstu_delsc;
9,储存过程和触发器的区别
| 储存过程 | 触发器 | |
|---|---|---|
| 语法 | 关键字:procedure | 关键字:trigger |
| 执行 | 需要调用才执行 | 自动执行 |
| 返回值 | 可以定义返回值 | 没有返回值 |
| 功能 | 是一组特定的SQL语句 | 在SQL语句前后执行 |
边栏推荐
- Wang Shuang's detailed learning notes of assembly language II: registers
- If the position is absolute, touchablehighlight cannot be clicked - touchablehighlight not clickable if position absolute
- Investment should be calm
- How to use Moment. JS to check whether the current time is between 2 times
- C language do while loop classic Level 2 questions
- 数字电路基础(五)算术运算电路
- Quaternion -- basic concepts (Reprint)
- Thinking about three cups of tea
- Rearrange spaces between words in leetcode simple questions
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
猜你喜欢

UCORE lab2 physical memory management experiment report

ucore lab7 同步互斥 实验报告

ucore lab1 系统软件启动过程 实验报告

Wang Shuang's detailed learning notes of assembly language II: registers

The minimum number of operations to convert strings in leetcode simple problem

Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)

Daily code 300 lines learning notes day 9

How to rename multiple folders and add unified new content to folder names

What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?

数字电路基础(二)逻辑代数
随机推荐
Logstack introduction and deployment -- elasticstack (elk) work notes 019
Report on the double computer experiment of scoring system based on 485 bus
Fundamentals of digital circuits (III) encoder and decoder
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
Login the system in the background, connect the database with JDBC, and do small case exercises
UCORE lab2 physical memory management experiment report
Leetcode simple question: check whether two strings are almost equal
Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025
The minimum sum of the last four digits of the split digit of leetcode simple problem
Flash implements forced login
What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
[issue 18] share a Netease go experience
ucore lab2 物理内存管理 实验报告
C language do while loop classic Level 2 questions
Emqtt distribution cluster and node bridge construction
"If life is just like the first sight" -- risc-v
全网最详细的postman接口测试教程,一篇文章满足你
ByteDance ten years of experience, old bird, took more than half a year to sort out the software test interview questions
Numpy快速上手指南
Rearrange spaces between words in leetcode simple questions