******************************************************MySQL 的第三个坑***************************************************************
默认模式,是非严格模式,插入的字段比定义的字段长会自动截取,也不报错,产生数据混乱。
root@test 09:13:09>select @@sql_mode;
+------------------------+
| @@sql_mode |
+------------------------+
| NO_ENGINE_SUBSTITUTION |
+------------------------+
1 row in set (0.00 sec)
root@test 09:41:54>desc test;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
root@test 09:46:28>insert into test values(12, '11111111111111111111111');
Query OK, 1 row affected, 1 warning (0.02 sec)
root@test 09:47:19>select * from test;
+------+------------+
| id | name |
+------+------------+
| 1 | aa11 |
| 2 | 1234 |
| 3 | Ab |
| 3 | ab |
| 12 | 1111111111 |
+------+------------+
2. 所以需要设置为traditional模式,意味着是传统的数据库模式,它的反应也是跟oracle和sql server 相同的处理方式。
root@test 09:46:53>set sql_mode='traditional,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)
--字段超长会报错
root@test 09:47:15>insert into test values(12, '11111111111111111111111');
ERROR 1406 (22001): Data too long for column 'name' at row 1
所以我们的应用由传统的数据库oracle转向mysql时, 最好配置 sql_mode='traditional,NO_ENGINE_SUBSTITUTION', 这样不会出现一些奇怪陌生的行为。
默认模式,是非严格模式,插入的字段比定义的字段长会自动截取,也不报错,产生数据混乱。
root@test 09:13:09>select @@sql_mode;
+------------------------+
| @@sql_mode |
+------------------------+
| NO_ENGINE_SUBSTITUTION |
+------------------------+
1 row in set (0.00 sec)
root@test 09:41:54>desc test;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
root@test 09:46:28>insert into test values(12, '11111111111111111111111');
Query OK, 1 row affected, 1 warning (0.02 sec)
root@test 09:47:19>select * from test;
+------+------------+
| id | name |
+------+------------+
| 1 | aa11 |
| 2 | 1234 |
| 3 | Ab |
| 3 | ab |
| 12 | 1111111111 |
+------+------------+
2. 所以需要设置为traditional模式,意味着是传统的数据库模式,它的反应也是跟oracle和sql server 相同的处理方式。
root@test 09:46:53>set sql_mode='traditional,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)
--字段超长会报错
root@test 09:47:15>insert into test values(12, '11111111111111111111111');
ERROR 1406 (22001): Data too long for column 'name' at row 1
所以我们的应用由传统的数据库oracle转向mysql时, 最好配置 sql_mode='traditional,NO_ENGINE_SUBSTITUTION', 这样不会出现一些奇怪陌生的行为。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/674865/viewspace-2123709/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/674865/viewspace-2123709/