Deleted value in trigger MySQL in WHERE statement? - mysql

I want to do simple trigger on delete in table X which will change value in table Table which has the same id.
UPDATE Table T
SET T.spots = T.spots +1
Where T.id = delete.id
But it doesnt work. I am not sure if "delete.columnName" works.

As mentioned by fejese in the comments, use
UPDATE Table T
SET T.spots = T.spots +1
Where T.id = OLD.id -- Use OLD.id instead of delete.id

Related

Trying to use value from updated row in a where statement in my trigger

I want to use a trigger to automatically update another table but I'm having some problems with it.
DELIMITER $$
DROP TRIGGER IF EXISTS `trigger1` $$
CREATE TRIGGER `trigger1`
AFTER UPDATE ON `table1` FOR EACH ROW
UPDATE `table4`
inner join (SELECT o.`Name`,
o.Date,
(o.`Availability` * (c.Rate)) total
FROM `table2` o
LEFT JOIN `table1` r
ON o.`Name` = r.`Name`
AND o.Date = r.Date
LEFT JOIN (SELECT table3.`Name`,Choke, Rate FROM table3
left join `table1` as w
on table3.`Name` = w.`Name`
and table3.Choke = w.`Size`
where w.`Name` = table3.`Name`
and table3.Date <= w.Date
ORDER BY table3.Date DESC
LIMIT 1) c
ON c.`Name` = o.`Name`)x
set `Contribution` = x.total
where (`table4`.Date) = x.Date and `table4`.`Name` = x.`Name`;
END $$
DELIMITER ;
I would like to use the date from table1 row (that is the table which triggers the trigger) in my left join named c. As it stands c.Rate gives the same value every time because it uses the default table1.
If the row being updated has a date of '2022-01-13' then I want the date used at the line asterisked
and table3.Date <= w.Date
I want w.Date to be '2022-01-13'. But as it stands I can't get that and all the c.Rate give the same value.
Thanks.
The lack of consistent indentation and capitalisation makes your query almost impossible to read. Instead of obfuscating what is going on by using table1, table2, table3 & table4 you would be better off using the real table names, as it will make more sense to anyone trying to read it.
Your current update query makes little sense with the repeated left joins back to the originating table1 but it is hard to be sure given the lack of supporting information in your question. Your first left join to table1, aliased as r, does not get used anywhere. Your second left join to table1, aliased as w, is then referenced in the where clause which turns it into an inner join.
I suggest you update your question with the CREATE TABLE statements and some sample data to show the values before and after executing your update and the trigger update. Your current update query is definitely not the most efficient way of achieving your goal.
I don't really understand your question but it seems that you all are asking is how to use the value from the table1 row being updated? In which case the answer is simply -
and w.Date = NEW.Date
where NEW references the post-update version of the table1 row.
From 25.3.1 Trigger Syntax and Examples -
Within the trigger body, the OLD and NEW keywords enable you to access
columns in the rows affected by a trigger. OLD and NEW are MySQL
extensions to triggers; they are not case-sensitive.
In an INSERT trigger, only NEW.col_name can be used; there is no old
row. In a DELETE trigger, only OLD.col_name can be used; there is no
new row. In an UPDATE trigger, you can use OLD.col_name to refer to
the columns of a row before it is updated and NEW.col_name to refer to
the columns of the row after it is updated.

Execution of UPDATE based on condition

Is there a way in MySQL to insert a condition based on which the entire UPDATE query will be executed? I know that you can use IF or CASE within the query itself to insert different values, but I'm talking about this scenario:
IF ( condition is true ) UPDATE ...
Let's say I wanted to validate data and execute the UPDATE based on result (I know it's a bad idea and data validation should be done scripting wise, I'm just reviewing the theoretical possibilities). Like here below where I test a value against regexp to check if it's numerical value:
UPDATE executed:
IF ( "12345" REGEXP "[0-9]+" ) UPDATE table SET numdata = "12345" WHERE...;
UPDATE not executed:
IF ( "a1234" REGEXP "[0-9]+" ) UPDATE table SET numdata = "a1234" WHERE...;
Thanks,
Prez
Just put the REGEXP in your WHERE clause:
DECLARE mynumdata varchar(10) = "a1234";
UPDATE table SET numdata = mynumdata
WHERE #numdata REGEXP "[0-9]+"
AND <other update conditions>;
One way to do this would be to add the condition to the WHERE clause.
So if you originally had
UPDATE table SET numdata = "a1234" WHERE id=1
you could write this as
UPDATE table SET numdata = "a1234" WHERE id=1 AND "1234" REGEXP "[0-9]+"

SQL - update table with sequential numbering

i have a table in my MySQL database which i have added a new column to.
I would like to update this column on every row with a number starting at 20000 going up +1 each time.
i have tried this solution:
UPDATE table1 set new_col = new_col + 1;
but it just updates all rows with the same number
The easy way:
UPDATE table1 t, (SELECT #nr:= 20000-1) tmp
SET t.new_col = (#nr:=#nr+1) ;
I have used this query to solve this:
SET #rank:=20000;
update customer
set accountnumber_new=#rank:=#rank+1

MySQL trigger alter second table on a specific row

I created a trigger like:
CREATE TRIGGER `update_tb2` AFTER UPDATE ON `table_1` FOR EACH ROW
IF new.parameter = 'param1'
THEN UPDATE table_2 SET table_2.input = '10' WHERE table_2.key = '0';
END IF ;
It works as expected, when I update any row in the table_1 the one with key=0 in the other table is updated.
Since there is a field key in both tables, I want to update in the second table to row who has the same key of the updated row in table_1. I can't figure out how to use table_2.key inside the IF block.
You can use the new or old keyword (in case you change the key value, you may want to use one or the other). It let's you access all the values from the record that was updated on table_1:
CREATE TRIGGER `update_tb2` AFTER UPDATE ON `table_1` FOR EACH ROW
IF new.parameter = 'param1'
THEN UPDATE table_2 SET table_2.input = '10' WHERE table_2.key = new.key;
END IF ;
You can see an example on this fiddle
try this:
CREATE TRIGGER `update_tb2` AFTER UPDATE ON `table_1` FOR EACH ROW
IF new.parameter = 'param1'
THEN UPDATE table_2 SET table_2.input = '10' WHERE table_2.key = old.key;
END IF ;

Insert the same fixed value into multiple rows

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;