MySql Trigger, before insert delete a row in the same table - mysql

Is this possible? Before a row is inserted into tableA, I need to delete any rows in tableA containing duplicate data.The trigger below does not generate an error but doesn't work either.
CREATE TRIGGER remove_old_user
BEFORE INSERT ON userStatus
FOR EACH ROW
DELETE FROM userStatus WHERE username = NEW.username
Any ideas?

Is it essential that the previous record be deleted, rather than updated to match the new info? Sounds like INSERT ... ON DUPLICATE KEY UPDATE would meet most of your needs. More info in the MySQL manual.
(I'm assuming based on the question that the field is unique, and can be part of the primary key)

Related

Trigger ID from Table A to Table B

I have two Tables as follows:
Report
Checks
ID column on Table "Report" has AI (Auto-Increment enabled and set to Primary Key)
ID column on Table "Checks" has AI (Auto-Increment Disabled and Indexed)
Here are my Foreign Key Constraints created under "checks" Table:
Foreign Key
What I need to accomplish is:
Whenever I run my Powershell query to insert a new row into table "Report", I need the same ID to get written under the ID column of the "Checks" Table.
Powershell Query:
"INSERT INTO Checks (WiFi, Printers, Notepad) VALUES ('$WiFi', '$Printers', '$NotepadPlus')"
Should I create a trigger in phpmyadmin in this case? and if so could anyone please assist with this?
Do I need to specify the ID in my Powershell command above? if yes how could this be done?
Thank you in advance,
Yes, you can specify the ID in PowerShell command.
I assume you are using Powershell command to insert into Reports too. To capture new ID generated you can use $command.LastInsertedId and it can be used while inserting into checks table.
Hope it helps.
SQL for creating TRIGGER to insert ID to Checks table:
CREATE DEFINER = CURRENT_USER TRIGGER `dbname`.`trigger_name` AFTER INSERT ON `Report` FOR EACH ROW
BEGIN
insert into Checks (id) values (NEW.id)
END
But the values '$WiFi', '$Printers', '$NotepadPlus' cannot be inserted by TRIGGER. You need to set these values by Updating the row related to the new inserted ID in your PHP code.

MySQL - Insert Multiple Records or Update Multiple Records If Exists

I want to INSERT a new record into my database if it is no exists, otherwise it will UPDATE those existing records.
I did search on Stackoverflow, but none of the result can solve my issue.
"INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"
MySQL ON DUPLICATE KEY UPDATE for multiple rows insert in single query
TABLE STRUCTURE
SCENARIO
There are a lot of records inside Purchase Details table. So the only Unique ID is only purchase_id.
I want to update the amount which have the same product_id. Refer to the table.
Below is the SQL Query I have tried so far.
INSERT INTO `purchase_details` (`product_id`, `amount`)
VALUES (1583, 0)
ON DUPLICATE KEY UPDATE amount = 0
The query shows 1 row affected.
But total has 146 rows which mean this query is not working.
PROBLEM
ON DUPLICATE KEY UPDATE only allow to inserted a row that would cause a duplicate value in a UNIQUE index or PRIMARY KEY
If I understand your question correctly, you want to update the other existing rows of your table which have the same product_id as the new one being inserted with the newly provided amount value.
Since product_id is not a unique column in purchase_details table, it will not consider this scenario for ON DUPLICATE KEY UPDATE condition. Ergo, 1 row affected.
To achieve what you want, you need to CREATE TRIGGER.
Like this:
DELIMITER $$
CREATE TRIGGER ins_check
AFTER INSERT ON purchase_details
FOR EACH ROW
BEGIN
UPDATE purchase_details
SET amount = NEW.amount
WHERE product_id = NEW.product_id;
END;$$
DELIMITER ;

MySql Trigger INSERT... ON DUPLICATE KEY UPDATE not recognising columns

I have two tables in a Mysql database: "stock_pricing" and "DATA_IMPORT"
columns in first table: STOCK_ID, DATE, LAST_CLOSE_DOM_CURR
columns in second table: STOCK_ID, DATE, ADJ_CLOSE
The first table has an index on stock_id and date together. These have been defined as UNIQUE.
The second table has no index at all.
The second table has incoming data. On this table there is a BEFORE INSERT trigger that inserts incoming data into the first table.
If upon the insert trigger the combination of STOCK_ID and DATE violates the UNIQUE index of the first table, the ON DUPLICATE KEY UPDATE part of the trigger is fired.
I have tried every combination I can think of, but the trigger does not recognise my column names, any thoughts? Many thanks.
BEGIN
INSERT INTO stock_pricing (`STOCK_ID`, `DATE`, `LAST_CLOSE_DOM_CURR`)
VALUES (DATA_IMPORT.STOCK_ID, DATA_IMPORT.DATE, DATA_IMPORT.ADJ_CLOSE)
ON DUPLICATE KEY UPDATE
stock_pricing.STOCK_ID= DATA_IMPORT.STOCK_ID, stock_pricing.DATE= DATA_IMPORT.DATE, stock_pricing.LAST_CLOSE_DOM_CURR= DATA_IMPORT.ADJ_CLOSE;
END
You are referencing a table called data_import with no from clause. This is fixed using insert . . . select:
INSERT INTO stock_pricing (`STOCK_ID`, `DATE`, `LAST_CLOSE_DOM_CURR`)
SELECT DATA_IMPORT.STOCK_ID, DATA_IMPORT.DATE, DATA_IMPORT.ADJ_CLOSE
FROM DATA_IMPORT
ON DUPLICATE KEY UPDATE
stock_pricing.STOCK_ID= DATA_IMPORT.STOCK_ID, stock_pricing.DATE= DATA_IMPORT.DATE, stock_pricing.LAST_CLOSE_DOM_CURR= DATA_IMPORT.ADJ_CLOSE;
A more typical way of writing such a query is:
INSERT INTO stock_pricing (`STOCK_ID`, `DATE`, `LAST_CLOSE_DOM_CURR`)
SELECT di.STOCK_ID, di.DATE, di.ADJ_CLOSE
FROM DATA_IMPORT di
ON DUPLICATE KEY UPDATE STOCK_ID = VALUES(STOCK_ID),
DATE = VALUES(DATE),
LAST_CLOSE_DOM_CURR = VALUES(LAST_CLOSE_DOM_CURR);
For ON DUPLICATE KEY UPDATE to work, you need a unique index or primary key. I assume you have these.
Finally, this code looks a bit strange for a trigger, because there are no references to NEW or OLD. If you are still having trouble with the trigger, then ask another question and include the full code for the trigger.

PHP/Mysql Calendar

Sorry for my poor english.
I would like to use the "on duplicate key update" but I do not know how to.
My MySQL database is alike :
id (primary key | autoincrement), id_hostel, date, allotement
My MySQL query :
insert into table (id_hostel, datebvj, allotement) VALUES
('1','09/05/2012','7'), ('1','10/05/2012','5'),
('1','11/05/2012','6')
ON DUPLICATE KEY UPDATE allotement=VALUES(allotement)
allotement means rooms
The problem : This query makes an insert query even if there is already a data in the database.
I would like the query to run good.
Any suggestion ?
Thank you very much.
An 'on duplicate' will only convert into an update if the insert would result in a duplicate record being created, where duplicate means a unique/primary key index would be violated.
Given your table structure, you'd have to insert a duplicate id field to trigger the conversion. None of your other fields have unique keys, and your insert statement is not inserting an id value, so there is no way to trigger the insert->update switch.

MySQL on duplicate key update without updating the recorded row

I read about the MySQL command ON DUPLICATE KEY UPDATE. I have a column Surnames in a Users table. Since there must be no identical surnames, I want to INSERT a new surname when the surname isn't in the database, and leave the row as it was recorded if the surname was previously saved in the database, without updating it. How can I achieve this?
INSERT IGNORE ... will try to insert a new row, if a duplicate key is found the new data will be discarded.
Documentation of INSERT