Update mysql table with same values and still get a timestamp update - mysql

So I have this stamp timestamp DEFAULT NOW() ON UPDATE NOW() row on my table, and I need it to update even when the update I'm executing is basically same data on all fields.
Is there any way of doing this within the declaration of the table, like some other option other than on update, or do I have to force a stamp = now() every time I update (and remove the on update of course since it will be useless).
I've seen this thread, but it only answers what is happening and why, not how to get around it other than forcing it indirectly

As #veeTrain said, it'd be easy if you added it to your update statement. This is just another way of doing it, you can also use unix_timestamp().
UPDATEtable_nameSETlast_logged_in= unix_timestamp() WHEREid= '$user_id'
I know my response is late, but I ran into a similar issue and figured I'd share my solution for those encountering this thread in the future.

You'd have to use a trigger to force it each time.
DELIMITER GO
CREATE TRIGGER `mydb`.`mytable_U` BEFORE UPDATE ON `mydb`.`mytable`
FOR EACH ROW
BEGIN
SET NEW.stamp = CURRENT_TIMESTAMP;
END
GO
DELIMITER ;

I find it easier to just do the following as part of your update statement:
UPDATE table_name SET last_logged_in = NOW() WHERE `user_id`...
This way the value is always updated and if nothing else has changed the timestamp still gets updated. Thanks for your question; it was the same as mine.

Related

How to set a MySQL Trigger the correct way

