Trying to understand how to make a trigger in mysql to automatically set a value in a record field. Not even sure if I need a BEFORE or AFTER trigger.
I have a table with one field named date_created and another date_updated, both of type datetime. The date_created field is set to NOT NULL and default to CURRENT_TIMESTAMP. The date_updated field is currently set to NULL, but I can change that if needed.
Whenever a new record is created, the date_created field gets filled automatically. This way I can omit the field from an input FORM.
Now whenever the record gets updated, I'd like for the date_updated field to be filled-in automatically - for any subsequent updates.
I've tried several versions but keep getting errors.
You don't need a trigger for this case. Use DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
Read this manual page for details: https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html
Related
I'm creating a user registration form and when I get to confirming the user's email by emailing them, I need some sort of unique string to confirm against.
Instead of generating one in PHP and inserting it into the database, I wanted to try and add a column to my table that would hold a unique value that I could use whenever I needed to confirm something.
What I want to do is set the value to an MD5 of the current timestamp. I tried just doing SELECT MD5(CURRENT_TIMESTAMP) in phpMyAdmin just to see if it would let me and it did so I thought I'd add that to an update condition but it doesn't seem to be letting me.
ALTER TABLE users ADD confirmation VARCHAR(40) DEFAULT NULL ON UPDATE MD5(CURRENT_TIMESTAMP);
The above is what I've tried. I get an error and I don't know how else to do it.
Is there anyway I can do this or something similar? Side question, does the ON UPDATE trigger on a row that just got inserted?
The syntax you are trying to use doesn't exist. It looks like you are thinking of ON UPDATE CURRENT_TIMESTAMP but that is a rather specific command, as per https://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is
specific to TIMESTAMP.
So the ON UPDATE clause only works with CURRENT_TIMESTAMP and only on fields of type TIMESTAMP.
If you want to use the MD5 of the current timestamp either set a trigger, or just manually set the value (e.g. UPDATE users SET confirmation=MD5(CURRENT_TIMESTAMP()) WHERE user_id=123).
Bare in mind that the MD5 of the current timestamp is something that could be quite easily guessed / brute forced, so don't rely on it for security.
Use a universal unique identifier for this purpose. It's a 128-bit unique number; it's designed for this kind of thing.
It has a string representation that fits in 36 bytes.
aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
You can generate these things in most programming languages. In MySQL you use the UUID() function to get one. Every time you call UUID(), you're guaranteed to get a new value.
Add a CHAR(36) column to your database, or just use your VARCHAR(40) column.
You can't use data definition language (ALTER TABLE) to declare ON UPDATE except for a native timestamp. You'll need application code to set your UUID values, just like you do for MD5(CURRENT_TIMESTAMP).
I am using phpMyAdmin for my project and I have a table without date/datetime field.
I am wondering if there's a way to know when I inserted each record on the database
While designing your database if you forgot to keep an extra field to store the insertion time of a row then you have only one option left to know the the insertion time.
But the condition is you must have binary logging enabled prior to it.
Reference
You may check if binary logging is enabled in your part by executing this query :
SHOW VARIABLES LIKE 'log_bin';
Further Note:
And from now on you may keep an extra field which will track the insertion time of each row.
Change your table design :
Add an extra field in your table with datatype "timestamp/datetime" with default value CURRENT_TIMESTAMP and on update CURRENT_TIMESTAMP.
I have a users table in my mysql database and there I record the time user signed up by having a column timeOfUserSignup (or something like that) of the type TIMESTAMP and default is set to CURRENT_TIME.
But what will happen if I update that user's info. Will that column value change to the time when the update occurred?
No, it will not be changed on updates.
A default value will only be generated if you don't pass any value on table insertion. If you need to change a value on updates too, then you can use an additional trigger for that.
Is there any way, in MySQL, to dynamically generate a field value? For example a field that stamps the exact date and time that a row was inserted.
You could just get the interface to do it (i.e. PHP) but in a case where you have more than one handler it would be safer if I could get MySQL to do it independently.
The MySQL TIMESTAMP data type has a special default called CURRENT_TIMESTAMP that does this.
Read all about it:
http://dev.mysql.com/doc/refman/5.1/en/timestamp.html
EDIT:
For the more generic case, use a trigger. You can set the value on an insert using a BEFORE INSERT triggers, and likewise you can set the value on updates using a separate BEFORE UPDATE trigger:
http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html
Is it possible to set a field to Timestamp but not have it change on update to current timestamp?
I'm trying to do that using phpMyAdmin and it doesn't let me remove the default on update CURRENT_TIMESTAMP
See this question and answer: Support user time zones
I am trying to use the TIMESTAMP as it will allow me to play around with the timezones easily.
Is it not possible to keep the data in that field intact when updating the same row?
This is the behaviour of TIMESTAMP. It can be confusing alright. Read this to work through it. Alternatively consider using a DATETIME.
The server allows any combination of DEFAULT and ON UPDATE, if phpMyAdmin doesn't let you set it, then it's maybe a bug in phpMyAdmin. Anyway, it's important to note that timestamp columns are treated specially in mysql, so if you have more than one of this type in your table, it's well possible that it's not gonna work the way you expect.
From the mysql docs:
In a CREATE TABLE statement, the first TIMESTAMP column can be declared in any of the following ways:
With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses, the column has the current timestamp for its default value, and is automatically updated.
With neither DEFAULT nor ON UPDATE clauses, it is the same as DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
With a DEFAULT CURRENT_TIMESTAMP clause and no ON UPDATE clause, the column has the current timestamp for its default value but is not automatically updated.
With no DEFAULT clause and with an ON UPDATE CURRENT_TIMESTAMP clause, the column has a default of 0 and is automatically updated.
Use the command interface.
Looks like you must specify an "DEFAULT CURRENT_TIMESTAMP" attribute on table creation to get that behaviour.
documentation here.