Is it possible to update zero columns in a MySQL update query?
I'd like to utilise the automatic updating of the timestamp column when this particular row is selected. Does anyone have any clue?
So you want your update query to only update the timestamp column? Just update it yourself instead of relying on the automatic update:
UPDATE mytable SET tscolumn = NOW() WHERE ...
Just try something like :
UPDATE myTable SET someField = someField WHERE id = myId
So the data values won't move, and the automatic timestamp will be updated.
As said by GaryG, you may also update the timestamp directly.
Related
I'm trying to update a column from a MySQL table so that it changes to current timestamp when the new date from another column is greater than the existing date from that column, and stays the same otherwise.
For example, let's say that I have a table with two columns, enddate and enddate_modif. If the date in enddate in the updating record is greater than the date in enddate in the existing table, I want enddate_modif to change to current timestamp. Otherwise, it keeps the same value.
How can I do it in MySQL?
Thank you in advance.
An after update trigger should do the trick.
Try:
CREATE TRIGGER `enddate_modif_trigger` BEFORE UPDATE ON `my_table`
FOR EACH ROW
SET NEW.enddate_modif = (SELECT CASE WHEN NEW.enddate > enddate_modif
THEN CURRENT_TIMESTAMP
ELSE NEW.enddate_modif
END
FROM my_table
);
I think you can use an after update trigger
something like this
if(New.enddate >Old.enddate ) then
update yourtable set enddate_modif=NOW() where Your_table_ID=NEW.Your_table_ID;
end iff;
When I append,
ON UPDATE CURRENT_TIMESTAMP
I know the column will be updated when the row is updated.
Is there any way to make the clause optional?
I mean a way to update the column only when client didn't send the column value.
Use trigger logic:
CREATE TRIGGER tr
BEFORE UPDATE
ON tablename
FOR EACH ROW
SET NEW.datetimefield = COALESCE(NEW.datetimefield, CURRENT_TIMESTAMP);
Do not forget to remove ON UPDATE attribute from field definition.
One simple way to handle your requirement would be to always include the timestamp column in question in the update, but wrap it in COALESCE and use CURRENT_TIMESTAMP as the default value, e.g.
UPDATE yourTable
SET
col1 = <some value>,
co2l = <some other value>,
ts_col = COALESCE(?, CURRENT_TIMESTAMP)
WHERE ...
To the ? placeholder, you would either bind a value, should you want to explicitly update it, or just bind NULL if you don't want to update it. The NULL value would be replaced with the current timestamp.
I have one field in a table and I'm updating it often; what I want to do is if when that field is updated it equals the same value of another field then update another field.
Let me explain, basically a quicker way of doing this:
UPDATE my_table SET spots_taken=spots_taken+1 WHERE id=1234;
UPDATE my_table SET open=1 WHERE id=1234 AND spots_taken=spots;
Can this be done in one query?
Try using CASE:
UPDATE my_table
SET spots_taken=spots_taken+1,
open = (CASE WHEN spots_taken=spots THEN 1 ELSE open END)
WHERE id=1234
I've got a table with a column, lets call it table_column that is currently null for all rows of the table. I'd like to insert the value "test" into that column for all rows. Can someone give me the SQL for this?
I've tried INSERT INTO table (table_column) VALUES ("test"); but that only populates that last row. How do I do all of the rows at once?
You're looking for UPDATE not insert.
UPDATE mytable
SET table_column = 'test';
UPDATE will change the values of existing rows (and can include a WHERE to make it only affect specific rows), whereas INSERT is adding a new row (which makes it look like it changed only the last row, but in effect is adding a new row with that value).
This is because in relational database terminology, what you want to do is not called "inserting", but "UPDATING" - you are updating an existing row's field from one value (NULL in your case) to "test"
UPDATE your_table SET table_column = "test"
WHERE table_column = NULL
You don't need the second line if you want to update 100% of rows.
To update the content of existing rows use the UPDATE statement:
UPDATE table_name SET table_column = 'test';
What you're actually doing is adding rows. To update the content of existing rows use the UPDATE statement:
UPDATE table SET table_column = 'test';
UPDATE `table` SET table_column='test';
The SQL you need is:
Update table set table_column = "test";
The SQL you posted creates a new row rather than updating existing rows.
To create a new empty column and fill it with the same value (here 100) for every row (in Toad for Oracle):
ALTER TABLE my_table ADD new_column INT;
UPDATE my_table SET new_column = 100;
I've a table where I've two fields:
dateCreated
dateUpdated
I want both fields to hold timestamps, dateCreated to have a DEFAULT CURRENT TIMESTAMP flag and dateUpdated to have a ON UPDATE CURRENT TIMESTAMP behavior, however seems that I can't have more than one timestamps field types on the same table.
It would be really useful if the database could take care of this for me, can I circumvent this issue somehow?
YES WE CAN.
You can use a trigger to update the field, like so:
create trigger CreationTimestamp after insert on MyTable for each row
begin
update MyTable set CreateField = UpdateField where id = new.id
end
This way, you can set your auto-update field using the native MySQL method, and use this to capture the creation date.