Chinaunix首页 | 论坛 | 博客
  • 博客访问: 492103
  • 博文数量: 99
  • 博客积分: 3621
  • 博客等级: 中校
  • 技术积分: 1089
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-22 16:29
文章存档

2012年(21)

2011年(28)

2010年(50)

分类: Mysql/postgreSQL

2012-05-10 10:50:01

在做一个简单的插入测试时出现如下结果:

点击(此处)折叠或打开

  1. mysql> INSERT INTO `Pro****` VALUES(1,0,0,0,0,'en_us','***.satis***','S*** Index',0,'2008-01-28 23:53:08',0,'2011-04-21 22:59:04',0);
  2. ERROR 1048 (23000): Column 'CreatedDate' cannot be null
表结构如下:

点击(此处)折叠或打开

  1. mysql> show create table Pro****;
  2. +------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  3. | Table | Create Table |
  4. +------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  5. | Pro**** | CREATE TABLE `Pro****` (
  6.   `ID` int(11) NOT NULL AUTO_INCREMENT,
  7.   `Industry` tinyint(1) NOT NULL,
  8.   `BusinessId` int(11) NOT NULL,
  9.   `UserId` int(11) NOT NULL,
  10.   `CustomerId` int(11) NOT NULL,
  11.   `Locale` varchar(10) NOT NULL,
  12.   `Property` varchar(254) NOT NULL,
  13.   `Value` text NOT NULL,
  14.   `CreatedUserID` int(11) NOT NULL DEFAULT '0',
  15.   `CreatedDate` datetime NOT NULL,
  16.   `LastModifiedById` int(11) NOT NULL,
  17.   `LastModifiedDate` datetime NOT NULL,
  18.   `LastModifiedUserID` int(11) NOT NULL DEFAULT '0',
  19.   PRIMARY KEY (`ID`),
  20.   KEY `IDX_BusinessId` (`BusinessId`,`Industry`,`Locale`,`Property`),
  21.   KEY `idx_ip` (`Industry`,`Property`)
  22. ) ENGINE=InnoDB AUTO_INCREMENT=14000 DEFAULT CHARSET=latin1 |
  23. +------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  24. 1 row in set (0.00 sec)
从结构来看,不应该出现上面的插入错误,检查是否有触发器。发现如下:

点击(此处)折叠或打开

  1. mysql> show create trigger Trig_***_insert;
  2. +------------------------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
  3. | Trigger | sql_mode | SQL Original Statement | character_set_client | collation_connection | Database Collation |
  4. +------------------------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
  5. | Trig_***_insert | | CREATE DEFINER=`root`@`localhost` TRIGGER `Trig_***_insert` BEFORE INSERT ON `Pro****` FOR EACH ROW begin
  6. SET NEW.CreatedDate = case when NEW.CreatedDate ='0000-00-00 00:00:00' then NOW() when NEW.CreatedDate is null then NOW() end,
  7. NEW.LastModifiedDate =case when NEW.LastModifiedDate ='0000-00-00 00:00:00' then NOW() when NEW.LastModifiedDate is null then NOW() end;
  8. end | utf8 | utf8_general_ci | latin1_swedish_ci |
  9. +------------------------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
  10. 1 row in set (0.04 sec)

 

粗略一看,没有问题,在触发器里判断Createdate是否是'0000-00-00'或NULL,如果是,设为now().LastModifiedDate也是做同样的判断。

等等,如果Createdate做了赋值,但是不是上面的2种情况,会如何呢?看下面这个简单的模拟例子

点击(此处)折叠或打开

  1. mysql> set @A=10;
  2. Query OK, 0 rows affected (0.00 sec)

  3. mysql> set @A =case when @A=10 then 10 when @A=20 then 20 end;
  4. Query OK, 0 rows affected (0.00 sec)

  5. mysql> select @A;
  6. +------+
  7. | @A |
  8. +------+
  9. | 10 |
  10. +------+
  11. 1 row in set (0.00 sec)

  12. mysql> set @A=100;
  13. Query OK, 0 rows affected (0.00 sec)

  14. mysql> set @A =case when @A=10 then 10 when @A=20 then 20 end;
  15. Query OK, 0 rows affected (0.00 sec)

  16. mysql> select @A;
  17. +------+
  18. | @A |
  19. +------+
  20. | NULL |
  21. +------+
  22. 1 row in set (0.00 sec)
这就很明了了。当@A在判断条件里面没有匹配值时,它最终的结果是:NULL
在表定义里面,Createdate字段是不允许NULL的,所以报出错误。
 
修改:


点击(此处)折叠或打开

  1. SET NEW.CreatedDate = case when NEW.CreatedDate ='0000-00-00 00:00:00' then NOW() when NEW.CreatedDate is null then NOW() else NEW.CreatedDate end
 
在判断条件后面加上else部分,这样即使匹配不到任何条件时,还能够使用这个else部分的值。



阅读(1451) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

啦哆A梦2012-05-12 16:38:34

呵呵,不错的文章,平时都没注意。。。