Detailed explanation of MySql view trigger stored procedure

Detailed explanation of MySql view trigger stored procedure

view:

When a temporary table is used repeatedly, you can give it an alias to facilitate future use. You can then create a view, and the alias is the name of the view. A view is just a virtual table whose data is dynamically read from the physical table, so changes to the physical table will change the view.

create:

create view v1 as SQL

For example: create view v1 as select * from student where sid<10

After creation, if you use the MySQL terminal, you can see a table called v1. If you use navicate, you can see that a view called v1 is generated in the view.

When you use it again, you can directly use the query table method. For example: select * from v1

Modification: You can only modify the SQL statements in the view

alter view view name as sql

delete:

drop view view name

trigger:

When adding, deleting, modifying, or checking a table (before, after, or after), you can use triggers to customize the associated behavior.

Modify the terminator delimiter in the SQL statement

before after

--delimiter //
  
-- before or after defines the operation before or after the operation (insert or other) -- on represents the trigger operation after the operation occurs on that table -- CREATE TRIGGER t1 BEFORE INSERT on teacher for EACH row
--BEGIN
-- INSERT into course(cname) VALUES('Ultraman');
--END //
--delimiter;
 
 
-- insert into teacher(tname) VALUES('triggertest111')
--
--delimiter // 
-- CREATE TRIGGER t1 BEFORE INSERT on student for EACH row
--BEGIN
-- INSERT into teacher(tname) VALUES('Ultraman');
--END //
--delimiter;
 
 
-- insert into student(gender,sname,class_id) VALUES('男','1小刚111',3);
-- Delete trigger -- drop trigger t1;
 
 
-- NEW and OLD represent new and old data to make them consistent -- delimiter //
-- create TRIGGER t1 BEFORE insert on student for each row
--BEGIN
 
--Here new specifies the newly inserted data, and old is usually used for delete -- insert into teacher(tname) VALUES(NEW.sname);
-- end //
--delimiter;
insert into student(gender,sname,class_id) VALUES('男','蓝大蟹',3);

Stored Procedure:

Essentially, it is a collection of SQL statements, and then this collection is given an alias. The difference between a view and a table is that a view is a SQL query statement treated as a table.

Way:

1 msyql----stored procedure for program calls

2 msyql---Do not do stored procedures, the program writes sql

3 mysql--do not do stored procedures, program write classes and objects (converted into sql statements)

Creation method:

-- 1 Create a stored procedure without parameters
--delimiter //
-- create PROCEDURE p1()
--BEGIN
-- select * from student;
-- insert into teacher(tname) VALUES('cccc');
-- end //
--delimiter;

-- Calling a stored procedure

 call p2(5,2)<br data-filtered="filtered"><br data-filtered="filtered"><em id="__mceDel"> cursor.callproc('p1',(5,2)) in pymysql</em>
-- 2 with parameter in parameter -- delimiter //
-- create PROCEDURE p2(
-- in n1 int,
-- in n2 int
-- )
--BEGIN
-- select * from student where sid<n1;
-- 
-- end //<br data-filtered="filtered"><br data-filtered="filtered"> call p2(5,2)<br data-filtered="filtered"><br data-filtered="filtered"><em id="__mceDel"> cursor.callproc('p1',(5,2)) in pymysql</em>
-- 3 out parameter When using out as a parameter in a stored procedure, the variable can be called externally -- There is no return in the stored procedure. If you want to call a variable externally, you need to use out
--delimiter //
-- create PROCEDURE p3(
-- in n1 int,
--out n2 int
-- )
--BEGIN
--set n2=444444;
-- select * from student where sid<n1;
-- 
-- end //
--
--delimiter;
--
-- set @v1=999 is equivalent to creating a variable at the session level -- set @v1=999; 
-- call p3(5,@v1);
-- select @v1; #By passing a variable in and then monitoring this variable, you can monitor whether the stored procedure is executed successfully -- in pymsyql --  
-- cursor.callproc('p3',(5,2))
-- r2=cursor.fetchall()
-- print(r2)
--
-- The stored procedure contains the out keyword. If you want to get the return value, cursor.execute('select @_p3_0,@_p3_1')
-- # Among them, 'select @_p3_0,@_p3_1' is a fixed writing method select @_stored procedure name_input parameter index position-- cursor.execute('select @_p3_0,@_p3_1')
-- r3=cursor.fetchall()
-- print(r3)
--

Why do we need out to forge the returned value when we have a result set?

Because the stored procedure contains multiple SQL statements, it is impossible to determine whether all SQL statements can be executed successfully. The out feature is used to identify whether the SQL statement is executed successfully.

