This is schoolwork..
I’m trying to create a table that has a default date value in the future and found a great solution here. This answer says that if a date is given in the INSERT statement, it will override the trigger. However, when I try to populate the table with some old data including a date, the trigger clobbers the date. I feel I’m just missing something obvious.
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();
So when I insert some data:
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';
The date gets overridden:
mysql> select first_name,join_date from member;
+------------+---------------------+
| first_name | join_date |
+------------+---------------------+
| Midori | 2012-03-18 00:00:00 |
+------------+---------------------+
You need to add a conditional to the trigger.
For example: