I have a table with two fields insert_time and update_time.
The type of insert_time is varchar(30) NOT NULL DEFAULT '' while the type of update_time is timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
insert into tableName(inser_time,update_time) values('2017-02-17 20:30:38')
will make the update_time lose its meanings.
So how to make the two fields have a same server time when inserted if not update the Mysql version?
Just set both fields to the same value ?
insert into tableName(inser_time,update_time)
values('2017-02-17 20:30:38','2017-02-17 20:30:38')
EDIT to use current time
insert into tableName(inser_time,update_time)
values(now(), now())
EDIT 2 From mysql manual at:
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_now
Functions that return the current date or time each are evaluated only
once per query at the start of query execution. This means that
multiple references to a function such as NOW() within a single query
always produce the same result.
Related
If I have a column in a table of type TIMESTAMP and has as default: CURRENT_TIMESTAMP does this column get updated to the current timestamp if I update the value
of any other column in the the same row?
It seems that it does not but I am not sure if this is what should happen.
I can not understand what this means (from MySQL documentation):
If the column is auto-updated, it is automatically updated to the
current timestamp when the value of any other column in the row is
changed from its current value. The column remains unchanged if all
other columns are set to their current values. To prevent the column
from updating when other columns change, explicitly set it to its
current value. To update the column even when other columns do not
change, explicitly set it to the value it should have]2
Give the command SHOW CREATE TABLE whatever
Then look at the table definition.
It probably has a line like this
logtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
in it. DEFAULT CURRENT_TIMESTAMP means that any INSERT without an explicit time stamp setting uses the current time. Likewise, ON UPDATE CURRENT_TIMESTAMP means that any update without an explicit timestamp results in an update to the current timestamp value.
You can control this default behavior when creating your table.
Or, if the timestamp column wasn't created correctly in the first place, you can change it.
ALTER TABLE whatevertable
CHANGE whatevercolumn
whatevercolumn TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
This will cause both INSERT and UPDATE operations on the table automatically to update your timestamp column. If you want to update whatevertable without changing the timestamp, that is,
To prevent the column from updating when other columns change
then you need to issue this kind of update.
UPDATE whatevertable
SET something = 'newvalue',
whatevercolumn = whatevercolumn
WHERE someindex = 'indexvalue'
This works with TIMESTAMP and DATETIME columns. (Prior to MySQL version 5.6.5 it only worked with TIMESTAMPs) When you use TIMESTAMPs, time zones are accounted for: on a correctly configured server machine, those values are always stored in UTC and translated to local time upon retrieval.
I think you have to define the timestamp column like this
CREATE TABLE t1
(
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
See here
An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values.
To explain it let's imagine you have only one row:
-------------------------------
| price | updated_at |
-------------------------------
| 2 | 2018-02-26 16:16:17 |
-------------------------------
Now, if you run the following update column:
update my_table
set price = 2
it will not change the value of updated_at, since price value wasn't actually changed (it was already 2).
But if you have another row with price value other than 2, then the updated_at value of that row (with price <> 3) will be updated to CURRENT_TIMESTAMP.
Add a trigger in database:
DELIMITER //
CREATE TRIGGER update_user_password
BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF OLD.password <> NEW.password THEN
SET NEW.password_changed_on = NOW();
END IF;
END //
DELIMITER ;
The password changed time will update only when password column is changed.
Adding where to find UPDATE CURRENT_TIMESTAMP because for new people this is a confusion.
Most people will use phpmyadmin or something like it.
Default value you select CURRENT_TIMESTAMP
Attributes (a different drop down) you select UPDATE CURRENT_TIMESTAMP
need an advice, how to auto-store datetime value for my historyActivity table in select insert mysql query. This is an example:
INSERT INTO history_sequence(CODE, LAST_MOUNTH, LAST_VALUE) SELECT CODE, MOUNTH, VALUE FROM seq WHERE CODE = CODEVALUE
i just want to add datetime to see time when the data inserted. Need help please
You can do this in the MySQL table definition:
ALTER TABLE history_sequence ADD inserted TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
When records are inserted into the table table, the inserted column gets automatically populated with the current timestamp.
If I have a column in a table of type TIMESTAMP and has as default: CURRENT_TIMESTAMP does this column get updated to the current timestamp if I update the value
of any other column in the the same row?
It seems that it does not but I am not sure if this is what should happen.
I can not understand what this means (from MySQL documentation):
If the column is auto-updated, it is automatically updated to the
current timestamp when the value of any other column in the row is
changed from its current value. The column remains unchanged if all
other columns are set to their current values. To prevent the column
from updating when other columns change, explicitly set it to its
current value. To update the column even when other columns do not
change, explicitly set it to the value it should have]2
Give the command SHOW CREATE TABLE whatever
Then look at the table definition.
It probably has a line like this
logtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
in it. DEFAULT CURRENT_TIMESTAMP means that any INSERT without an explicit time stamp setting uses the current time. Likewise, ON UPDATE CURRENT_TIMESTAMP means that any update without an explicit timestamp results in an update to the current timestamp value.
You can control this default behavior when creating your table.
Or, if the timestamp column wasn't created correctly in the first place, you can change it.
ALTER TABLE whatevertable
CHANGE whatevercolumn
whatevercolumn TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
This will cause both INSERT and UPDATE operations on the table automatically to update your timestamp column. If you want to update whatevertable without changing the timestamp, that is,
To prevent the column from updating when other columns change
then you need to issue this kind of update.
UPDATE whatevertable
SET something = 'newvalue',
whatevercolumn = whatevercolumn
WHERE someindex = 'indexvalue'
This works with TIMESTAMP and DATETIME columns. (Prior to MySQL version 5.6.5 it only worked with TIMESTAMPs) When you use TIMESTAMPs, time zones are accounted for: on a correctly configured server machine, those values are always stored in UTC and translated to local time upon retrieval.
I think you have to define the timestamp column like this
CREATE TABLE t1
(
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
See here
An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values.
To explain it let's imagine you have only one row:
-------------------------------
| price | updated_at |
-------------------------------
| 2 | 2018-02-26 16:16:17 |
-------------------------------
Now, if you run the following update column:
update my_table
set price = 2
it will not change the value of updated_at, since price value wasn't actually changed (it was already 2).
But if you have another row with price value other than 2, then the updated_at value of that row (with price <> 3) will be updated to CURRENT_TIMESTAMP.
Add a trigger in database:
DELIMITER //
CREATE TRIGGER update_user_password
BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF OLD.password <> NEW.password THEN
SET NEW.password_changed_on = NOW();
END IF;
END //
DELIMITER ;
The password changed time will update only when password column is changed.
Adding where to find UPDATE CURRENT_TIMESTAMP because for new people this is a confusion.
Most people will use phpmyadmin or something like it.
Default value you select CURRENT_TIMESTAMP
Attributes (a different drop down) you select UPDATE CURRENT_TIMESTAMP
I have some SQL code below which enables me to change all instances of quantities that are '0' and change them to '1' within the 'quantity' column only. It works ok but...
What I am trying to do, however, is not affect the timestamp column (called 'entry_date') on the affected rows ie. keep the original time the entry was made. When I run this query it replaces the time the entry was originally made with the time the query below was ran.
How do I get around this? I have basic-intermediate PHP knowledge but my SQL knowledge isn't great. Any help would be appreciated.
UPDATE databasename.tablename SET quantity = '1'
WHERE tablename.quantity = '0';
You could try something like this:
UPDATE databasename.tablename SET quantity = '1', entry_date = entry_date
WHERE tablename.quantity = '0';
and see if it doesn't override the behaviour specified by the ON UPDATE CURRENT_TIMESTAMP.
First, DESCRIBE tablename, and note "entry_date" has Extra info "on update CURRENT_TIMESTAMP". That's the behavior you want to avoid.
Next, ALTER TABLE tablename MODIFY COLUMN entry_date TIMESTAMP [NULL | NOT NULL] DEFAULT CURRENT_TIMESTAMP;
(You specify 'NULL' or 'NOT NULL' as appropriate.)
Finally, DESCRIBE tablename again, and note that the Extra info is gone.
When you want timestamp field entry_date to not change automatically on update.. you need following query to execute before any update query on your table
ALTER TABLE `tableName` CHANGE `entry_date` `entry_date` TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
Opposite : After this if you again want timestamp field entry_date to change automatically on update.. you need following query to execute before any update query on your table
ALTER TABLE `tableName` CHANGE `entry_date` `entry_date` TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
I am creating table with sqlalchemy.
user = Table('users', Metadata,
Column('datecreated', TIMESTAMP,
server_default=text('CURRENT_TIMESTAMP')),
Column('datemodified', TIMESTAMP,
server_onupdate=text('CURRENT_TIMESTAMP')),
)
But this will not set DEFAULT ON UPDATE CURRENT_TIMESTAMP.
I checked out How do you get SQLAlchemy to override MySQL "on update CURRENT_TIMESTAMP" but that will for literal I need to wire that in create table definition.
You can hijack the server_default to set also the ON UPDATE:
Column('datemodified', TIMESTAMP,
server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
This generates the following column entry:
datemodified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
However, Mchl's answer still applies: There can be only one automated TIMESTAMP column in a table (http://dev.mysql.com/doc/refman/5.5/en/timestamp.html)
Also note that the order of the columns is of importance! If you have a TIMESTAMP column without DEFAULT and ON UPDATE modifiers, and it is the first TIMESTAMP column in your table,
it automatically will be set to DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP. (http://dev.mysql.com/doc/refman/5.5/en/timestamp.html)
So this is fine:
Column('datemodified', TIMESTAMP,
server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
Column('datecreated', TIMESTAMP)
while this is not:
Column('datecreated', TIMESTAMP)
Column('datemodified', TIMESTAMP,
server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
In order to set the `datecreated' to the current time during first entry of a row, just set its value explicitly to NULL. Again, from http://dev.mysql.com/doc/refman/5.5/en/timestamp.html:
By default, TIMESTAMP columns are NOT NULL, cannot contain NULL
values, and assigning NULL assigns the current timestamp.
If you're on MySQL 5.6 or later please scroll down for the relevant answer. Users of older versions, please read this one.
Until MySQL 5.6. in one table, you can only have one 'automated' TIMESTAMP column.
From: http://dev.mysql.com/doc/refman/5.5/en/timestamp.html
For one TIMESTAMP column in a table, you can assign the current
timestamp as the default value and the auto-update value. It is
possible to have the current timestamp be the default value for
initializing the column, for the auto-update value, or both. It is not
possible to have the current timestamp be the default value for one
column and the auto-update value for another column.
This worked on Mysql 8
sa.Column('created_at', sa.TIMESTAMP, server_default=func.now()),
sa.Column('updated_at', sa.TIMESTAMP, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
For some reason the server_onupdate options was not working.
from sqlalchemy.sql import func
Column('datemodified',
TIMESTAMP,
server_onupdate=text(
' ON UPDATE '.join([str(func.current_timestamp())] * 2)))
This way you use SQLAlchemy's inner func.current_timestamp() function.
If all you want is DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP in MySQL, then just setting the TIMESTAMP column in sqlalchemy to be non-nullable works
Column(TIMESTAMP, nullable=False)