http://click.aliyun.com/m/39994/

本文介绍了一种在MySQL中从大小写敏感模式迁移到大小写不敏感模式的方法。通过一个存储过程实现表名和数据库名的批量重命名,确保迁移过程中数据的完整性和一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用过MySQL的应该都会注意到,默认情况下,Linux下安装的MySQL是大小写敏感的,也就是说Table1和table1可以同时存在。而Windows下的MySQL却是大小写不敏感的,所有表名和数据库名都会变成小写。

用过MySQL的应该都会注意到,默认情况下,Linux下安装的MySQL是大小写敏感的,也就是说Table1和table1可以同时存在。而Windows下的MySQL却是大小写不敏感的,所有表名和数据库名都会变成小写。

对于怎么启用或者停用MySQL数据库的大小写敏感,这个网上随便都能找到,就是改改参数lower_case_table_names,然后重启即可。

但是,如果我们的数据库中已经有了多个区分大小写的数据库,现在要改为不区分大小写的,那么就会报错:Table 'databasenamexxx.tablenamexxx' doesn't exist.

为此,我们需要将MySQL改为大小写敏感的模式,然后去重命名每个表名和数据库名。

MySQL确实很神奇的一点是不允许重命名数据库,所以如果我们要重命名Test1为test1,那么只有新建一个test1的数据库,然后把Test1中的表全面rename到test1数据库中。

而且在rename的过程中,我们也需要将表面从大小写的形式改为全部小写的形式。

为了批量的做这么一件事,与,我写了一个存储过程,通过读取系统表,获得数据库表名,然后用游标的方式依次执行rename操作。

DELIMITER //

CREATE PROCEDURE renametables(olddb VARCHAR(50),newdb VARCHAR(50)) 
BEGIN 
DECLARE done BOOLEAN DEFAULT 0; 
DECLARE tmp VARCHAR(100); -- 定义局部变量

DECLARE tbcur CURSOR 
    FOR    SELECT TABLE_NAME FROM `information_schema`.`TABLES` WHERE table_schema=olddb AND Table_Type='BASE TABLE'; 
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1; 
    OPEN tbcur;  -- 打开游标

    REPEAT 
     FETCH tbcur INTO tmp; 
     IF done=0 
     THEN 
      SET @sqlstring=CONCAT( 'RENAME TABLE ',olddb,'.`',tmp,'` TO ',newdb,'.`',LOWER(tmp),'`;'); 
      SELECT @sqlstring; -- 这一句可以不要,只是打印我们拼接后要运行的SQL是什么
      PREPARE s1 FROM  @sqlstring; -- 执行拼接出来的SQL 
      EXECUTE s1;  
      DEALLOCATE PREPARE s1;    
    END IF; 
     UNTIL done END REPEAT; 
    CLOSE tbcur; -- 关闭游标,释放游标使用的所有内部内存和资源 
    
    END// 复制代码


我们在新数据库中建立了该存储过程,然后调用即可:

CALL renametables('Test1','test1')复制代码

这样所有Test1中的大小写混合的表,就全部转换到了test1数据库中,而且表名都变成了小写了。

一个一个的数据库去这么做,然后再把MySQL的参数改为大小写不敏感,这样才能正常使用。

这里我只是做了表的迁移,接下来存储过程和视图的迁移,由于不涉及到数据,所以比较简单,找到当年的DDL或者我们在大小写敏感的时候就导出View和存储过程的定义,然后用文本编辑器把整个SQL变成小写的,然后到新数据库中去执行,重新创建即可。


原文链接


转载于:https://juejin.im/post/5a586c83f265da3e33044512

