今天遇到了mysql 提示错误 doesn't have a default value.
点击(此处)折叠或打开
<h1>CDbException</h1>
<p>CDbCommand 无法执行 SQL 语句:SQLSTATE[HY000]:Generalerror:1364 Field'sms_percent_amount'doesn't have a default value.The SQL statement executed was:INSERT INTO t_dev(`mail`,`passwd`,`name`,`reg_ip`,`git_id`,`status`)VALUES(:mail,:passwd,:name,:reg_ip,:git_id,:status);.Bound with mail='safFD@jj.dk',passwd='e99a18c428cb38d5f260853678922e03',name='magic007',status=1,reg_ip='2130706433',git_id='2484137'(/Users/magic/Applications/web/cctv/yii/framework/db/CDbCommand.php:358)</p><pre>#0/Users/magic/Applications/web/cctv/cctv-web/www.cctv.cn/protected/models/User.php(329):CDbCommand->execute(Array)
#1/Users/magic/Applications/web/cctv/cctv-web/www.cctv.cn/protected/models/User.php(37):User->addRow(Array)
#2/Users/magic/Applications/web/cctv/cctv-web/www.cctv.cn/protected/controllers/SiteController.php(398):User->register(Array)
#3/Users/magic/Applications/web/cctv/yii/framework/web/actions/CInlineAction.php(49):SiteController->actionAuthRegister()
#4/Users/magic/Applications/web/cctv/yii/framework/web/CController.php(308):CInlineAction->runWithParams(Array)
#5/Users/magic/Applications/web/cctv/yii/framework/web/filters/CFilterChain.php(133):CController->runAction(Object(CInlineAction))
#6/Users/magic/Applications/web/cctv/yii/framework/web/filters/CFilter.php(40):CFilterChain->run()
#7/Users/magic/Applications/web/cctv/yii/framework/web/CController.php(1145):CFilter->filter(Object(CFilterChain))
#8/Users/magic/Applications/web/cctv/yii/framework/web/filters/CInlineFilter.php(58):CController->filterAccessControl(Object(CFilterChain))
#9/Users/magic/Applications/web/cctv/yii/framework/web/filters/CFilterChain.php(130):CInlineFilter->filter(Object(CFilterChain))
#10/Users/magic/Applications/web/cctv/yii/framework/web/CController.php(291):CFilterChain->run()
#11/Users/magic/Applications/web/cctv/yii/framework/web/CController.php(265):CController->runActionWithFilters(Object(CInlineAction),Array)
#12/Users/magic/Applications/web/cctv/yii/framework/web/CWebApplication.php(282):CController->run('AuthRegister')
#13/Users/magic/Applications/web/cctv/yii/framework/web/CWebApplication.php(141):CWebApplication->runController('site/AuthRegist...')
#14/Users/magic/Applications/web/cctv/yii/framework/base/CApplication.php(185):CWebApplication->processRequest()
#15/Users/magic/Applications/web/cctv/cctv-web/www.cctv.cn/index.php(24):CApplication->run()
#16{main}</pre>
查看目前mysql使用模式
mysql> select @@sql_mode;
| @@sql_mode |
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
修改MySQL 模式
命令行修改 SET sql_mode = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
如果需要重启后修改,请修改my.conf
sql_mode= NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
关于sql_mode 介绍
1. sql_mode模式
mysql数据库的中有一个环境变量sql_mode,定义了mysql应该支持的sql语法,数据校验等!我们可以通过以下方式查看当前数据库使用的sql_mode:
[java] [view
plain](http://blog.csdn.net/wulantian/article/details/8905573# "view plain")
copy
- [mysql]()> select @@sql_mode;
- +----------------------------------------------------------------+
- | @@sql_mode |
- +----------------------------------------------------------------+
- | STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
- +----------------------------------------------------------------+
或者通过查看系统变量方式:
[java] [view
plain](http://blog.csdn.net/wulantian/article/details/8905573# "view plain")
copy
- mysql> show variables like 'sql_mode%'\G;
- 1. row **
- Variable_name: sql_mode
- Value: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
mysql5.0以上版本支持三种sql_mode模式:ANSI、TRADITIONAL和STRICT_TRANS_TABLES。
ANSI模式:宽松模式,对插入数据进行校验,如果不符合定义类型或长度,对数据类型调整或截断保存,报warning警告。
TRADITIONAL模式:严格模式,当向mysql数据库插入数据时,进行数据的严格校验,保证错误数据不能插入,报error错误。用于事物时,会进行事物的回滚。
STRICT_TRANS_TABLES模式:严格模式,进行数据的严格校验,错误数据不能插入,报error错误。
1.2 ANSI模式
[java] [view
plain](http://blog.csdn.net/wulantian/article/details/8905573# "view plain")
copy
- 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)
我们可以看到,在ANSI模式下,当我们插入数据时,未满足列长度要求时,数据同样会插入成功,但是对超出列长度的字段进行截断,同时报告warning警告。
1.3 STRICT_TRANS_TABLES模式
[java] [view
plain](http://blog.csdn.net/wulantian/article/details/8905573# "view plain")
copy
- 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)
我们可以看到,在STRICT_TRANS_TABLES模式下,当我们插入数据时,mysql会严格的进行数据的校验,当发现插入列值未满足要求,直接报告error错误,保证了错误数据无法插入到数据库中。
1.3 TRADITIONAL模式
[java] [view
plain](http://blog.csdn.net/wulantian/article/details/8905573# "view plain")
copy
- 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)
TRADITIONAL模式与STRICT_TRANS_TABLES模式执行的结果,在这种情况下一致。
[java] [view
plain](http://blog.csdn.net/wulantian/article/details/8905573# "view plain")
copy
- mysql> select @@sql_mode\G;
- 1. row **
- @@sql_mode: STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,E
- RROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER
- 1 row in set (0.00 sec)
看一下TRADITIONAL模式,我们发现在TRADITIONAL模式下,对所有的事务存储引擎,非事务存储引擎检查,日期类型中的月和日部分不能包含0,不能有0这样的日期(0000-00-00),数据不能除0,禁止grant自动创建新用户等一些校验。
注意:我们这里设置的sql_mode都是session级别的。另外,可以直接修改my.ini文件,找到sql_mode,然后设置新的模式即可!
例如:
vi /etc/my.cnf
在[mysqld]下面添加如下列:
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
评论 (0)