zoukankan      html  css  js  c++  java
  • 【MySQL】全库调整表大小写语句

    统一修改字段成小写+下划线的命名规则:

    SQL实例:

    SELECT
    concat( 'alter table ', TABLE_NAME, ' change column ', COLUMN_NAME, ' ', LOWER( COLUMN_NAME ), ' ', COLUMN_TYPE, ' ', if(extra = 'auto_increment', extra, ''), if(COLLATION_NAME = 'utf8mb4_0900_ai_ci', ' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci', ''), ' comment \'', COLUMN_COMMENT, '\';' ) AS '修改脚本'
    FROM
    information_schema.COLUMNS
    WHERE
    TABLE_SCHEMA = 'dcs_coordinate_sit';

    更改库名即可:

    查询结果是一条条的改表语句,把SQL复制出来执行

     

    保证单字段主键的自增不会被覆盖掉

    if(extra = 'auto_increment',extra,''),

    统一字段字符集标准:

    if(COLLATION_NAME = 'utf8mb4_0900_ai_ci', ' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci', ''),

     

    使用风险:

    1、主键失去自增设置

    2、失去非空约束

    V1上线后,重新看SQL调整的较可行的写法:

    # = = = = = = = = = = = = = = = 统一更改全库所有字段大小写脚本SQL(会删除字段原来的字符集和排序规则) = = = = = = = = = = = = = = = = = = = = 
    SELECT
        CONCAT(
            'ALTER TABLE `', `TABLE_SCHEMA`, '`.`', `TABLE_NAME`, '` CHANGE COLUMN `', 
            `COLUMN_NAME`, '` `', LOWER(`COLUMN_NAME`) , '` ',   -- 调整小写或者大写 UPPER(`COLUMN_NAME`)
            `COLUMN_TYPE`, 
            IF(`IS_NULLABLE` = 'NO', ' NOT NULL', ' NULL'),  -- 保持原字段 是否非空设定
            IF(`EXTRA` IS NOT NULL, ' AUTO_INCREMENT', ''),  -- 保持主键自带自增属性
            IF(`COLUMN_DEFAULT` IS NULL, '',  CONCAT('DEFAULT \'', `COLUMN_DEFAULT`, '\'')),  -- 保持字段默认值属性
            ' COMMENT \'',  `COLUMN_COMMENT`, '\';' -- 保持字段注释信息,如果无则是空字符串
        ) AS '统一更改字段大小写脚本SQL'
    FROM 
        `information_schema`.`COLUMNS`
    WHERE 
        `TABLE_SCHEMA` NOT IN('mysql', 'information_schema', 'performance_schema', 'sys') -- 非MySQL库

    如果要对字段统一设置字符集&排序规则:

    # = = = = = = = = = = = = = = = 统一更改字段字符集脚本SQL(utf8mb4_general_ci) = = = = = = = = = = = = = = = = = = = = 
    SELECT
        CONCAT(
            'ALTER TABLE `', `TABLE_SCHEMA`, '`.`', `TABLE_NAME`, 
            '` MODIFY COLUMN `', `COLUMN_NAME`, '` ', `COLUMN_TYPE`, 
            ' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ',  -- 设定字符集
            IF(`IS_NULLABLE` = 'NO', ' NOT NULL', ' NULL'), 
            IF(`COLUMN_DEFAULT` IS NULL, '', CONCAT(' DEFAULT \'', `COLUMN_DEFAULT`, '\'')), 
            ' COMMENT \'', `COLUMN_COMMENT`,'\';' -- 保持注释
        ) AS '统一更改字段字符集脚本SQL'
    FROM 
        `information_schema`.`COLUMNS`
    WHERE 
        `TABLE_SCHEMA` NOT IN('mysql', 'information_schema', 'performance_schema', 'sys') -- 非MySQL库
        AND `DATA_TYPE` IN ('varchar', 'char', 'tinytext', 'text', 'mediumtext', 'longtext') -- 指定文本类型才具有字符集和排序规则属性设定
        AND `COLUMN_KEY` != 'PRI'; -- 不干涉主键
  • 相关阅读:
    Linux
    Monkey命令
    ADB命令
    DOS命令
    列表
    函数
    文件读写及实操项目
    Selenium2自动化——初体验
    新开淘宝店怎么提升信誉,是不是真的要刷钻?
    JSFL:导入根文件夹将所有图片添加库链接导出swf
  • 原文地址:https://www.cnblogs.com/mindzone/p/15627855.html
Copyright © 2011-2022 走看看