For example, if success is marked as 1, partial success is marked as 2, and failure is marked as 3

Transactions in stored procedures:

Transactions:

It is called atomic operation. DML (insert, update, delete) statements are completed together, and transactions are only related to DML statements, or only DML has transactions.

Features of transactions:

Atomicity A: A transaction is the smallest unit and cannot be divided.

Consistency C: Transactions require that all DML statements must succeed or fail when they are executed.

Isolation I: There is isolation between transaction A and transaction B

Persistence D: It is the guarantee of the transaction and the sign of the end of the transaction (the data in the memory is completely saved to the hard disk)

Transaction keywords:

Start transaction: start transaction

End of transaction: end transaction

commit transaction

Rollback transaction

Basic operations of transactions

delimiter //
 create procedure p5(
 in n1 int,
 out n2 int
 )
 begin
 1 Statement if abnormal execution occurs (
   set n2=1;
   rollback;
  )
  2 Start transaction Buyer account - 100
        Selling account +100
        commit
  3 End set n2=2
   end //
   
   delimiter ;
   
 In this way, you can detect errors through n2 and roll back. The following is the detailed code delimiter //
  create procedure p6(
  out code TINYINT
  )
  begin
   Declare that if a SQLException is encountered, execute the following operation DECLARE exit HANDLER for SQLEXCEPTION
   begin
    --error
      set code=1;
      rollback;
   end;
   START TRANSACTION;
       delete from tb1;
       insert into tb2(name)values('slkdjf')
   commit;
   ---success
   code=2
   end //
delimiter ;

Use of cursors in stored procedures:

delimiter //
create procedure p7()
 begin
    declare row_id int;
     declare row_num int;
     declare done int DEFAULT FALSE;
     Declare a cursor declare my_cursor cursor for select id,num from A;
     Declare that if there is no data, set done to True
     declare continue handler for not found set done=True;
     
     
     open my_cursor; open cursor xxoo; LOOP start loop called xxoo
            fetch my_cursor into row_id,row_num;
              if done then if done is True leave the loop leave xxoo;
              end if;
              set temp=row_id+row_num;
       insert into B(number)VALUES(temp);
            end loop xxoo; close the loop close my_cursor;
    end //
     
delimiter ;
 
 
The above code is converted into Python 
for row_id,row_num in my_cursor:
  Check if there is any data in the loop, if not, break
    break
    insert into B(num) values(row_id+row_num)

Dynamically execute SQL and place SQL injection at the database level:

delimiter \\
create procedure p6(
 in nid int)
 begin
  1 Precompile (pre-check) the legitimacy of a certain sql statement 2 sql=format tpl+arg
    3 Execute SQL
 
  set @nid=nid
    prepare prod from 'select * from student where sid>?'
    EXECUTE prod using @nid;
    deallocate prepare prod
  end \\
  delimiter ;

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL series five views, stored functions, stored procedures, triggers
  • Tutorial on basic functions and triggers in MySQL stored procedures
  • Application analysis based on mysql transactions, views, stored procedures, and triggers
  • Take you to understand the event scheduler EVENT in MySQL
  • Introduction to using MySQL event scheduler
  • Getting Started Guide for MySQL Stored Procedures, Triggers, and Event Schedulers

<<:  How to connect Xshell5 to Linux in a virtual machine and how to solve the failure

>>:  JS implements random generation of verification code

Recommend

Teach you how to implement Vue3 Reactivity

Table of contents Preface start A little thought ...

CSS web page responsive layout to automatically adapt to PC/Pad/Phone devices

Preface There are many devices nowadays, includin...

Examples of correct use of maps in WeChat mini programs

Table of contents Preface 1. Preparation 2. Actua...

Vue parent component calls child component function implementation

Vue parent component calls the function of the ch...

Detailed explanation of crontab scheduled execution command under Linux

In LINUX, periodic tasks are usually handled by t...

Ubuntu 20.04 CUDA & cuDNN Installation Method (Graphical Tutorial)

CUDA installation download cuda Enter the nvidia-...

HTML commonly used meta encyclopedia (recommended)

The Meta tag is an auxiliary tag in the head area...

Detailed explanation of primary keys and transactions in MySQL

Table of contents 1. Comments on MySQL primary ke...

How to simulate enumeration with JS

Preface In current JavaScript, there is no concep...

How to use Navicat to operate MySQL

Table of contents Preface: 1. Introduction to Nav...

Detailed explanation of Nginx rewrite jump application scenarios

Application scenario 1: Domain name-based redirec...

MySQL 8.0.13 decompression version installation graphic tutorial under Windows

This article shares with you the MySQL 8.0.13 ins...