MySQL date column auto fill with current date - mysql

I have a table where I have a date column. Is there a way for MySQL to auto fill this field whenever I insert a new registry with the current date? Or is this made automatically by default?
P.S.: I'm using PHPMyAdmin

Although it is an old post, maybe this image will help as it is more explicit:
(For phpMyAdmin users)
This configuration sets that field with a value like:
2015-12-11 07:50:47
PS: Note that the timestamp will set the time OF your server!! (i.e. the example above got the time from Pacific Time (07:50:47) but it could have been from a Spanish user at 16:50:47 local time) Keep this in mind.
Also, if you already have a "Created Date" you might need another column that updates the modification date whenever there is an update:
You only need to set on update CURRENT TIME STAMP in Attributes Field.
Ready to rock!

Set Default to in your mySql query
CURRENT_TIMESTAMP

you have to use
now()
function where you want to fill current time.
i.e.:
INSERT INTO user_rights (`user_id`,`right`,`group_id`,`created_date`) VALUES ( '42', '160', '1', now());

I realize this may not be a direct answer to the question but I do believe this is the most useable solution.
I highly recommend using a DATETIME or TIMESTAMP data type for the column in question.If you are utilizing a fairly current version of MySQL, MySQL will do the work for you.
Details:
To be very clear, as of 5.6.5, for both the TIMESTAMP & DATETIME datatypes, you can do the following:
Set a DEFAULT value of the current date & time (using NOW() or one of its aliases such as CURRENT_TIMESTAMP)This means every time you insert a new row into this table a TIMESTAMP or DATETIME column with this default will get the current date and time
Set an ON UPDATE constraint that will UPDATE a column to the current date & time when, (you guessed it) the row is updated
Here's how:
An Example in a CREATE TABLE statement:
CREATE TABLE t1 (
ts1 DATETIME ON UPDATE CURRENT_TIMESTAMP
,ts2 DATETIME DEFAULT NOW()
);
Please note that DATETIME can be replaced with TIMESTAMP for effectively the same functionality.
Additionally I suggest the use of the DATETIME data type over TIMESTAMP as DATETIME has a much larger range of dates it can support. It's worth mentioning that TIMESTAMP is smaller for those few cases that matters. For more details please read my answer here: https://stackoverflow.com/a/26117532/1748266

I have added this to my table and it works
ALTER TABLE Medewerkers ADD med_created TIMESTAMP DEFAULT now();
When you insert data into your record it update automatically the med_created

MySQL unfortunately doesn't allow specifying values other than constants as the default for columns other than TIMESTAMPs.
This is a feature available in MySQL versions 8.0+, but for older versions the only solution for a database defined default would be to use a trigger.

You can do something like this from the SQL screen
ALTER TABLE `table_name` CHANGE `created_at` `created_at` TIMESTAMP NOT NULL

Related

Creating a record table in MYSQL