------------------------------------------------------------ E:\soft\1\Anaconda\Scripts\pip-script.py run on 03/30/25 17:00:01 Downloading/unpacking click Getting page https://mirrors.aliyun.com/pypi/simple/click URLs to search for versions for click: * https://mirrors.aliyun.com/pypi/simple/click/ Analyzing links from page https://mirrors.aliyun.com/pypi/simple/click/ Skipping link https://mirrors.aliyun.com/pypi/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl#sha256=2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13 (from https://mirrors.aliyun.com/pypi/simple/click/); unknown archive format: .whl Found link https://mirrors.aliyun.com/pypi/packages/f8/5c/f60e9d8a1e77005f664b76ff8aeaee5bc05d0a91798afd7f53fc998dbc47/Click-7.0.tar.gz#sha256=5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7 (from https://mirrors.aliyun.com/pypi/simple/click/), version: 7.0 Skipping link https://mirrors.aliyun.com/pypi/packages/82/e9/39bc04e46ac4dc16f60d3c95d2a8238f8a86a738ecab723755470e1486d1/click-0.1-py2.py3-none-any.whl#sha256=6ece7fdc438597979abb5e237cd42ec9b0ed9342acfa13aabd0d57dae5122f00 (from https://mirrors.aliyun.com/pypi/simple/click/); unknown archive format: .whl Found link https://mirrors.aliyun.com/pypi/packages/1a/65/bde2803d3f5d217fde361f7773d51d5c9b1332181f740bdd7adb2462607c/click-0.1.tar.gz#sha256=9f8290d502cf11fad5ccc64d19f2724abcbc11549e6a8e2cdafc530109f198b4 (from https://mirrors.aliyun.com/pypi/simple/click/), version: 0.1 Skipping link https://mirrors.aliyun.com/pypi/packages/ee/a5/97f43386352a0658f12842848c152479fce3162251c08339866da45e912e/click-0.2-py2.py3-none-any.whl#sha256=54c90326cb37daf23389b909fa593660db74d68861cbc36d871d8c7ccc2fe003 (from https://mirrors.aliyun.com/pypi/simple/click/); unknown archive format: .whl Found link https://mirrors.aliyun.com/pypi/packages/bb/aa/c8b583d8d7cc5e21c8da30de6d8c605652836b4ef33b2b57c37f6a017c09/cl
03-31
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/ Collecting certifi==2019.3.9 (from -r requirements.txt (line 1)) Using cached https://mirrors.aliyun.com/pypi/packages/60/75/f692a584e85b7eaba0e03827b3d51f45f571c2e793dd731e598828d380aa/certifi-2019.3.9-py2.py3-none-any.whl (158 kB) Collecting chardet==3.0.4 (from -r requirements.txt (line 2)) Using cached https://mirrors.aliyun.com/pypi/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133 kB) Collecting click==6.7 (from -r requirements.txt (line 3)) Using cached https://mirrors.aliyun.com/pypi/packages/34/c1/8806f99713ddb993c5366c362b2f908f18269f8d792aff1abfd700775a77/click-6.7-py2.py3-none-any.whl (71 kB) Collecting Flask==1.0.2 (from -r requirements.txt (line 4)) Using cached https://mirrors.aliyun.com/pypi/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91 kB) Collecting Flask-WTF==0.14.2 (from -r requirements.txt (line 5)) Using cached https://mirrors.aliyun.com/pypi/packages/60/3a/58c629472d10539ae5167dc7c1fecfa95dd7d0b7864623931e3776438a24/Flask_WTF-0.14.2-py2.py3-none-any.whl (14 kB) Collecting idna==2.8 (from -r requirements.txt (line 6)) Using cached https://mirrors.aliyun.com/pypi/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl (58 kB) Collecting itsdangerous==0.24 (from -r requirements.txt (line 7)) Using cached https://mirrors.aliyun.com/pypi/packages/dc/b4/a60bcdba945c00f6d608d8975131ab3f25b22f2bcfe1dab221165194b2d4/itsdangerous-0.24.tar.gz (46 kB) Preparing metadata (setup.py) ... done Collecting Jinja2==2.10 (from -r requirements.txt (line 8)) Using cached https://mirrors.aliyun.com/pypi/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126 kB) Requirement already satisfied: setuptools in c:\programdata\anaconda3\envs\3112\lib\site-packages (from -r requirements.txt (line 9)) (78.1.1) Collecting MarkupSafe (from -r requirements.txt (line 10)) Using cached https://mirrors.aliyun.com/pypi/packages/da/b8/3a3bd761922d416f3dc5d00bfbed11f66b1ab89a0c2b6e887240a30b0f6b/MarkupSafe-3.0.2-cp311-cp311-win_amd64.whl (15 kB) Collecting numpy==1.15.4 (from -r requirements.txt (line 11)) Using cached https://mirrors.aliyun.com/pypi/packages/2d/80/1809de155bad674b494248bcfca0e49eb4c5d8bee58f26fe7a0dd45029e2/numpy-1.15.4.zip (4.5 MB) Preparing metadata (setup.py) ... done Collecting pandas==0.23.4 (from -r requirements.txt (line 12)) Using cached https://mirrors.aliyun.com/pypi/packages/e9/ad/5e92ba493eff96055a23b0a1323a9a803af71ec859ae3243ced86fcbd0a4/pandas-0.23.4.tar.gz (10.5 MB) Preparing metadata (setup.py) ... error error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [15 lines of output] C:\Users\Administrator\AppData\Local\Temp\pip-install-mwx2ea8w\pandas_33f6702689674a99929697720e4cfb12\setup.py:12: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html import pkg_resources C:\ProgramData\anaconda3\envs\3112\Lib\site-packages\setuptools\__init__.py:94: _DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated. !! ******************************************************************************** Requirements should be satisfied by a PEP 517 installer. If you are using pip, you can try `pip install --use-pep517`. ******************************************************************************** !! dist.fetch_build_eggs(dist.setup_requires) error in pandas setup command: 'install_requires' must be a string or iterable of strings containing valid project/version requirement specifiers; Expected end or semicolon (after version specifier) pytz >= 2011k ~~~~~~~^ [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
最新发布
08-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值