QUESTION How can I create a MySQL table so, that a field's default value is the output of a function.
Can it be done without an insert trigger?
BACKGROUND: Some entities and the groups they belong to need to be observed on a daily basis.
To do that I created the following MySQL table:
CREATE TABLE entity (
entity_id VARCHAR(10) NOT NULL,
group_id VARCHAR(10) NOT NULL,
first_seen DATE NOT NULL,
last_seen DATE NOT NULL,
PRIMARY KEY (entity_id,group_id)
)
A script parses the entity logs on a daily basis and writes to to entity_raw, I then want to update the tables using
REPLACE INTO entity (entity_id,group_id,last_seen)
SELECT entity_id,group_id,record_date
FROM entity_raw
WHERE (record_date = DATE(DATE_SUB(NOW(),INTERVAL 1 DAY))
Naturally I get an error when inserting new (entity_id,group_id) pairs, since first_seen may not be NULL...
Can this be done in one statement, short of using a trigger (I dislike them, too surprising for maintenance)
or do I need to select for existance first, then insert/update accordingly?