How can I create a record table when a client adds a new row on a table, which keeps ;
the client's Id,
when the data is added,
Id of the data.
For example I have a PersonTable in mysql; and when a client(with the Id of 2 in users table) adds a new person with the Id of 12, my record table automatically adds cliend_id(2),related_Id(12) and creation time of the new record.
I think there is a way in mysql, writing a trigger would be possible, but is there any possible ways. I'm very beginner of mysql. Thanks
I'm not sure which version you run of MySQL but from version 5.5 onwards this is possible when using TIMESTAMP as a type. Versions >=5.6.5 supports this functionality for type DATETIME as well.
-- TIMESTAMP example with 1 column
CREATE TABLE foo (
'current_ts' TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
-- DATETIME example with 2 columns
CREATE TABLE foo (
'creation_time' DATETIME DEFAULT CURRENT_TIMESTAMP,
'modification_time' DATETIME ON UPDATE CURRENT_TIMESTAMP
)
From the docs:
As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically
initializated and updated to the current date and time (that is, the
current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and
for at most one TIMESTAMP column per table. The following notes first
describe automatic initialization and updating for MySQL 5.6.5 and up,
then the differences for versions preceding 5.6.5.
https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html

Unix Timestamp in MySQL phpMyAdmin

is there any possibility that there is a field called 'time' in the table and there are 3 more fields, and when I insert the 3 fields, the time field is set default to fill in the current UNIX Timestamp? It will be much appreciated if you help me out!
I just inserted the timestamp from PHP to the table.
exactly there is no any default unix_timestamp for mysql. you can set the field as a datetime or timestamp and set its default to CURRENT_TIMESTAMP. then use strtotime function to change back to unix. or set type to int or bigint ordouble and use time() function on every insert row.
I prefer myself to use second way.

How to get SQL to update my record Modified Timestamp correctly?

I am trying to set up an sql table which records when an account was created and when it was last modified. I would like sql to handle this so I don't have to do it in my php files.
I have two columns in my users table (both are of type timestamp):
created
modified
I want the "created" time to never change and always contain the date it was created, and the "modified" to be changed each time the users row is modified. I have the table set up so "created" works as I expect, but when I try to update modified:
ALTER TABLE `users`
CHANGE `modified` `modified` TIMESTAMP NOT NULL
DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP
I get the following error:
1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
Can someone assist me on what I need to do to accomplish this correctly?
It's stil not possible in mysql. You can have them set to the actual time only on INSERTs, only on UPDATEs or on both. However you couldn't have more than one of these auto-TIMESTAMP columns in one table. that's now possible using TRIGGERs if using Mysql 5.x
Refer this article It will help you lot :
Two auto-TIMESTAMP columns in one table with MySQL 5.0

MySQL: Trying to insert a value in a timestamp throws an error

I have a table with this column:
last_modified timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
And it looks like I can not insert a row with a custom timestamp, I get this error:
Incorrect datetime value: '1145868501' for column 'last_modified' at row 1
I am trying to populate this table with data coming from another table, that other table only has a creation_time field which is a DATETIME so I use UNIX_TIMESTAMP(creation_time) to populate the timestamp.
I think the timestamp column with "DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" prevents me from inserting my own stuff, am I right? If yes where is the official doc about that, and what is the best solution? Creating a simple timestamp first then alter the table after inserting data?
Thanks!
EDIT: since people are advising me to not use UNIX_TIMESTAMP, I have to say that I didn't want to use that at the beginning, but I got this kind of error:
Incorrect datetime value: '2010-03-28 02:15:51' for column 'last_modified'
So I thought I had to insert a "real" timestamp...
You can explicitedly insert a value in a TIMESTAMP column. Read: TIMESTAMP Properties
The auto-update TIMESTAMP column, if there is one, is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. If all other columns are set to their current values, the TIMESTAMP column does not change. Automatic updating does not apply if the TIMESTAMP column is explicitly assigned a value other than NULL.
Update
Hehe, the error occurs because - well- there was no datetime with '2010-03-28 02:15:51'! This was in the daylight saving time gap (which usually appears some day in March, between 02:00 - 03:00 or 03:00 - 04:00.
See: Daylight Saving Time explanation.
You're trying to put a long integer into a datetime field. That doesn't work. Remove the call to UNIX_TIMESTAMP() and it should work.
The MySQL TIMESTAMP type is almost identical to a DATETIME; it just has some extra auto-update magic. As far as SELECT and UPDATE is concerned, it is a DATETIME.
If the column is always auto-updated, you can remove the property, getters and setters from the Entity.
Doing this way, it will be ignored in all queries.

Is there a reason to use CURRENT_TIMESTAMP in inserts/updates?

I was using phpmyadmin to insert some dummy data into a table, and noticed it structured the insert like this:
INSERT INTO `arc`.`transactions` (
`txn_id` ,
`date_time` )
VALUES (
'50005',
CURRENT_TIMESTAMP );
Normally, I'd just not include the field "date_time" and the value is created by mySQL (as the field is of type timestamp), and auto-updates whenever you insert or update a record.
Is there any reason to structure the query as you see it above, explicitly inserting CURRENT_TIMESTAMP? Or is this a phpmyadmin oddity?
It could also be a matter of compatibility. CURRENT_TIMESTAMP is defined by the SQL standard. Automatically updated timestamp columns are not. An app that wants to be portable and record timestamps is better off explicitly specifying the currrent timestamp. And what better way to do that than to use the standard, built-in CURRENT_TIMESTAMP function?
You can have TIMESTAMP fields that don't auto-update (see the TIMESTAMP properties page for how to specify these), so depending on how you define the column, it might not be the case that an UPDATE query automatically adjusts the TIMESTAMP field.
PHPMyAdmin is probably taking the safe approach here and specifying the value to ensure it's updated, no matter the column definition. PHPMyAdmin can probably detect the default value if it wants to, so another possible explanation would be compatibility between various server versions and modes for any SQL that it generates.
It depends solely on date_time column definition. if it is like
`date_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
Then you surely don't need to specify anything when inserting or updating a row, but if it is:
`date_time` TIMESTAMP NOT NULL
Then you have to specify something every time you create or update a row