共計(jì) 6942 個(gè)字符,預(yù)計(jì)需要花費(fèi) 18 分鐘才能閱讀完成。
自動(dòng)寫(xiě)代碼機(jī)器人,免費(fèi)開(kāi)通
這篇文章給大家介紹如何在 mysql 中實(shí)現(xiàn)一個(gè) sql_mode 模式,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
mysql 數(shù)據(jù)庫(kù)的中有一個(gè)環(huán)境變量 sql_mode, 定義了 mysql 應(yīng)該支持的 sql 語(yǔ)法,數(shù)據(jù)校驗(yàn)等!我們可以通過(guò)以下方式查看當(dāng)前數(shù)據(jù)庫(kù)使用的 sql_mode:
mysql select @@sql_mode;
+----------------------------------------------------------------+
| @@sql_mode |
+----------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+----------------------------------------------------------------+
mysql5.0 以上版本支持三種 sql_mode 模式
ANSI 模式
寬松模式,對(duì)插入數(shù)據(jù)進(jìn)行校驗(yàn),如果不符合定義類型或長(zhǎng)度,對(duì)數(shù)據(jù)類型調(diào)整或截?cái)啾4妫瑘?bào) warning 警告。
TRADITIONAL 模式
嚴(yán)格模式,當(dāng)向 mysql 數(shù)據(jù)庫(kù)插入數(shù)據(jù)時(shí),進(jìn)行數(shù)據(jù)的嚴(yán)格校驗(yàn),保證錯(cuò)誤數(shù)據(jù)不能插入,報(bào) error 錯(cuò)誤。用于事物時(shí),會(huì)進(jìn)行事物的回滾。
STRICT_TRANS_TABLES 模式
嚴(yán)格模式,進(jìn)行數(shù)據(jù)的嚴(yán)格校驗(yàn),錯(cuò)誤數(shù)據(jù)不能插入,報(bào) error 錯(cuò)誤。
1 ANSI 模式
在 ANSI 模式下,當(dāng)我們插入數(shù)據(jù)時(shí),未滿足列長(zhǎng)度要求時(shí),數(shù)據(jù)同樣會(huì)插入成功,但是對(duì)超出列長(zhǎng)度的字段進(jìn)行截?cái)啵瑫r(shí)報(bào)告 warning 警告。
mysql set @@sql_mode=ANSI;
Query OK, 0 rows affected (0.00 sec)
mysql create table test(name varchar(4), pass varchar(4));
Query OK, 0 rows affected (0.03 sec)
mysql insert into test values(aaaaa , aaaaa),( bbbb , bbbb
Query OK, 2 rows affected, 2 warnings (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 2
mysql show warnings;
+---------+------+-------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------+
| Warning | 1265 | Data truncated for column name at row 1 |
| Warning | 1265 | Data truncated for column pass at row 1 |
+---------+------+-------------------------------------------+
2 rows in set (0.00 sec)
mysql select * from test;
+------+------+
| name | pass |
+------+------+
| aaaa | aaaa |
| bbbb | bbbb |
+------+------+
2 rows in set (0.00 sec)
2 STRICT_TRANS_TABLES 模式
在 STRICT_TRANS_TABLES 模式下,當(dāng)我們插入數(shù)據(jù)時(shí),mysql 會(huì)嚴(yán)格的進(jìn)行數(shù)據(jù)的校驗(yàn),當(dāng)發(fā)現(xiàn)插入列值未滿足要求,直接報(bào)告 error 錯(cuò)誤,保證了錯(cuò)誤數(shù)據(jù)無(wú)法插入到數(shù)據(jù)庫(kù)中。
mysql set @@sql_mode=STRICT_TRANS_TABLES;
Query OK, 0 rows affected (0.00 sec)
mysql create table test(name varchar(4), pass varchar(4));
Query OK, 0 rows affected (0.02 sec)
mysql insert into test values(aaaaa , aaaaa),( bbbb , bbbb
ERROR 1406 (22001): Data too long for column name at row 1
mysql show errors;
+-------+------+------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------+
| Error | 1406 | Data too long for column name at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)
mysql select * from test;
Empty set (0.00 sec)
3 TRADITIONAL 模式,初看結(jié)果是不是一樣
mysql set @@sql_mode=TRADITIONAL;
Query OK, 0 rows affected (0.00 sec)
mysql create table test(name varchar(4), pass varchar(4));
Query OK, 0 rows affected (0.02 sec)
mysql insert into test values(aaaaa , aaaaa),( bbbb , bbbb
ERROR 1406 (22001): Data too long for column name at row 1
mysql show errors;
+-------+------+------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------+
| Error | 1406 | Data too long for column name at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)
mysql select * from test;
Empty set (0.00 sec)
但是,可以看看設(shè)置后的情況
mysql set @@sql_mode=TRADITIONAL;
Query OK, 0 rows affected (0.00 sec)
mysql select @@sql_mode\G
*************************** 1. row ***************************
@@sql_mode: STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
1 row in set (0.00 sec)
在 TRADITIONAL 模式下,對(duì)所有的事務(wù)存儲(chǔ)引擎,非事務(wù)存儲(chǔ)引擎檢查,日期類型中的月和日部分不能包含 0,不能有 0 這樣的日期(0000-00-00), 數(shù)據(jù)不能除 0,禁止 grant 自動(dòng)創(chuàng)建新用戶等一些校驗(yàn)。
最后:
set @@只是在 sessions 級(jí)別設(shè)置的,要想所有的都生效,還是要設(shè)置配置文件
vi /etc/my.cnf
在 [mysqld] 下面添加如下列:
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#NO_ENGINE_SUBSTITUTION 對(duì)于不存在的引擎就報(bào)錯(cuò),不加的話,指定不支持的引擎時(shí)指定默認(rèn)的 innodb
另外:sql_mode 還有一個(gè)配置 ONLY_FULL_GROUP_BY,這個(gè)表示采用 group by 帥選數(shù)據(jù)的時(shí)候只能查看新組內(nèi)信息
改模式之前的操作
mysql select * from employee group by post;
+----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 14 | 張野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 1 | egon | male | 18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使 | NULL | 7300.33 | 401 | 1 |
+----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
4 rows in set (0.00 sec)
此時(shí)的 sql_mode:
mysql select @@sql_mode;
+----------------------------------------------------------------+
| @@sql_mode |
+----------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+----------------------------------------------------------------+
1 row in set (0.00 sec)
修改一下,退出再進(jìn)入才會(huì)生效
mysql set global sql_mode= STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY
Query OK, 0 rows affected (0.00 sec)
mysql select @@sql_mode;
+----------------------------------------------------------------+
| @@sql_mode |
+----------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+----------------------------------------------------------------+
1 row in set (0.00 sec)
mysql exit
Bye
再次進(jìn)入
mysql select @@sql_mode;
+-----------------------------------------------------------------------------------+
| @@sql_mode |
+-----------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
下面查看修改后的查看結(jié)果
mysql select * from employee group by post; // 只能查看 post
ERROR 1055 (42000): t1.employee.id isn t in GROUP BY
mysql select post from employee group by post;
+-----------------------------------------+
| post |
+-----------------------------------------+
| operation |
| sale |
| teacher |
| 老男孩駐沙河辦事處外交大使 |
+-----------------------------------------+
4 rows in set (0.00 sec)
mysql select id,post from employee group by post;
ERROR 1055 (42000): t1.employee.id isn t in GROUP BY
mysql select name,post from employee group by post,name; // 根據(jù) group by 后面的選擇查看
+------------+-----------------------------------------+
| name | post |
+------------+-----------------------------------------+
| 張野 | operation |
| 程咬金 | operation |
| 程咬鐵 | operation |
| 程咬銅 | operation |
| 程咬銀 | operation |
| 丁丁 | sale |
| 丫丫 | sale |
| 星星 | sale |
| 格格 | sale |
| 歪歪 | sale |
| alex | teacher |
| jingliyang | teacher |
| jinxin | teacher |
| liwenzhou | teacher |
| wupeiqi | teacher |
| xiaomage | teacher |
| yuanhao | teacher |
| egon | 老男孩駐沙河辦事處外交大使 |
+------------+-----------------------------------------+
18 rows in set (0.00 sec)
關(guān)于如何在 mysql 中實(shí)現(xiàn)一個(gè) sql_mode 模式就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
向 AI 問(wèn)一下細(xì)節(jié)