当前位置:网站首页>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

原网站

版权声明
本文为[Hand pluckable Xinchen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060919160100.html