i have a database table that gets updated via a CMS. Sometimes, there is one field that gets updated and then another field should get updated as well. I tried to get this done by just modifying the php code of the CMS, where the value gets saved in the DB. The problem is, with every update of the CMS this is gone.
I came across MySQL Triggers and i think this is a good way to do this. So whenever this field is updated, it should also update the other field with a value that will always be the same. But i cant quite get my head around the syntax, to get this going...
I tried:
CREATE TRIGGER after_unique_content_update
AFTER UPDATE
ON articles FOR EACH ROW
BEGIN
IF OLD.date <> new.date THEN
INSERT INTO release=2020
END IF;
END
But it is not working. So what i try to do: Every time in the CMS someone changes the date, the release for that db entry should be set to 2020. The standard for release is NULL. With the above trigger, nothing happens after the date is changed... Never worked with Trigger or Events in MySQL before...
How can i set the release to 2020 when the date field for that entry is updated?
Thanks for your help in advance!
best regards
CREATE TRIGGER before_unique_content_update
BEFORE UPDATE
ON articles
FOR EACH ROW
SET NEW.release = CASE WHEN OLD.date <> NEW.date
THEN 2020
ELSE NEW.release
END;
Firstly the condition is checked by the trigger, and, if the condition matched, new value to the column is set to 2020 (or, maybe, it's better to set it not to 2020 but to YEAR(CURRENT_DATE)?). If the condition not matched then the value is not altered (assigned value is the same as current one).
After the trigger performs this task, actual table data update is performed.
AFTER UPDATE trigger is not applicable - when it fires the data in the table is already set to the value specified in a query.

MySql cannot update a DATETIME field - no record found

I have a table in MySql 5.6.10 defined as this:
When I do a select query (from HeidiSQL client) for a particular record filtering on the id_failed_delivery_log column, the record is found successfully, but when I use the same filter for the UPDATE command, then no record is found and no error reported:
When I update a different column using the same filter, the update works and I can see the updated value. Then there is an issue with an update to this particular column.
I've also tried updating with a time function instead of hard-coded date value, for example with the now() function but I still get the same result and the record is not found.
Could it be caused by the 'Default' value is set to CURRENT_TIMESTAMP?
After further investigation I found the reason why I couldn't update that field. I was not totally familiarized with the database definition and I found that there was a trigger in the same schema that forced to keep the date_created column with the same value:
SET #OLDTMP_SQL_MODE=##SQL_MODE, SQL_MODE='STRICT_ALL_TABLES';
DELIMITER //
CREATE TRIGGER `failed_delivery_log_before_update` BEFORE UPDATE ON
`failed_delivery_log` FOR EACH ROW BEGIN
SET NEW.date_created = OLD.date_created ;
END//
DELIMITER ;
SET SQL_MODE=#OLDTMP_SQL_MODE;
I removed this trigger temporarily in order to test. Once removed, the updated worked fine. The trigger execution was not reported in the SQL client, so it was difficult to find out its execution.

MYSQL Change Values based on Table values (Constraint or Trigger?)

Quick few questions on MYSQL syntax, it seems extremely hard to find these answers on google. Is it possible to set a constraint or trigger so that when a value of a column reaches a certain point it get's reset and something else is done. Or also if say hourLog is greater than zero, then licence status changes from Default value of INVALID to Valid.
I know all this could be done with a program that was using the database, but was just curious if there were MYSQL commands to do this automatically with a trigger or a Constraint.
Sorry if this was posted, I just was looking for quick clarification for a class.
You could use these two triggers, one for the UPDATE, one for the INSERT:
CREATE TRIGGER upd_yourtable BEFORE UPDATE ON yourtable
FOR EACH ROW
SET new.licenseStatus=
CASE WHEN new.hourLog>0 THEN 'Valid' ELSE 'INVALID' END
;
CREATE TRIGGER ins_yourtable BEFORE INSERT ON yourtable
FOR EACH ROW
SET new.licenseStatus=
CASE WHEN new.hourLog>0 THEN 'Valid' ELSE 'INVALID' END
;
Please see fiddle here.

MySQL: Is there a way to automatically set datetime field to record creation timestamp?

I know there is TIMESTAMP data type that automatically updates to timestamp value when a record is updated, and I already have such column.
Besides that I'd like to have a column that automatically populates to NOW() (or CURRENT_TIMESTAMP) and never changes, but MySQL DEFAULT doesn't appear to support function calls.
Please post only pure MySQL answers. I know how to do it at application level.
EDIT: If there's no such feature - I'd appreciate to hear that.
EDIT2: MySQL version is 5.0.32
Use a trigger to set the default.
DELIMITER |
CREATE
TRIGGER trigger_name BEFORE INSERT ON tbl_name FOR EACH ROW
BEGIN
SET NEW.colname = NOW();
END;
|
Try this one, including the delimiter.
I'd like to have a column that automatically populates to NOW() (or CURRENT_TIMESTAMP) and never changes
By saying you'd like it to populate to NOW() it sounds like you're referring to the initial INSERT. If that's the case, can you just use that? Something like
INSERT INTO table (field1,field2,my_datetime) VALUES (1,'a',NOW())

Blocking '0000-00-00' from MySQL Date Fields

I have a database where old code likes to insert '0000-00-00' in Date and DateTime columns instead of a real date. So I have the following two questions:
Is there anything that I could do on the db level to block this? I know that I can set a column to be not-null, but that does not seem to be blocking these zero values.
What is the best way to detect the existing zero values in date fields? I have about a hundred tables with 2-3 date columns each and I don't want to query them individually.
Followup:
The default is already set to null. A long time ago, the default was '0000-00-00'. Some code still explicitly places '0000-00-00'. I would prefer to force that code to throw an error so I could isolate and remove it.
Is there anything that I could do on the db level to block this?
Yes, enable the NO_ZERO_DATE mode:
SET sql_mode = 'NO_ZERO_DATE';
The behaviour is documented. Additionally, you might want to also set the mode to include NO_ZERO_IN_DATE...
Also make sure the sql_mode includes either STRICT_ALL_TABLES or STRICT_TRANS_TABLES; without these NO_ZERO_IN_DATE only give a warning, but insert still succeeds.
What is the best way to detect the existing zero values in date fields? I have about a hundred tables with 2-3 date columns each and I don't want to query them individually.
Separate columns means they have to be checked individually--nothing you can do about that.
Assuming you can't easily fix the data and "SET sql_mode = 'NO_ZERO_DATE';", you could create a view on the table...
CREATE VIEW filter AS
SELECT other_column,
CASE
WHEN realtable.dodgy_date = 0 THEN NULL
ELSE realtable.dodgy_date
END AS dodgy_date
FROM realtable;
SET SQL_MODE='ALLOW_INVALID_DATES';
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'NO_ZERO_DATE',''));
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'NO_ZERO_IN_DATE',''));
Just run above queries on database. It will solve the issue permanently.
If it doesn't matter what date goes in there (ie as long as it's non-zero) you can change the column definition to use NOW() as the default. Probably not an ideal solution, but it does satisfy the criteria :
1) Not-null
2) Non-zero
I'm actually really not proud of that suggestion
You could make the columns nullable and have NULL as the default value, but it sounds like you already have that and it's not working. ALTHOUGH... it could be the tool you're using to display the data doesn't like displaying NULL dates... what tool are you using? Or is the '0000-00-00' showing up in data retreived by code?
You could set a default value that is non-null and also easily recognizable as a default such as 1900-01-01 (assuming you don't normally deal with dates that are close to this date).
A trigger can be used to enforce values for columns.
set the timestamp by default is maybe an option for you, use table change statement for that:
ALTER TABLE mytable CHANGE date_update timestamp NOT NULL DEFAULT CURRENT_DATE()
MySQL CURRENT_DATE() documentation at w3c resource
To remove Zero Dates and replace them by e.g. the current date do this:
UPDATE mytable SET date_update = CURRENT_DATE() where date_update = "0000-00-00"
This is the trigger I use:
delimiter //
CREATE TRIGGER bad_date BEFORE INSERT ON some_table
FOR EACH ROW
BEGIN
IF NEW.the_date='0000-00-00' THEN
SET NEW.the_date= CURDATE();
END IF;
END;//
If updates are a concern, add a separate trigger (BEFORE UPDATE) to do the same thing.