当前位置:网站首页>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 |
边栏推荐
- The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
- Introduction to variable parameters
- Rearrange spaces between words in leetcode simple questions
- Collection集合与Map集合
- How to use Moment. JS to check whether the current time is between 2 times
- Global and Chinese market of pinhole glossmeter 2022-2028: Research Report on technology, participants, trends, market size and share
- "If life is just like the first sight" -- risc-v
- Keil5 MDK's formatting code tool and adding shortcuts
- UCORE lab8 file system experiment report
- pytest
猜你喜欢
Database monitoring SQL execution
软件测试需求分析之什么是“试纸测试”
China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
How to transform functional testing into automated testing?
Query method of database multi table link
Fundamentals of digital circuits (I) number system and code system
[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
[200 opencv routines] 98 Statistical sorting filter
Dlib detects blink times based on video stream
Install and run tensorflow object detection API video object recognition system of Google open source
随机推荐
[pointer] solve the last person left
CSAPP家庭作业答案7 8 9章
[pytorch] simple use of interpolate
几款开源自动化测试框架优缺点对比你知道吗?
Statistics 8th Edition Jia Junping Chapter 1 after class exercises and answers summary
MySQL数据库(二)DML数据操作语句和基本的DQL语句
Video scrolling subtitle addition, easy to make with this technique
Es full text index
Oracle foundation and system table
Don't you even look at such a detailed and comprehensive written software test question?
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
Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share
Install and run tensorflow object detection API video object recognition system of Google open source
How to transform functional testing into automated testing?
UCORE lab1 system software startup process experimental report
CSAPP家庭作業答案7 8 9章
ucore lab7 同步互斥 实验报告
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
Software testing interview summary - common interview questions
“Hello IC World”