I'm trying to do the same thing as described in MySQL: Insert record if not exists in table, but I'm running it on command line so I need to lock tables manually. This is what I have:
LOCK TABLES `mytable` WRITE;
INSERT INTO `mytable` (`mycolumn`)
SELECT * FROM (SELECT 'test') AS temp
WHERE NOT EXISTS (
SELECT `mycolumn`
FROM `mytable`
WHERE `mycolumn` = 'test'
) LIMIT 1;
UNLOCK TABLES;
However, when I run this I get the error:
ERROR 1100 (HY000) at line 239: Table 'mytable' was not locked with LOCK TABLES
The command executes fine without lock/unlock in MySQL Workbench. My guess is that I need to lock the "temp" table, but how do I do that??
LOCK TABLES `mytable` WRITE, <something> AS temp WRITE;
Since "temp" is not an actual table in the database, it's just (SELECT 'test').
I can add that the reason why I don't (just) want to create a unique index is that this results in an error that I don't know how to handle since the exit code for the command line process is always just 1 in case of an error and I really don't want to parse stderr for it...