How to batch generate MySQL non-duplicate mobile phone number table example code

How to batch generate MySQL non-duplicate mobile phone number table example code

Preface

In many MySQL test scenarios, some test data needs to be manually generated for testing. This article provides a procedure for constructing a MySQL large table storage, which can generate fields including user name, mobile phone number, date of birth, etc. You can also filter out duplicate mobile phone numbers to simulate real-life scenarios.

1. Generate Script

Build Instructions:

The following stored procedure is used to batch generate a large table containing fields such as user name, mobile phone number, date of birth, etc.

This stored procedure uses uid as the primary key, so a small number of duplicate mobile phone numbers will be generated. A duplicate filtering SQL script is provided later.

If you want to generate unique mobile phone numbers at one time, you can consider modifying the following script, removing the uid and using the mobile column as the primary key.

DROP TABLE IF EXISTS big_table;

DROP PROCEDURE IF EXISTS prc_gen_user;

CREATE TABLE `big_table` (
 `uid` int(11) NOT NULL AUTO_INCREMENT,
 `mobile` char(11) DEFAULT NULL,
 `passwd` varchar(50) DEFAULT NULL,
 `name` varchar(50) DEFAULT NULL,
 `sex` tinyint DEFAULT NULL,
 `birthday` datetime DEFAULT NULL,
 `updated_time` datetime DEFAULT NULL,
 PRIMARY KEY (`uid`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE PROCEDURE prc_gen_user(l_cnt int)
BEGIN
 DECLARE x INT DEFAULT 0;
 DECLARE p char(11);

 WHILE x < l_cnt
 DO
 SET x = x + 1;
 SET p =
  concat('1',
   substring(cast(3 + (rand() * 10) % 7 AS char(50)), 1, 1),
   right(left(trim(cast(rand() AS char(50))), 11), 9));

 INSERT INTO big_table(mobile,
    passwd,
    name,
    sex,
    birthday,
    updated_time)
  VALUES (
   p,
   md5(ceiling(rand() * 1000000)),
   concat(
   substring(
    'Zhao Qiansun Li Zhou Wu Zheng Wang Feng Chen Zhu Wei Jiang Chen Han Yang Zhu Qin You Xu He Lu Shi Zhang Kong Cao Yan Hua Jin Wei Tao Jiang Qi Xie Zou Yu Bai Shui Dou Zhang Yun Su Pan Ge Xi Fan Peng Lang Lu Wei Chang Ma Miao Feng Hua Fang Yu Ren Yuan Liu Feng Bao Shi Tang Fei Lian Cen Xue Lei He Ni Tang Teng Yin Luo Bi Hao Wu An Chang Le Yu Shi Fu Pi Qi Kang Wu Yu Yuan Bu Gu Meng Ping Huang He Mu Xiao Yin Yao Shao Kan Wang Qi Mao Yu Di Mi Bei Ming Zang Ji Fu Cheng Dai Tan Song Mao Pang Xiong Ji Shu Qu Xiang Zhu Dong Liang Du Ruan Lan Min Xi Ji Ma Qiang Jia Lu Lou Wei Jiang Tong Yan Guo Mei Sheng Lin Diao Zhong Xu Qiu Luo Gao Xia Cai Tian Fan Hu Ling Huo Yu Wan Zhi Ke Jiu Guan Lu Mo Jing Fang Qiu Gan Jie Ying Zong Ding Xuan Ben Deng Yu Shan Hang Hong Bao Zhu Zuo Shi Cui Ji Niu Gong',
    floor(1 + 190 * rand()),
    1),
   substring(
    : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
    floor(1 + 400 * rand()),
    1),
   substring(
    : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
    floor(1 + 400 * rand()),
    1)),
   ceiling(rand() * 10) % 2,
   date(
   now()
   - INTERVAL (20 + ceiling(rand() * 100) % 40) YEAR),
   concat('2018-',
    1 + ceiling(rand() * 100) % 12,
    '-',
    1 + ceiling(rand() * 100) % 28))
 ON DUPLICATE KEY UPDATE updated_time = now();
 END WHILE;
END

2. Data filling

call prc_gen_user(1000);
Query OK, 1 row affected (1.38 sec)

select count(*) from big_table;
+----------+
| count(*) |
+----------+
| 1000 |
+----------+
1 row in set (0.00 sec)
select 'Leshami' author,'http://blog.csdn.net/leshami' Blog;
+---------+------------------------------+
| author | Blog |
+---------+------------------------------+
| Leshami | http://blog.csdn.net/leshami |
+---------+------------------------------+

3. Filtering Duplicate SQL Statements

DELETE FROM big_table
WHERE mobile IN (SELECT mobile
   FROM (SELECT u1.mobile
    FROM big_table u1
    GROUP BY u1.mobile
    HAVING count(*) > 1) a)
 AND uid NOT IN (SELECT uid
   FROM (SELECT min(u2.uid) AS uid
    FROM big_table u2
    GROUP BY u2.mobile
    HAVING count(*) > 1) b);

IV. Others

This article refers to the following code, which implements MySQL batch creation of user data, name/mobile phone number/birthday/password

DROP PROCEDURE IF EXISTS batchGenerateUsers;


DELIMITER $$
CREATE PROCEDURE batchGenerateUsers()
BEGIN
DECLARE x INT Default 0;
    DECLARE p char(11);
WHILE x < 10000 DO
SET x=x+1;
        SET p=concat('1', cast(3+(rand()*10)%7 as char(1)), right(left(trim(cast(rand() as char (20))), 11),9));
insert into my_users(mobile, passwd, name, sex, birthday, updated_time)
values(p,
md5(ceiling(rand()*1000000)),
: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
ceiling(rand()*10)%2,
date(now()-interval (20+ceiling(rand()*100)%40) year),
concat('2015-', 1+ceiling(rand()*100)%12,'-',1+ceiling(rand()*100)%28))
ON DUPLICATE KEY UPDATE
updated_time=now();
END WHILE;
END $$


#call batchGenerateUsers();

and modify it appropriately.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • How to add a column to a large MySQL table
  • Detailed explanation of how to gracefully delete a large table in MySQL
  • A brief discussion on MySQL large table optimization solution
  • High-efficiency query method for repeated fields in large MySQL tables
  • Solution to MySQL performance problem of deleting large tables
  • How to implement batch deletion of large amounts of data in MySQL large tables

<<:  How to remove carriage return characters from text in Linux

>>:  Summary of the advantages of Vue3 vs. Vue2

Recommend

Share the problem of Ubuntu 19 not being able to install docker source

According to major websites and personal habits, ...

The One-Hand Rule of WEB2.0

<br />My previous article about CSS was not ...

Summary of methods for querying MySQL user permissions

Introduce two methods to view MySQL user permissi...

An analysis of div+float, a very important concept in website design

In website construction, you will always encounter...

Goodbye Docker: How to Transform to Containerd in 5 Minutes

Docker is a very popular container technology. Th...

Detailed explanation of 7 SSH command usages in Linux that you don’t know

A system administrator may manage multiple server...

Automatic line breaks in html pre tags

At this time, you can use overflow:auto; (when the...

Super detailed MySQL usage specification sharing

Recently, there have been many database-related o...

Vue implements accordion effect

This article example shares the specific code of ...

Add ?v= version number after js or css to prevent browser caching

Copy code The code is as follows: <span style=...

CSS scroll-snap scroll event stop and element position detection implementation

1. Scroll Snap is a must-have skill for front-end...

Vue implements a scroll bar style

At first, I wanted to modify the browser scroll b...