I have a table with a DEC field named product_price and I wanted to add a field called price_updated_date. Is there a way to set the table to automatically insert the current time-stamp whenever the product_price field has been updated?
If not, is there a way to set it to insert the current time-stamp any time the entry is updated at all?
update:
It seems like using a trigger is the best option here. I am new to triggers and having some trouble creating it. Here is my code:
CREATE TRIGGER price_update
AFTER UPDATE ON cart_product
FOR EACH ROW
IF(OLD.product_price != NEW.product_price)
THEN
UPDATE cart_product
SET price_updated_date = CURDATE()
WHERE product_id = NEW.product_id
This is giving me this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8
Why not set properties for that field as:
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
yup, create trigger after update on your table
CREATE TRIGGER price_update AFTER UPDATE on _table_ FOR EACH ROW UPDATE _table_ SET price_updated_date = CURTIME() where id=NEW.id
I note you're using MySQL
The default behaviour of a MySQL timestamp field is to default to NOW() on insert AND on update
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
I know this isn't dependant on which column has been updated as you require, but may be of some use to you or someone else browsing this question.
Its a 5-second job - just add a timestamp field & hey presto
Anytime you run an update statement, make sure you do something like this:
UPDATE table SET `product_price` = 'whatever', price_updated_date = NOW()
This will always insert the current date/time when you change the row.
Use a database trigger:
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
Related
In MySQL, I want to fire a trigger when update on all columns except one column update.
In my table row I have 40 columns. I want trigger to update the column update_time whenever there is an update happens on any field except update_time field.
CREATE TRIGGER `UpdateDateTrigger`
BEFORE UPDATE ON `users`
FOR EACH ROW
IF NOT UPDATE(`update_time`) BEGIN
SET new.update_timestamp = now()
END
But it is not working as expected.
By looking at MySQL automatic update for specific column instead of whole record , I've got the solution to exclude two columns update in trigger
IF !((NEW.last_visited <> OLD.last_visited) ||
(NEW.update_time <> OLD.update_time)) THEN
SET new.update_time = now();
END IF
Thank you!
Instead of having a trigger function I would like to suggest you to create the table wisely so that the update_time field gets updated automatically when something is changed associated with that row.
Please look into the documentation here for automatic update on time for each update of the row. The create syntax is simple and effective.
CREATE TABLE t1 (
// .... Other columns
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
If you want a specific control over each of your column, then writing a trigger is the best idea I think. Please check the answer here.
I have this table:
Table: product
By using PHPMYADMIN, I am thinking of setting a trigger so that whenever I make changes to the price on any item, it will trigger to record the time on the price_change_time for that particular item.
My trigger is like this:
DELIMITER //
CREATE TRIGGER update_time BEFORE UPDATE ON product
FOR EACH ROW
BEGIN
UPDATE product SET price_change_time = NOW() WHERE NEW.price <> OLD.price;
END; //
DELIMITER ;
Sadly this won't work and I get:
MySQL said:Documentation
#1442 - Can't update table 'product' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
I read some of the previous asked questions and answers which is somehow related, but still not able to suit it for my case.
Anyone willing to help? My question is:
1) Is there any way to achieve what I want by just using PHPMYADMIN?
2) If Not then what is the proper way?
You can create a column in a MySQL table something like this:
CREATE TABLE product (
...
price_change_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
...
);
This creates a "magic" column in your table that gets updated to the current time whenever its row is first INSERTed or UPDATEd.
Recent versions of MySQL, but not older ones, also allow this for DATETIME columns.
You can use DATE(price_change_time) to retrieve just the date part of a timestamp.
I have a TRIGGER (before update) on a table like this:
UPDATE contact_us SET updated_at = unix_timestamp() WHERE id = new.id
And when I update a row of that table by using phpMyadmin, it throws this error:
To see it more clear:
#1442 - Can't update table 'contact_us' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
How can I fix the problem?
First of all, you do not need a trigger at all for this functionality, just use a timestamp or datetime field with automatic initialisation. So, define updated_at as follows:
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
If you insist on using a trigger for whatever reason, even then do not use a separate update statement to update the same table the trigger is defined on because that's a no go (endless loop). Use the NEW keyword to access the fields of the newly created record to modify a field's content:
set NEW.updated_at = unix_timestamp();
I've attempted to create a trigger to update the CREATED_DATE field of my table when a new row is inserted, but it ends up breaking the Java Application.
Basically I have an NMS that is writing to the MySql server when there is an alarm. I created the following trigger because the Java application is not applying a timestamp to alarm creation (even though there is a field for it):
CREATE TRIGGER alarm_timestamp AFTER INSERT on Alarm_Details FOR EACH ROW UPDATE Alarm_Details SET CREATED_DATE = NOW() WHERE ID = NEW.ID;
After I make this trigger, the alarms never write to the table. Then I drop the trigger and they instantly work again.
Is there a problem with the way I wrote the trigger? I'm surprised a trigger in the Sql server is preventing the application from submitting an insert query.
I have now tried to add a default value to the column with the following:
alter table Alarm_Details add constraint df_alarm_time default NOW() for CREATED_DATE;
I get a syntax error though near 'default NOW() for CREATED_DATE.'
I also tried:
ALTER TABLE Alarm_Details ALTER CREATED_DATE SET DEFAULT NOW();
It doesn't like NOW, CURRENT_TIMESTAMP, or anything. The column is set to type datetime.
I am using MySql 5.5.25.
I need to keep track of the time a row was inserted into the database, and the time it was last modified.
I tried to create two separate columns, and use CURRENT_TIMESTAMP:
create table def (
id int,
creation timestamp
default CURRENT_TIMESTAMP,
modification timestamp
on update CURRENT_TIMESTAMP
);
However, this produced an error:
ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
What is the best way to do this?
I'm thinking stored procedure, but looking for a standard solution. I'm also concerned with access privileges -- as few programs/things should be able to touch the timestamps as possible.
Although I would prefer MySQL answers, solutions for other RDBMS's are also appreciated!
Ya this is a lame limitation on MySQL. If you are going through an application you can add a time() call for the created_at column, and let the updated_at column use the CURRENT_TIMESTAMP.
$sql = "INSERT INTO my_table SET name = 'Mike', created_at = " . time();
I'd opt to do this on the created_at column as it probably won't be touched as often as the updated_at column.
-- Edit --
Better yet, use MySQL's built in now() function. This way you only need to be concerned with the timezone of the mysql server, and not the timezones of the app server AND the mysql server.
$sql = "INSERT INTO my_table SET name = 'Mike', created_at = NOW()";
You can use a trigger. The application can also set the value, but if do, it will be overwritten by the database.
delimiter //
CREATE TRIGGER def_bef_update BEFORE UPDATE ON def FOR EACH ROW BEGIN
SET NEW.modification = CURRENT_TIMESTAMP;
END//
delimiter ;
You can also use it to check the data and update your modification date only if has important changes.