How update table with duplicate primary key? - mysql

I trying to update a table with another table of other database, I the last updated I created a function to save more than one category in one product, so, I when a run the old script to update my table, a constraint error appears. I understand the situation and why that is happenning, but how I allow the update with the table with duplicates data? Is there a way to disable the constraint?
My query
UPDATE novourbano.oc_product_to_category oc
INNER JOIN erp_product_category erp ON oc.product_id = erp.erp_productid
SET oc.product_id = erp.erp_productid,
oc.category_id = erp.erp_categoryid
WHERE oc.product_id <> 0
I try to use that:
SET GLOBAL FOREIGN_KEY_CHECKS=0;
But still not working. Any suggestion? Thank's in advance!

If the table name reflects the table purpose the primary key should be
PRIMARY KEY(product_id, category_id)
in order to avoid duplicates like several rows with the same product_id and category_id.
You can use IGNORE for this update:
UPDATE IGNORE novourbano.oc_product_to_category oc
INNER JOIN erp_product_category erp ON oc.product_id = erp.erp_productid
SET oc.product_id = erp.erp_productid,
oc.category_id = erp.erp_categoryid

Related

Conditional UPDATE MariaDB (MySQL)

Code:
UPDATE COMPANY SET id='21'
WHERE id='20';
Error:
SQLException: Duplicate entry '21' for key 'PRIMARY'
I want to UPDATE the primary key field in this case it's called 'id' to another value but if the value exists already it throws the error above. How would I do a conditional UPDATE based on if the 'id' doesn't exist in the COMPANY table already, to avoid throwing that error using MariaDB syntax.
NOTE:
I am NOT talking about doing a conditional INSERT that uses "ON DUPLICATE KEY UPDATE" as shown below.
INSERT INTO COMPANY(id,first,last,age)
VALUES('1','Tim','Jones','70')
ON DUPLICATE KEY UPDATE id='1';
You can use UPDATE IGNORE:
UPDATE IGNORE COMPANY
SET id='21'
WHERE id = '20'
See a simplified demo.
You can count the number of values already in the table:
UPDATE COMPANY C CROSS JOIN
(SELECT COUNT(*) as cnt
FROM COMPANY
WHERE id = 21
) CC
SET c.id = 21
WHERE id = 20 AND cnt = 0;
Note: In most databases, you would use NOT EXISTS in the WHERE clause, but MySQL/MariaDB doesn't support references to the table being updated.

Is ON DUPLICATE KEY functionality possible when doing an UPDATE query?

I'm familiar with using the ON DUPLICATE KEY construct when doing INSERT queries. I'd like to do the same with an UPDATE query, but it seems to be not allowed in MySQL, or else I just don't know the syntax.
My table has a unique index, and I want my UPDATE query to update as many records as it can, and ignore or delete records that would cause a duplicate key. The hypothetical query would look something like this:
UPDATE votes SET user = 'foo' WHERE user = 'bar'
ON DUPLICATE KEY <ignore update or delete row>;
I'm trying to merge 2 users in my database. Where that user already has an existing vote on a topic, the duplicate vote can be ignored, but I don't want the whole query to fail when this happens. Ideally I'd like to delete any records that trigger the duplicate key, and if that can be rolled into the query, so much the better.
This would be a fairly rare event, so if this functionality just plain doesn't exist with UPDATE, I'm open to answers that accomplish what I want with multiple queries.
You can check for this with a self-join:
UPDATE votes AS v1
LEFT JOIN votes AS v2 ON v1.topic = v2.topic AND v2.user = 'foo'
SET v1.user = 'foo'
WHERE v1.user = 'bar'
AND v2.user IS NULL
DEMO
It's not possible to combine UPDATE and DELETE in a single query. You need to do them separately, using a transaction to ensure atomicity. The DELETE query should only delete the records that would be duplicated based on the topic.
START TRANSACTION;
DELETE v1 FROM votes AS v1
JOIN votes AS v2 ON v1.topic = v2.topic
WHERE v1.user = 'foo' AND v2.user = 'bar';
UPDATE votes SET user = 'foo' WHERE user = 'bar';
COMMIT;

SQL: Move column data to other table in same relation

