MySql Insert multiple and Ignore duplicate records - mysql

I have this MySQL INSERT query that adds a Product to multiple categories:
INSERT INTO _categories_products (product_id, category_id) VALUES (1, 14), (1, 8), (1, 1), (1, 22);
This works great, however, if I add the same product to another subcategory from the same parent, it will create duplicate records for the parent categories:
INSERT INTO _categories_products (product_id, category_id) VALUES (1, 14), (1, 8), (1, 1), (1, 23);
Question: What would be the appropriate MySQL query that Ignores the insertion of duplicate records? In other words, the second query should INSERT only one record: 1, 23.
I tried also INSERT IGNORE INTO but nothing changed.
Thank You!

To start with, you want to create a unique constraint on categories/product tuples to avoid duplicates:
alter table _categories_products
add constraint _categories_products _bk
unique (product_id, category_id);
From that point on, an attempt to insert duplicates in the table would, by default, raise an error. You can trap and manage that error with MySQL on duplicate key syntax:
insert into _categories_products (product_id, category_id)
values (1, 14), (1, 8), (1, 1), (1, 23)
on duplicate key update product_id = values(product_id)
In the case of duplicate, the above query performs a dumb update on product_id, which actually turns the insert to a no-op.

Related

INSERT INTO... VALUES with inexisting values [duplicate]

This question already has answers here:
MySQL - ignore insert error: duplicate entry
(5 answers)
Closed 4 years ago.
I have a question about insert data in a table.
My table is just composed of a primary key (that is composed by two foreign key).
The problem is: I don't know how to execute my script to insert all rows except non-existing values. Because in the script, some of the inserts are not available anymore.
For some reason, someone gave me the insert script but with some foreign key that were delete. So when I executed the script, I get an error "Constraint fail..." and it's normal.
But how to insert rows easily to avoid error due to invalid foreign key?
INSERT INTO `X_Y` (`x_id`, `y_id`) VALUES
(4, 1),
(4, 2),
(4, 3),
(4, 4),
(4, 5),
(5, 6),
(5, 7),
(5, 8),
(6, 9),
(6, 10),
(7, 11),
(8, 12),
(8, 13),
(11, 18),
(12, 19),
... ( about 2000 insert)
(1680, 2071);
For example : insert (4,1) can't be executed because foreign key x_id 4 does not exist anymore.
Try first inserting your data into a temporary table, with no constraints:
INSERT INTO temp (x_id, y_id)
VALUES
(4, 1),
(4, 2),
...;
Then, use the following query to insert into your target table:
INSERT INTO X_Y (x_id, y_id)
SELECT x_id, y_id
FROM temp t
WHERE
EXISTS (SELECT 1 FROM X x WHERE x.id = t.x_id) AND
EXISTS (SELECT 1 FROM Y y WHERE y.id = t.y_id);
The above insert checks, for every row, that the specified x_id and y_id values in fact have matching primary keys in the two parent tables.

Performance issue of ON DUPLICATE KEY UPDATE in mysql bulk Update

As Mysql doesn't Provide any Bulk Update query but we use the feature of ON DUPLICATE KEY UPDATE. Is it good to use the below query when we are updating in bulk if not then what are the performance issues of using the below query? Is there is any other way to bulk update in MySQL
INSERT into fruits(id, value) VALUES
(1, 'apple'), (2, 'orange'), (3, 'peach'),
(4, 'apple'), (5, 'orange'), (6, 'peach'),
(7, 'apple'), (8, 'orange'), (9, 'peach'), (10, 'apple')
ON DUPLICATE KEY UPDATE value = VALUES(value);
Clever trick. Let us know if it is faster than a 10 UPDATE statements. I suspect it is -- 9 fewer round trips to server; 9 fewer calls to parser; etc.
There is REPLACE, but that is very likely to be less efficient, since it is
DELETE all rows that match any UNIQUE index; and
INSERT the row(s) given.
IODKU is effectively
if row exists (based on any UNIQUE key)
then do "update"
else do "insert"
The effort to check if the row exists pulls the necessary blocks into cache, thereby priming things for the update or insert.

Delete old record and insert new ones in one query mysql

Is there a way to delete old records from table and to insert new ones in one query with mysql?
For example to make this 2 querys into one.
DELETE FROM A where customer_id = 5;
INSERT INTO A (product_id, customer_id) VALUES (1, 5);
INSERT INTO A (product_id, customer_id) VALUES (5, 5);
INSERT INTO A (product_id, customer_id) VALUES (12, 5);
INSERT INTO A (product_id, customer_id) VALUES (543, 5);
Delete and insert are not meant to be in one single query. However, you can merge your inserts into a single query. This would be the script:
DELETE FROM A where customer_id = 5;
INSERT INTO A (product_id, customer_id) VALUES (1, 5), (5, 5), (12, 5), (543, 5);

How long should inserting 150K rows take in mysql?

So I am inserting dummy data into my application.
I have insert statements that look like this:
INSERT INTO `submission_tagged` (1, 4);
INSERT INTO `submission_tagged` (1, 6);
INSERT INTO `submission_tagged` (1, 11);
INSERT INTO `submission_tagged` (2, 6);
INSERT INTO `submission_tagged` (2, 15);
INSERT INTO `submission_tagged` (2, 19);
150,000 of them to be precise; The insertion seems to be taking it's time; but they are obviously rather simple inserts, so I am wondering How long I should expect this to take if it will take a while I will cancel the insert and change the dummy data script to generate bulk insert statements...
Local server; so other traffic.
You can insert multiple rows at once, like:
INSERT INTO `submission_tagged` (1, 4), (1, 6)...
But check out docs for you RDBMS how many records at once it can handle. Seems that 1000 will work. That'l be much faster than inserting single row per query
Try doing them as a single INSERT:
INSERT INTO `submission_tagged` VALUES (1, 4), (1, 6), (1, 11), ...

MySQL: Conditional INSERT ... SELECT for multiple rows with constant values

I know how to insert multiple rows with constant values in a single query:
INSERT INTO table
VALUES
(1, 'a', 'x'),
(2, 'b', 'y'),
(3, 'c', 'z');
But how do I do this conditionally? I want to make sure only non-existing* rows get inserted. Is it even possible to do it in a single query?
*A row exists when all its columns are identical to the one we are inserting
Use INSERT IGNORE INTO instead of INSERT INTO. The former will only insert rows, if no unique key constraints are violated.
INSERT IGNORE INTO table
VALUES
(1, 'a', 'x'),
(2, 'b', 'y'),
(3, 'c', 'z');