当前位置:网站首页>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 |
边栏推荐
- With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
- UCORE lab8 file system experiment report
- ucore lab2 物理内存管理 实验报告
- Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
- “Hello IC World”
- Report on the double computer experiment of scoring system based on 485 bus
- [Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
- Wang Shuang's detailed learning notes of assembly language II: registers
- CSAPP家庭作業答案7 8 9章
- [HCIA continuous update] working principle of static route and default route
猜你喜欢
Summary of thread implementation
Investment operation steps
Video scrolling subtitle addition, easy to make with this technique
转行软件测试必需要知道的知识
What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
The latest query tracks the express logistics and analyzes the method of delivery timeliness
[200 opencv routines] 98 Statistical sorting filter
In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
Don't you even look at such a detailed and comprehensive written software test question?
随机推荐
想跳槽?面试软件测试需要掌握的7个技能你知道吗
Wang Shuang's detailed learning notes of assembly language II: registers
In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
Don't you even look at such a detailed and comprehensive written software test question?
Portapack application development tutorial (XVII) nRF24L01 launch B
Réponses aux devoirs du csapp 7 8 9
Global and Chinese markets of PIM analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
The minimum sum of the last four digits of the split digit of leetcode simple problem
Video scrolling subtitle addition, easy to make with this technique
如何成为一个好的软件测试员?绝大多数人都不知道的秘密
Thinking about three cups of tea
Face and eye recognition based on OpenCV's own model
Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
Summary of thread implementation
Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 2022-2028: Research Report on technology, participants, trends, market size and share
自动化测试你必须要弄懂的问题,精品总结
[pytorch] simple use of interpolate
Programmers, how to avoid invalid meetings?
Global and Chinese market for antiviral coatings 2022-2028: Research Report on technology, participants, trends, market size and share
接口测试面试题及参考答案,轻松拿捏面试官