iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >MySQL 用随机数据填充外键表
  • 407
分享到

MySQL 用随机数据填充外键表

2024-04-02 19:04:59 407人浏览 安东尼
摘要

参考: Http://blog.itpub.net/29254281/viewspace-1686302/ 准备环境 1.创建数字辅助表 create table nums(id int not null

参考:
Http://blog.itpub.net/29254281/viewspace-1686302/

准备环境
1.创建数字辅助表
create table nums(id int not null primary key);

delimiter $$
create procedure pFastCreateNums(cnt int)
begin
    declare s int default 1;
    truncate table nums;
    insert into nums select s;
    while s*2<=cnt do
        insert into nums select id+s from nums;
        set s=s*2;
    end while;
end $$
delimiter ;


call pFastCreateNums(1000000);

数字辅助表的行数决定最后能生成的表行数的最大值.

2.创建生成随机字符的函数

  1. DROP FUNCTioN IF EXISTS rand_string;
  2. delimiter //
  3. CREATE FUNCTION rand_string(l_num int UNSIGNED,l_type tinyint UNSIGNED)
  4. RETURNS varchar(2000)
  5. BEGIN
  6.  -- Function : rand_string
  7.  -- Author : dbachina#dbachina.com
  8.  -- Date : 2010/5/30
  9.  -- l_num : The length of random string
  10.  -- l_type: The string type
  11.  -- 1.0-9
  12.  -- 2.a-z
  13.  -- 3.A-Z
  14.  -- 4.a-zA-Z
  15.  -- 5.0-9a-zA-Z
  16.  -- :
  17.   -- Mysql> select rand_string(12,5) random_string;
  18.   -- +---------------+
  19.   -- | random_string |
  20.   -- +---------------+
  21.   -- | 3KzGJCUJUplw |
  22.   -- +---------------+
  23.   -- 1 row in set (0.00 sec)
  24.  DECLARE i int UNSIGNED DEFAULT 0;
  25.  DECLARE v_chars varchar(64) DEFAULT '0123456789';
  26.   DECLARE result varchar (2000) DEFAULT '';
  27.  
  28.   IF l_type = 1 THEN
  29.     SET v_chars = '0123456789';
  30.   ELSEIF l_type = 2 THEN
  31.     SET v_chars = 'abcdefghijklmnopqrstuvwxyz';
  32.   ELSEIF l_type = 3 THEN
  33.     SET v_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  34.   ELSEIF l_type = 4 THEN
  35.     SET v_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  36.   ELSEIF l_type = 5 THEN
  37.     SET v_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  38.   ELSE
  39.     SET v_chars = '0123456789';
  40.   END IF;
  41.  
  42.   WHILE i < l_num DO
  43.       SET result = concat( result,substr(v_chars,ceil(rand()*(length(v_chars)-1)),1) );
  44.     SET i = i + 1;
  45.   END WHILE;
  46.   RETURN result;
  47. END;
  48. //
  49. delimiter ;
您可能感兴趣的文档:

--结束END--

本文标题: MySQL 用随机数据填充外键表

本文链接: https://www.lsjlt.com/news/49541.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作