Add Date & Time to Database Entry - mysql

How do i add a column to show the date and time of the database entry in a mySQL database?
I have a form that automatically stores into a database, do i need to add hidden inputs in the forms to grab the date and time or is there a specific function on the mySQL setup i can put to do this automatically?

You can define your table to put the timestamp of when the record was entered or last edited.
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
Create a column with type TIMESTAMP and DEFAULT CURRENT_TIMESTAMP

alter table
`your_table`
add column `created` timestamp not null default current_timestamp
Now all inserted rows will have an additional column namned created with the timestamp from creation.

Related

Update mysql row or column data with current datetime?

Here is my table structure below, How can I update my Num_pages column data so that it will insert with newly updated DateTime, rather than the old one.
You can alter your Date_Time_Acqure field as following:
ALTER TABLE mytablename
CHANGE COLUMN `Date_Time_Acqured` `Date_Time_Acqured` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ;
The column then will update to current timestamp whenever there's any change on the row; means that if you change any value from column title and not just Num_Pages, it will also update the Date_Time_Acqure to a current time.

Does mysql supports two default timestamp columns in a table now?

I've two columns in my tables in mysql db:
1. ins_ts
2. upd_ts
1st column should get a default timestamp whenever a row is inserted
2nd column should be updated with the default current timestamp each time a row is updated.
So I need both column to have a default value.
That's what I'm trying to do:
alter table test MODIFY ins_ts timestamp DEFAULT CURRENT_TIMESTAMP;
alter table test MODIFY upd_ts timestamp ON UPDATE CURRENT_TIMESTAMP;
1st works but 2nd one gives me following error:
Error Code: 1293. Incorrect table definition; there can be only one TIMESTAMP column
with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
I had read somewhere that mysql now supports two default timestamp columns, but here it doesn't works.
Please suggest is there any way to achieve the same functionality.
Thanks

Mysql auto-initialization of timestamp column when adding it to an existent table

I want to add a column to a table to keep track of when each record was last updated. I already have some data in the table.
The SQL query I am running is this:
ALTER TABLE `my_table` ADD COLUMN `last_modified` timestamp NOT NULL
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP`;
When adding columns of other types existent records get the default value I specify for that column. However in this case all existent records are getting assigned the value of: 0000-00-00 00:00:00
Why is this happening if I explicitly said I wanted the default to be the current timestamp?

Touch MYSQL record to update TIMESTAMP field

In my Database i have a table with column
`LastUpdated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
i would like to Touch a record in that table so that LastUpdated column will be automatically updated but i don't want to change any value in that row.
Is that possible?
Thank you.
AFAIK, You don't have options to use touch to update mysql table records like you touch file in unix systems. You have to issue an update query to update the timestamp in the LastUpdated column .
UPDATE mytable SET LastUpdated=NOW() WHERE ...

mysql timestamp column

Is it possible to define a timestamp column in a MySQL table that will automatically be updated every time a field in the same row is modified? Ideally this column should initially be set to the time a row was inserted.
Cheers,
Don
You can use the timestamp column as other posters mentioned. Here is the SQL you can use to add the column in:
ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ;
This adds a column called 'lastUpdated' with a default value of the current date/time. When that record is updated (lets say 5 minutes later) that timestamp will automatically update to the current time.
That is the default functionality of the timestamp column type. However, note that the format of this type is yyyymmddhhmmss (all digits, no colons or other separation).
EDIT: The above comment about the format is only true for versions of MySQL < 4.1... Later versions format it like a DateTime
This is what I have observed (MySql 5.7.11) -
The first TIMESTAMP column in the table gets current timestamp as the default value. So, if you do an INSERT or UPDATE without supplying a value, the column will get the current timestamp.
Any subsequent TIMESTAMP columns should have a default value explicitly defined. If you have two TIMESTAMP columns and if you don't specify a default value for the second column, you will get this error while trying to create the table -
ERROR 1067 (42000): Invalid default value for 'COLUMN_NAME'
A MySQL timestamp is set with creation or update time only if their default value is set as it. ALTER TABLE some_table ADD when TIMESTAMP DEFAULT CURRENT_TIMESTAMP.
Otherwise it works just like a DateTime field, only that it's relative to 1970/01/01 UTC, so it's an absolute point in time not depending on a specific timezone as is DateTime.