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

Native Js implementation of calendar widget

This article example shares the specific code of ...

Detailed explanation of unique constraints and NULL in MySQL

Preface A requirement I had previously made, to s...

Use of MySQL DATE_FORMAT function

Suppose Taobao encourages people to shop during D...

Deploy Confluence with Docker

1. Environmental requirements 1. Docker 17 and ab...

Solution to the bug that IE6 select cannot be covered by div

Use div to create a mask or simulate a pop-up wind...

JavaScript Basics Variables

Table of contents 1. Variable Overview 1.1 Storag...

Win10 configuration tomcat environment variables tutorial diagram

Before configuration, we need to do the following...

PostgreSQL materialized view process analysis

This article mainly introduces the process analys...

How to create, save, and load Docker images

There are three ways to create an image: creating...

Web Design Principles of Hyperlinks

<br />Related articles: 9 practical tips for...

Solve the error problem caused by modifying mysql data_dir

Today, I set up a newly purchased Alibaba Cloud E...

IE9beta version browser supports HTML5/CSS3

Some people say that IE9 is Microsoft's secon...