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

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.

Related

Insertig datas, if not exists

I have SQL code, how should I change it to insert datas only if they aren't exist in my table. thanks for your answers:)
INSERT INTO stages_done(id_booked_proj, id_stage)
SELECT booked_proj.id, stages.id_st
FROM booked_proj, stages
WHERE booked_proj.name='home' AND stages.name_st= 'sm'"
You can use INSERT IGNORE, this way the row won't be inserted if it results in a duplicate key:
INSERT IGNORE INTO stages_done(id_booked_proj, id_stage)
SELECT booked_proj.id, stages.id_st
FROM booked_proj, stages
WHERE booked_proj.name='home' AND stages.name_st= 'sm'"
Any duplicate key in columns either with PRIMARY KEY or UNIQUE constraints will be ignored.
If you have writing access, you can add a constraint to your table, like this:
ALTER TABLE `stages_done` ADD UNIQUE `id_booked_proj_id_stage_index` (`id_booked_proj`, `id_stage`);
Where id_booked_proj_id_stage_index can be any name you pick as long as it is unique.

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 ;

How do I INSERT a new row or UPDATE an existing row effeciently?

So I have a 'recently_viewed' table with columns
product|user|time
However, if a user views a product they have already viewed, it will create a new row. What is the most efficient way of covering this possibility (while keeping the possibility that it is the first time they viewed it)? overwriting the old TIME they viewed it or deleting the old table row (and simultaneously creating the new row)? I can think of some inefficient ways to do it (querying first to see if it's in the table, but this requires multiple statements), but I suspect there is something far more efficient.
INSERT INTO recently_viewed ...
Sincere thanks for any help. It is greatly appreciated from an amateur.
You can check whether record for user exists in recently_viewed,
IF EXISTS (SELECT * FROM recently_viewed WHERE user = "user_id")
BEGIN
#UPDATE query
END
ELSE
BEGIN
#INSERT query
END
You can use define (product, user) as UNIQUE, for example you can set it as the PRIMARY KEY of your table:
CREATE TABLE tablename (
product INT,
user INT,
time DATETIME,
PRIMARY KEY (product, user)
);
(or you can also create a UNIQUE index) and then use an INSERT INTO ... ON DUPLICATE query:
INSERT INTO tablename (product, user, `time`)
VALUES (1, 1, '2015-01-01 10:00:00'),
ON DUPLICATE KEY UPDATE `time`=VALUES(`time`);
Please see a working example here.
I would strong suggest using on duplicate key update. This starts with a unique index on user/product:
create unique index idx_recently_viewed_user_product on recently_viewed(user, product);
Then:
insert into recently_viewed(user, product, time)
values ($user, $product, $time)
on duplicate key update time = values(time);

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: INSERT IGNORE or ON DUPLICATE KEY UPDATE with checking multiple and not unique columns

i want to insert rows IF a row containing the specific values exists, and if not update it.
Concretely:
A column user_id=5, user_zip=12345, distance=600 exists on the database.
If i try to insert user_id=5, user_zip=12345, distance=700 it should just update the distance
but i try to insert user_id=5, user_zip=67890, distance=800 it should insert a new row.
I can't define the columns user_zip and distance unique, so that i can use the on duplicate key update.
I think you are misunderstanding how ON DUPLICATE KEY UPDATE works. With a unique constraint on (user_id, user_zip), this should work:
INSERT INTO yourTable (user_id, user_zip, distance) VALUES (5,12345,600)
ON DUPLICATE KEY UPDATE distance=600;
You don't have to define user_zip unique (that's how I understand your question), just the combination of user_id and user_zip. So you still can have 2 or more rows with the same user_id, just the user_zip can't match on those rows.