Simple examples of creating stored procedures, triggers and using cursors in Navicat (pictures and text)

Simple examples of creating stored procedures, triggers and using cursors in Navicat (pictures and text)

1. Create tables <br /> First, create two tables (users table and number table). The specific design is as follows:
1.jpg
2.jpg

2. Stored procedure <br /> Write a stored procedure to insert data into the users table. The creation process is as follows:
3.jpg
4.jpg
5.jpg

The code is as follows

BEGIN
 #Routine body goes here...
 declare n bigint;
 set n = 201121029684;
 while n <= 201121029694
 do
 insert into users(student_ID) values(n);
 set n = n + 1;
 end while;
END

6.jpg
After executing the stored procedure, you can see the data in the users table as follows:
7.jpg

The entire stored procedure has been written. Of course, this is just a very simple example and is only for reference.

3. Triggers

Before writing the trigger, we first clear the users data

truncate table users;

Now we have two tables. What I need to do is, when I insert data into users, number will also change accordingly:

For example, there is an initial data in the number table, id=1, num=0;

When I insert a piece of data into users, the num field in the number table is triggered to increase by 1, which means the number of users is recorded.

Let's implement this little functionality.

Right-click the users table and select Design Table

8.jpg
Select Trigger Options
9.jpg
10.jpg
11.jpg

begin
 update number
 set num = (select count(*) from users) ;
end

After saving, add new data to the users table and check the data in number. You will magically find that the data in the number table has also changed. Try it yourself!

ps: stored procedures need to be executed by programmers themselves, while triggers, as the name suggests, are automatically triggered.

4. Use of cursor <br /> What I want to do now is to add 100 to the student_ID field. I will use this example to simply demonstrate the use of cursors.

Create a stored procedure. Refer to the above steps for the creation method. The stored procedure code is as follows:

12.jpg

BEGIN
 #Routine body goes here...

 declare tmp bigint default 0;
 declare cur CURSOR FOR SELECT student_ID FROM users; -- define the cursor/*
 02000 One of the following exceptions occurred: 
 The result of a SELECT INTO statement or a subquery of an INSERT statement is an empty table. 
 The number of rows identified within the searched UPDATE or DELETE statement is zero. 
 The cursor position referenced in the FETCH statement is after the last row of the result table.


 */
 declare CONTINUE HANDLER FOR SQLSTATE '02000' set tmp = 0; 


 OPEN cur; -- Open the cursor FETCH cur INTO tmp; -- Move the cursor down one step WHILE (tmp != 0)
 DO 
 select tmp; -- Print out tmp and you will find that tmp is like a pointer. It points to the first row at the beginning. When the cursor moves one step, it points to the next row of records UPDATE users
 SET student_ID = tmp + 100
 WHERE student_ID = tmp;
 FETCH cur INTO tmp;
 END WHILE;

 CLOSE cur; -- Close the cursor END

Execute the above stored procedure and you will find that the data in users has changed as you wish.

You may also be interested in:
  • How to use Navicat to create a database and connect with JDBC
  • Tutorial on installing MySQL database and using Navicat for MySQL
  • Simple tutorial on using Navicat For MySQL
  • Problems with changing password and connecting to Navicat when installing and using MySQL 8.0.16 under Windows 7
  • Import csv file into mysql using navicat
  • Use Navicate to connect to MySQL on Alibaba Cloud Server
  • Use Navicat Premium to export SQLServer data to sql format
  • Use Navicat 8 to create a database and import data to manage users and permissions [Graphic Method]
  • Detailed explanation of the simple use of Navicat

<<:  CentOS 7 configuration Tomcat9+MySQL solution

>>:  The difference between clientWidth, offsetWidth, scrollWidth in JavaScript

Recommend

Example of using CSS filter to write mouse over effect

Use CSS filter to write mouse over effect <div...

How React Hooks Work

Table of contents 1. React Hooks vs. Pure Functio...

HTML Basic Notes (Recommended)

1. Basic structure of web page: XML/HTML CodeCopy...

Summary of Linux file directory management commands

touch Command It has two functions: one is to upd...

Detailed explanation of how to use WeChat mini program map

This article example shares the specific implemen...

Introduction to ApplicationHost.config (IIS storage configuration area file)

For a newly created website, take ASP.NET MVC5 as...

How to resize partitions in CentOS7

Yesterday, I helped someone install a system and ...

Learn to deploy microservices with docker in ten minutes

Since its release in 2013, Docker has been widely...

Vue image cropping component example code

Example: tip: This component is based on vue-crop...

Some tips on deep optimization to improve website access speed

Some tips for deep optimization to improve websit...

This article teaches you how to play with CSS combination selectors

CSS combination selectors include various combina...