0

我在插入触发器后遇到问题。当我在表中插入数据时,它应该自动更新日期,它应该是当前日期。但是当我执行以下查询时,它在我的表中无法正常工作。首先我创建了一个表:

DROP TABLE IF EXISTS `ignite`.`products`;
CREATE TABLE  `ignite`.`products`(
  `products_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `products_model` varchar(30) NOT NULL,
  `model_hash` bigint(20) unsigned NOT NULL,
  `Product_name` varchar(45) NOT NULL,
  `created` datetime NOT NULL,
  PRIMARY KEY (`products_id`)
) ENGINE=MyISAM AUTO_INCREMENT=977 DEFAULT CHARSET=latin1;

然后我创建了这个触发器:

use ignite;
CREATE TRIGGER created_date BEFORE INSERT ON products
FOR EACH ROW
SET NEW.created = NOW();

但是当我在这个表中插入数据时:

use ignite;
INSERT INTO products
(products_id, products_model, model_hash, Product_name)
values
(123, "456645", 457567, "awetert");

这显示以下错误:

field 'created' does not have default value. error-1364
4

1 回答 1

0

这是因为在您的INSERT声明中不包含这些created值。例如,您需要将其包含为零日期时间值:

INSERT INTO products
(products_id, products_model, model_hash, Product_name, created)
values
(123, "456645", 457567, "awetert", "0000-00-00 00:00:00");
于 2012-12-10T05:48:05.523 回答