当前位置:网站首页>Mysql database (V) views, stored procedures and triggers
Mysql database (V) views, stored procedures and triggers
2022-07-06 15:11:00 【Hand pluckable Xinchen】
1, View
Concept : A view is a virtual table exported from one or more tables
grammar :create view view_name as select...
explain :
view_name: Self defined view name
as: The following is the query results used in this view
# View
-- Create view
-- CREATE VIEW Trial drawing name AS Inquire about sql sentence ;
CREATE VIEW v_stu_class1 AS SELECT * FROM student WHERE classid =1;
-- Try to use
SELECT * FROM v_stu_class1;
2, View operation
(1) View query
Query all views :select * from View name where Database name ;
View the view just created :show create view View name
(2) Delete stored procedure :drop view View name
-- View all views
SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = 'day002';
-- View view name
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
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;
-- Use view
SELECT * FROM v_vstu_class;
-- Delete an attempt
DROP VIEW v_stu_class1;
3, The function of view
(1) Simplify queries
(2) Rewrite formatted data
(3) Frequent access to the database
(4) Filtering data
4, The role of the storage process :
(1) The business flow is complex : When the business is complex ,SQL Statements are interdependent , Sequential execution .
(2) Frequent access to the database : Every one of them SQL Statements need to connect and access the database separately
(3) Compile before execute :SQL The execution of the statement requires compiling
grammar :
create procedure (
[[in or out or inout] Parameter name Parameter type ...] );
begin
declare Variable Variable type
end
5, Operation of storage process
(1) Call the stored procedure call Stored procedure name [ Parameter name ]
(2) Look at the stored procedure select * from Stored procedure name where Database name
(3) Delete stored procedure drop procedure Stored procedure name
# Storage process
-- delimiter
-- delimiter $$
-- CREATE PROCEDURE Stored procedure name ( Parameter list )
-- BEGIN
-- sql Statements set ; Semicolons indicate each sql The end of the statement invalid
-- END $$
-- delimiter
-- {
-- }
-- Create stored procedure
delimiter $$
CREATE PROCEDURE proc_stu()
BEGIN
SELECT * FROM student;
END $$
delimiter;
-- Using stored procedures
CALL proc_stu();
-- Participate in the storage process
-- Create a parametric process
delimiter $$
CREATE PROCEDURE proc_test(
IN x INT, -- IN Only enter parameters
OUT y INT, -- OUT Only parameters
INOUT z INT -- INOUT In and out parameters
)
BEGIN
SET x = x+1;
SET y = y+10;
SET z = z+100;
END $$
delimiter ;
-- @xx environment variable @@xx Global variables
SET @a = 10;
SET @b = 20;
SET @c = 30;
SELECT @a,@b,@c;
CALL proc_test(@a,@b,@c);
-- Case pagination
delimiter $$
CREATE PROCEDURE proc_stu_page(
IN curpage INT,
IN sizepage INT,
OUT sumcount INT,
OUT sumpage INT
)
BEGIN
-- Define a variable
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, The difference between stored procedures and functions
Storage process | function | |
---|---|---|
grammar | keyword :procedure | keyword :function |
perform | It can be executed independently | Must rely on function expression calls |
Return value | You can define multiple return values | There can only be one return value |
function | Can do complex business logic | It is not easy to do complex business logic |
7, Defects in the storage process
(1) portability : There are subtle differences in the storage process of most relational databases .
(2) Maintenance : The maintenance cost of the storage process is high , It is troublesome to modify and debug .
(3) Collaboration : There is no relevant version control or IDE, The use of stored procedures in the team mostly depends on documents .
8, trigger
Concept : Trigger is a special stored procedure triggered by database table operation . It is a kind of storage process that automatically executes the connection with the table .
grammar :
create trigger database . trigger
before/after -- Trigger sequence
insert/update/delete -- Triggering event
on each bow
begin
Trigger content -- The statement to be written after the event
end $$
# trigger
delimiter $$
CREATE TRIGGER Trigger Name
Execution order event ON Table name FOR EACH ROW
BEGIN
Triggered statement
END $$
delimiter;
BEFORE event ON student;
Delete a student Delete grades sid
delimiter $$
CREATE TRIGGER trig_delstu_delsc
BEFORE DELETE ON student FOR EACH ROW
BEGIN
-- old There was data new There was no new data
delete from sc where sid = old.sid;
END $$
delimiter;
-- Delete statements
DELETE FROM student WHERE sname = ' Zhao Lei ';
select * from student;
select * from sc;
-- View triggers in the Library
SELECT * FROM information_schema.`TRIGGERS`
WHERE trigger_schema = 'myschool';
-- Check the trigger sql sentence
show create trigger trig_delstu_delsc;
-- Delete trigger
drop trigger trig_delstu_delsc;
9, The difference between stored procedures and triggers
Storage process | trigger | |
---|---|---|
grammar | keyword :procedure | keyword :trigger |
perform | Call required to execute | Automatic execution |
Return value | You can define the return value | no return value |
function | Is a specific set SQL sentence | stay SQL Execute before and after the statement |
边栏推荐
- Fundamentals of digital circuits (I) number system and code system
- Global and Chinese market of goat milk powder 2022-2028: Research Report on technology, participants, trends, market size and share
- 自动化测试中敏捷测试怎么做?
- Rearrange spaces between words in leetcode simple questions
- Global and Chinese market of RF shielding room 2022-2028: Research Report on technology, participants, trends, market size and share
- CSAPP家庭作業答案7 8 9章
- ucore lab8 文件系统 实验报告
- Réponses aux devoirs du csapp 7 8 9
- STC-B学习板蜂鸣器播放音乐2.0
- Cc36 different subsequences
猜你喜欢
Sorting odd and even subscripts respectively for leetcode simple problem
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
Logstack introduction and deployment -- elasticstack (elk) work notes 019
The maximum number of words in the sentence of leetcode simple question
Cadence physical library lef file syntax learning [continuous update]
STC-B学习板蜂鸣器播放音乐2.0
Install and run tensorflow object detection API video object recognition system of Google open source
Software testing interview summary - common interview questions
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
随机推荐
自动化测试你必须要弄懂的问题,精品总结
My first blog
Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share
Servlet
遇到程序员不修改bug时怎么办?我教你
Example 071 simulates a vending machine, designs a program of the vending machine, runs the program, prompts the user, enters the options to be selected, and prompts the selected content after the use
How to transform functional testing into automated testing?
Sleep quality today 81 points
Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
Introduction to variable parameters
ucore lab6 调度器 实验报告
MySQL数据库(二)DML数据操作语句和基本的DQL语句
MySQL数据库(一)
Why can swing implement a form program by inheriting the JFrame class?
Keil5 MDK's formatting code tool and adding shortcuts
Rearrange spaces between words in leetcode simple questions
全网最详细的postman接口测试教程,一篇文章满足你
Programmers, how to avoid invalid meetings?
{1,2,3,2,5} duplicate checking problem
Capitalize the title of leetcode simple question