Number of rows affected after Triggers applied - mysql

I have one mysql table on which i created on-after and on-before trigger for insertion. Each tigger update 2 rows respectively. So once i insert a row to the table, altogether 5 rows are updated, even though the response from the DB will be as "1 row is affected". I need to find a way to know the total no of rows got updated, in this case 5.

The problem seems to be that MySQL does not count along when you insert/update rows in a trigger. The best possible solution might be to count the manually inserted/updated rows for yourself, store the value in a variable, get it after the outer query and add the result of ROW_COUNT() to it.

I think it is giving one row affected because you are adding one row in table. Mysql reply gives information of row added in that table only. And not giving affected rows by triggers.

Related

Laravel Update get updated ids

If I have Laravel eqoquent model (or pure mysql) And i try to update 10 records:
UPDATE users SET active = 1 WHERE is_admin IN (2,3);
How will I know which records are updated.
Is there any function in MySQL that will return the updated ids.
I need them
Thanks
No, there is no function in mysql that would return the ids of the records that got updated. Why? Becsuse you already know which records you want to update: those that correspond to the where criteria of your update query. There is only a function that tells you how many records got affected by your query.
If you want to know exactly which records got updated by your query, then you need to query the fields and ids of the records that could be updated before the update and then compare the values after the update. Where the values got changed, those are the records that got updated.
Alternatively, you can add a timestamp column to the table, which gets modified if a record is updated. After the update you can query which records got updated.

preserving the first value assigned to a field in mysql

I have a MYSQL database with a table like:
Id myId Description
ABD1 0 some desc
ABD2 1 some desc
....
myId is an autoincremented column. I need to create a mysql trigger that will prevent anyone from changing the first myId value assigned to a row at the time of its insertion. How can this be done in mysql? I was thinking:
CREATE TRIGGER upd_check BEFORE UPDATE ON myTable
FOR EACH ROW
BEGIN
NEW.myId = OLD.myID
END
Could this be enough? If so, is this trigger going to run for all rows of my table? only for the new ones? I just need for one row.
Thx
To answer your question directly:
Could this be enough?
Yes, this will make sure any UPDATE will not change the value of your myID column. It will always reset that column to the value it was prior to the UPDATE.
If so, is this trigger going to run for all rows of my table? only for the new ones?
The answer is in the manual page https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html which says:
The statement following FOR EACH ROW defines the trigger body; that is, the statement to execute each time the trigger activates, which occurs once for each row affected by the triggering event.
In other words, the trigger executes once for each row matching the condition in your UPDATE's WHERE clause.
It will not apply to every row in the table—unless your WHERE clause matches every row.

How can I insert multiple rows into Mysql table with duplicate rows ignored while the row id is returned?

I'm using java/Mybatis with MySQL in my project. I need to insert multiple rows into a table and I want to ignore those rows which has a dupliate UNIQUE index. Also I want to get to know which rows are ignored. How to do it? it seems to me that insert ignore into can not tell me which rows are ignored.
I cannot help you out with a solution on how to do that while inserting. But depending on when you need to know the rows that are ignored you could either:
Invert you select so that you get all the duplicates before inserting into the new table.
Deduct the rows in the sink table from the rows in the source table(s) after the insert.

MySQL 5.5 - How to get row count while Table is locked during insert

I am currently inserting about 2 million rows and I need to monitor the progress of row inserts. The table is locked at the beginning of the insert operation. Is there a way I can find out the number of rows added or maybe an indicative number?
I am guessing this procedure but not tested with locks applied.
before insert find row count.
apply lock for insert.
find rows (row count value) from show table status from your_db_name where name='tbl_name'.
find the difference between new row count and from the former one.

how to get number of rows updated and inserted from mysql

I'm doing large number of:
INSERT.... ON DUPLICATE KEY UPDATE
queries and I want to find out the number of rows affected, ideally the number updated and the number inserted.
At the moment I'm using ROW_COUNT() but that counts as 2 from the above sql if the row is updated or 1 if it is inserted.
Is there a way to find this from a mysql function?
With ON DUPLICATE KEY UPDATE, the affected-rows value is 1, if update its 2. From this you can determine how many rows inserted successfully and how many are updated