日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql 大小写 if_【已解决】Windows下 MySQL大小写敏感 解决方案及分析

發布時間:2023/12/4 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 大小写 if_【已解决】Windows下 MySQL大小写敏感 解决方案及分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Windows下 MySQL大小寫敏感配置

zoerywzhou@163.com

作者:Zhouwan

2017-3-27

最近在window系統下 操作Linux系統下創建的數據庫,發現有些不對勁,比較了半天才發現是大小寫敏感的問題造成的。網上搜索了一下,解決了這個問題,做個簡明扼要的記錄。

按照網上的說明:

WINDOWS:

編輯MySQL安裝目錄下的my.ini 文件,在[mysqld]節下 添加 lower_case_table_names=0 (備注:為0時大小寫敏感,為1時大小寫不敏感,默認為1),可以實現MySql按照建表Sql語句的大小寫狀態來定義表名。

具體操作的時候會發現,理論和實踐總是有差異的:

我的MySQL安裝路徑為:C:\Program Files\MySQL\MySQL Server 5.7\bin(網上有的說my.ini配置文件在這個文件夾下)

但是my.ini所在的文件路徑為:C:\ProgramData\MySQL\MySQL Server 5.7\my.ini

然后在[mysqld]節下 添加 lower_case_table_names=0。

配置好文件了,去試一下咋樣。

然后,還沒重啟系統進行測試,在另一個博文里看到了官方手冊的網址鏈接,按捺不住好奇心去看一下。發現上面的這個配置是不可行的(具體分析見下面的劃重點)

可能出現的錯誤可參見這篇求助博文的內容以及下面的討論:http://bbs.csdn.net/topics/391860287

(在官網說明的基礎上加上我的獨家中文解說 O(∩_∩)O~英文好的同學最好直接看英文文檔,說的比較清楚)

How table and database names are stored on disk and used in MySQL is affected by the?lower_case_table_names?system variable, which you can set when starting?mysqld.?lower_case_table_names?can take the values shown in the following table. This variable does?not?affect case sensitivity of trigger identifiers. OnUnix, the default value of?lower_case_table_names?is 0. On Windows, the default value is 1. On OS X, the default value is2.

PS:mac OS(2012年前稱Mac?OS X,2012年-2016年稱OS X)

ValueMeaning

0

Table and database names are stored on disk using the lettercase specified in the?CREATE TABLE?or?CREATE DATABASE?statement. Name comparisons are case sensitive.

(總結:存儲和查詢的時候都大小寫敏感,都要是按照建表時指定的寫法)

You should?not?set this variable to 0 if you are running MySQL on a system that has case-insensitive file names (such as Windows or OS X). If you force this variable to 0 with?--lower-case-table-names=0?on a case-insensitive file system and access?MyISAM?tablenames using different lettercases, index corruption may result.(此處劃重點)

1

Table names are stored in lowercase on disk and name comparisons are not case sensitive. MySQL converts all table names to lowercase on storage and lookup. This behavior also applies to database names and table aliases.

(總結:存儲和查詢的時候大小寫都不敏感,都轉換為小寫字母)

2

Table and database names are stored on disk using the lettercase specified in the?CREATE TABLE?or?CREATE DATABASE?statement, but MySQL converts them to lowercase on lookup. Name comparisons are not case sensitive. This works?only?on file systems that are not case sensitive!?InnoDB?table names are stored in lowercase, as for?lower_case_table_names=1.

(總結:存儲時大小寫敏感,按照建表時指定的寫法;查詢時都轉換為小寫字母)

1、單平臺:If you are using MySQL on only one platform, you do not normally have to change the?lower_case_table_names?variable from its default value.

2、跨平臺:However, you may encounter difficulties if you want totransfer tables between platforms that differ in file system case sensitivity. For example, on Unix, you can have two different tables named?my_table?and?MY_TABLE, but on Windows these two names are considered identical. To avoid data transfer problems arising from lettercase of database or table names, you have two options:(跨平臺的MySQL大小寫敏感解決方案在此!!!看看官網怎么說~)

Use?lower_case_table_names=1?on all systems. The main disadvantage with this is that when you use?SHOW TABLES?or?SHOW DATABASES, you do not see the names in their original lettercase.

Use?lower_case_table_names=0on Unix and?lower_case_table_names=2on Windows. This preserves the lettercase of database and table names. The disadvantage of this is that you must ensure that your statements always refer to your database and table names with the correct lettercase on Windows. If you transfer your statements to Unix, where lettercase is significant, they do not work if the lettercase is incorrect.

Exception: If you are usingInnoDB?tables and you are trying to avoid these data transfer problems, you should set?lower_case_table_names?to1 on all platforms to force names to be converted to lowercase.

有點復雜。。。本來看懂了的,又出來了個Exception。。。本來愉悅的心情又down了,因為我的table就是InnoDB,看了半天想去配置文件大展身手來著。。。

好吧,那就先不管跨不跨平臺了,也不管配置文件my.ini了。直接上手使用的話,想要區別大小寫,可以在查詢語句的時候加上Binary,具體實現如下:

(此段文字的參考博文:http://www.cnblogs.com/softidea/p/6047766.html。在此博文的基礎上,內容已大幅度精簡提煉)

參考方案:

1、在建表時指定大小寫敏感:

MySql默認查詢是不區分大小寫的,如果需要區分他,必須在建表的時候,Binary標示敏感的屬性.

CREATE TABLE NAME(

name VARCHAR(10) ?BINARY);

2、 在查詢條件的字段名前 加上binary:

在SQL語句中實現 SELECT * FROM TABLE NAME WHEREBINARY?name=‘Clip‘;

3、 設置字符集使其大小寫敏感:

utf8_general_ci?--不區分大小寫

utf8_bin--區分大小寫

【設置collate(校對) 。 collate規則:

*_bin: 表示的是binary case sensitive collation,也就是說是區分大小寫的

*_cs: case sensitive collation,區分大小寫

*_ci: case insensitive collation,不區分大小寫?】

4、可以修改該字段的collation 為 binary

比如:

ALTER TABLE TABLENAME MODIFY COLUMN COLUMNNAME VARCHAR(50) BINARY CHARACTERSET utf8 ?COLLATE utf8_bin DEFAULT NULL;

總結

以上是生活随笔為你收集整理的mysql 大小写 if_【已解决】Windows下 MySQL大小写敏感 解决方案及分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。