I'm wondering if it's possible to move all data from one column in table to another table.
Here's what i'm trying to do:
Table 1 : users - columns trying to read+move = oauth_access_key and oauth_access_secret
Table 2 : account_users - target columns: oauth_token_key, oauth_token_secret
The relation key between these tables is "user_id".
Is this possible in one query? I know this is easily done in PHP, but i'm wondering if this can be done in plain SQL.
Thanks in advance.
UPDATE users, account_users
SET account_users.oauth_token_key=users.oauth_access_key,
account_users.oauth_token_secret = users.oauth_access_secret
WHERE account_users.user_id=users.user_id;
You can use JOIN syntax on MySQL Update.
I think the answer you are looking for is
INSERT INTO `account_users` (`user_id`, `oauth_token_key`, `oauth_token_secret`)
SELECT `user_id`, `oauth_access_key`, `oauth_access_secret` FROM `user`
ON DUPLICATE KEY UPDATE
`oauth_token_key` = VALUES(`oauth_token_key`),
`oauth_token_secret` = VALUES(`oauth_token_secret`);
EDIT
I am assuming you want to move data as in 'put it somewhere it hasn't been yet'.
EDIT2
Here is a documentation on VALUES(): http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_values
Since the title is SQL, and not DB specific... here's a solution for those who stumble upon this while searching for Postgres:
UPDATE account_users
SET oauth_token_key = users.oauth_access_key,
oauth_token_secret = users.oauth_access_secret
FROM profiles
WHERE account_users.user_id = users.user_id;
INSERT INTO account_users (user_id, oauth_token_secret)
SELECT users.user_id, users.oauth_access_secret FROM users
ON DUPLICATE KEY UPDATE account_users.oauth_token_secret = users.oauth_access_secret

MySql stored procedures, delete record logically or phisically depending on existing table references

I have to write a Stored Procedure to delete record from a table.
I have a memory table "tableids" where I store all the ids to delete from another table, say "addresses".
CREATE TABLE `tempids` (
`id` INT(11) NULL DEFAULT NULL
)
COLLATE='latin1_swedish_ci'
ENGINE=MEMORY
ROW_FORMAT=DEFAULT
I could do this:
DELETE FROM addresses INNER JOIN tempids ON addresses.id = tempids.id;
BUT I want to physically delete the records in the addresses table if they have no references in other known tables in my model; otherwise I want to delete the records logically. I'd like to do this in a single shot, that is without writing a loop in my SP.
In pseudo-sql code:
DELETE
FROM addresses
WHERE
id NOT IN (SELECT address_id FROM othertable1 WHERE address_id=(SELECT id FROM tempids))
AND id NOT IN (SELECT address_id FROM othertable2 WHERE address_id=(SELECT id FROM tempids))
...more possible references in other tables
IF "no records deleted"
DELETE FROM addresses INNER JOIN tempids ON addresses.id = tempids.id;
ELSE
UPDATE addresses SET deleted=TRUE INNER JOIN tempids ON addresses.id = tempids.id;
How do I accomplish this?
Thanks.
You can check ROW_COUNT() function value after deleting.
http://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_row-count
So "no records deleted" condition is replaced with ROW_COUNT() == 0

SSIS - Update flag of selected rows from more than one table

I have a SSIS package that copies data from table A to table B and sets a flag in table A so that the same data is not copied subsequently. This works great by using the following as the SQL command text on the ADO Net Source object:
update transfer
set ProcessDateTimeStamp = GetDate(), LastUpdatedBy = 'legacy processed'
output inserted.*
where LastUpdatedBy = 'legacy'
and ProcessDateTimeStamp is not null
The problem I have is that I need to run a similar data copy but from two sources table, joined on a primary / foreign key - select from table A join table B update flag in table A.
I don't think I can use the technique above because I don't know where I'd put the join!
Is there another way around this problem?
Thanks
Rob.
You can use a join in an update statement.
update m
set ProcessDateTimeStamp = GetDate(),
LastUpdatedBy = 'legacy processed',
somefield = t.someotherfield
output inserted.*
from transfer t
join mytable m
on t.id = m.id
where m.LastUpdatedBy = 'legacy'
and m.ProcessDateTimeStamp is null
and t.ProcessDateTimeStamp is not null
The key is to not alias the fields on the left side of the set but to alias everything else. And use the table alias for the table you are updating after the update key word so it knows which table of the join to update.