MySQL batch adding and storing method examples

MySQL batch adding and storing method examples

When logging in to the stress test, many different users are required, and new data needs to be added to the database

#Batch add user accounts - stored procedure:
delimiter //
drop procedure if exists test;
create procedure test()
 
begin
DECLARE i int;
set i = 1;
while i<21 do
insert into hg_user values ​​(concat("OM_TEST",cast(i as CHAR)),concat("OM_TEST",cast(i as CHAR)),"F1B2F5B9FBC8B513",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
set i = i+1;
end while;
select * from test;
end//
call test();

delimiter is the MySQL delimiter. The default delimiter in the MySQL client is a semicolon (;).

If you input more statements at one time and there is a semicolon in the middle of the statement, you need to specify a special separator, commonly used ones are // and &&.

The above is, first set the separator to //,

The statement is not executed as a whole until the next // is encountered.

After execution, the last line, delimiter; resets the MySQL delimiter to a semicolon.

If not modified, all separators in this session will be based on //.

concat is a character concatenation, which connects multiple strings into one string.

Syntax: concat(str1, str2,...)

eg:select concat (id, name, score) as info from tt2; 1Xiao Ming 60

The cast function is used to explicitly convert an expression of one data type to another data type.

Syntax: CAST (expression AS data_type)

There are restrictions on the types that can be converted. The type can be one of the following values:

  • Binary, with the effect of binary prefix: BINARY
  • Character type, with parameters: CHAR()
  • Date: DATE
  • Time: TIME
  • Date and time type: DATETIME
  • Floating point numbers: DECIMAL
  • Integer: SIGNED
  • Unsigned integer: UNSIGNED

Batch deletion solution (the same applies to deleting users)

#Delete solution - stored procedure;
delimiter //
drop procedure if exists test;
create procedure test()
 
begin
DECLARE i int;
set i = 1;
while i<11 do
DELETE from hg_application_flow_template where user_name=concat("OM_TEST",cast(i as CHAR));
DELETE from hg_application_flow_template_details where created_by=concat("OM_TEST",cast(i as CHAR));
set i = i+1;
end while;
select * from test;
end//
call test();

Summarize

This is the end of this article about MySQL batch addition and storage. For more relevant MySQL batch addition and storage content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Mysql uses insert to insert multiple records to add data in batches
  • Mybatis learning road mysql batch add data method
  • Detailed explanation of the batch query design pattern for MySQL sharding to achieve distributed storage of millions of records

<<:  How to hide the border/separation line between cells in a table

>>:  Vue complete code to implement single sign-on control

Recommend

Implementation example of Nginx+Tomcat load balancing cluster

Table of contents introduction 1. Case Overview 2...

Several reasons for not compressing HTML

The reason is simple: In HTML documents, multiple ...

How to start jar package and run it in the background in Linux

The Linux command to run the jar package is as fo...

How to solve the problem of clicking tomcat9.exe crashing

A reader contacted me and asked why there were pr...

Implementation code of short video (douyin) watermark removal tool

Table of contents 1. Get the first link first 2. ...

Share 16 burning flame effect English fonts treasure trove

We live in a visual world and are surrounded by m...

Summary of essential knowledge points for MySQL query optimization

Preface Query optimization is not something that ...

Introduction to installing JDK under Linux, including uninstalling OpenJDK

1. View openjdk rpm -qa|grep jdk 2. Delete openjd...

How to use CURRENT_TIMESTAMP in MySQL

Table of contents Use of CURRENT_TIMESTAMP timest...

How to install JDK and Mysql on Ubuntu 18.04 Linux system

Platform deployment 1. Install JDK step1. Downloa...

The difference between absolute path and relative path in web page creation

1. Absolute path First of all, on the local compu...

How to use the Linux nl command

1. Command Introduction nl (Number of Lines) adds...