这是学校作业..
我正在尝试创建一个具有默认日期值的表,并找到了一个很好的解决方案here。这个答案说如果INSERT语句中给出了日期,它将覆盖触发器。但是,当我尝试用包含日期的一些旧数据填充表时,触发器会破坏日期。我觉得我只是错过了一些明显的东西。
create table member (
member_id int not null primary key auto_increment,
last_name varchar(25) not null,
first_name varchar(25),
address varchar(100),
city varchar(30),
phone varchar(15),
join_date datetime not null);
create trigger setJoinDate
before insert on member
for each row
set NEW.join_date = curdate();
所以当我插入一些数据时:
insert into member
set
first_name = 'Midori',
last_name = 'Nagayama',
address = '68 Via Centrale',
city = 'Sao Paolo',
phone = '254-852-5764',
join_date = '1991-6-17';
日期被覆盖:
mysql> select first_name,join_date from member;
+------------+---------------------+
| first_name | join_date |
+------------+---------------------+
| Midori | 2012-03-18 00:00:00 |
+------------+---------------------+
答案 0 :(得分:0)
您需要为触发器添加条件。
例如:
create trigger setJoinDate
before insert on member
for each row
set NEW.join_date = IFNULL(NEW.join_date, CURDATE());