Mysql 存儲過程批量建表

CREATE DEFINER=`root`@`%` PROCEDURE `createTables`()
  begin
    declare i int;
    declare suffix varchar(20);
    declare createsql varchar(2048);
    set i = 0;
    while i < 100 do
      set suffix = hex(i);
      if length(suffix)<=1 then
      set suffix = concat('0',hex(i));
      end if;
      set suffix = lower(suffix);
      set createsql = concat('CREATE TABLE table_',suffix,'(
                              `id` int(11) NOT NULL AUTO_INCREMENT COMMENT \'自增主鍵\',
                              `uuid` varchar(32)  NOT NULL COMMENT \'業務\',
                              PRIMARY KEY (`id`)
                              ) ENGINE=InnoDB DEFAULT CHARSET=utf8');
      set @createsql = createsql;
      prepare stmt from @createsql;
      execute stmt;
      set i = i + 1;
    end while;
endmysql

裏面用到了幾個mysql自帶的函數:sql

1. hex(number): 十進制轉成16進制函數

2. length(string) : 字符串長度ui

3. concat(str1,str2) : 字符串拼接spa

4. lower(str) : 字符串轉小寫字符串

相關文章
相關標籤/搜索