I am running a query directly in MySQL Workbench. It is a work in progress, but I can't get past this issue.
The query is as follows:
#Temporary table to hold dcsIds that are past grace period
CREATE TABLE pastGracePeriod (
dcsId varchar(50) NOT NULL
);
INSERT INTO pastGracePeriod
SELECT dcsId FROM loyaltyOptOutGracePeriod
WHERE optOutDate <= ADDDATE(CURRENT_TIMESTAMP(), INTERVAL -15 DAY);
#temporary table to hold dcsIds that are validated against subscriptions table
CREATE TABLE validatedDcsIds (
dcsId varchar(50) NOT NULL
);
INSERT INTO validatedDcsIds
SELECT subscriptions.dcsId
FROM subscriptions
INNER JOIN pastGracePeriod ON subscriptions.dcsId = pastGracePeriod.dcsId
WHERE subscriptions.optOutDate <= ADDDATE(CURRENT_TIMESTAMP(), INTERVAL -15 DAY)
AND subscriptions.subscriptionId IN (24,25,30)
AND subscriptions.optInStatus='N';
#delete items in validatedDcsIds table from externalId table
DELETE FROM externalId
WHERE EXISTS (SELECT dcsId FROM validatedDcsIds
WHERE externalId.dcsId = validatedDcsIds.dcsId);
DROP TABLE pastGracePeriod;
DROP TABLE validatedDcsIds;
The query takes dcsIds
(PK) from a table called loyaltyOptOutGracePeriod
, validates them against another table called subscriptions
, then takes those validated dcsIds
and deletes them from a table called externalId
.
The query works fine until it gets to the DELETE
clause. In the externalId
table, the PK is a composite key consisting of fields dcsId
, appId
, and appName
. As it is written above, I get Error 1175: You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
.
If I change it to:
DELETE FROM externalId
WHERE EXISTS (SELECT dcsId FROM validatedDcsIds
WHERE externalId_backup.dcsId = validatedDcsIds.dcsId)
AND appId <> NULL;
I get no errors, but nothing gets deleted from the externalId
table as expected.
I am at a loss, any suggestions are very welcome!
NOTE: I do not need to know how to disable safe update mode.