mysql-error-1093

Handling database integrity

末鹿安然 提交于 2019-12-01 16:37:40
问题 I'm introducing database integrity using innodb constraints in the next version of my application. Everything goes well, but some of my tables have records with deleted references (dead records) and because of them I can't add constraints to the table. I am trying: ALTER TABLE `article` ADD FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON DELETE CASCADE; And I get: #1452 - Cannot add or update a child row: a foreign key constraint fails (`books`.<result 2 when explaining filename '

Handling database integrity

隐身守侯 提交于 2019-12-01 16:29:35
I'm introducing database integrity using innodb constraints in the next version of my application. Everything goes well, but some of my tables have records with deleted references (dead records) and because of them I can't add constraints to the table. I am trying: ALTER TABLE `article` ADD FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON DELETE CASCADE; And I get: #1452 - Cannot add or update a child row: a foreign key constraint fails (`books`.<result 2 when explaining filename '#sql-442_dc'>, CONSTRAINT `#sql-442_dc_ibfk_1` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON

MySQL | You can't specify target table 'a' for update in FROM clause

馋奶兔 提交于 2019-12-01 11:33:48
问题 DELETE FROM table_a WHERE id IN( SELECT table_a.id AS id FROM table_a, table_b WHERE table_a.object_id = 1 AND table_a.code = 'code' AND table_a.code = table_b.code AND table_b.id = table_a.b_id AND table_b.table = 'testTable') This is a (somewhat simplified) query I want MySQL to execute. I read on other pages of stackoverflow that this wasn't supported and that it's solvable by using JOINS. How could this be 'transcribed' to a query using JOINS? I find it hard to do so, because I've never

MySQL 5.7 error (1093: You can't specify target table ___ for update in FROM clause) - usual solution not working

时光毁灭记忆、已成空白 提交于 2019-12-01 11:08:33
I have a table 'employees' and I'm trying to set some attributes (e.g. salary) to the same value as some other value in the table. My understanding of this error is that it can be avoided with the following workaround, using a temporary table: UPDATE employees SET salary=(SELECT salary FROM (SELECT * FROM employees WHERE employee_id= '123') AS t1) WHERE employee_id='456'; However, I am still getting the same error code ("can't specify target table 'employees' for update in FROM clause") when I try this. Is there some other issue here? The issue is a functional change in mysql 5.7,scroll

MySQL 5.7 error (1093: You can't specify target table ___ for update in FROM clause) - usual solution not working

自古美人都是妖i 提交于 2019-12-01 09:26:07
问题 I have a table 'employees' and I'm trying to set some attributes (e.g. salary) to the same value as some other value in the table. My understanding of this error is that it can be avoided with the following workaround, using a temporary table: UPDATE employees SET salary=(SELECT salary FROM (SELECT * FROM employees WHERE employee_id= '123') AS t1) WHERE employee_id='456'; However, I am still getting the same error code ("can't specify target table 'employees' for update in FROM clause") when

Mysql SELECT inside UPDATE

≯℡__Kan透↙ 提交于 2019-11-30 15:37:41
问题 UPDATE forms SET pos = (SELECT MIN(pos)-1 FROM forms) WHERE id=$id This doesn't work, error message: **You can't specify target table 'form' for update in FROM clause** I hope it's clear: I want to get the minimal element-1 from the same table and assign it to pos 回答1: Consp is right that it's not supported. There's a workaround, however: UPDATE forms SET pos = (SELECT MIN(pos)-1 FROM (SELECT * FROM forms) AS x) WHERE id=$id A version that is probably faster: UPDATE forms SET pos = (SELECT

Mysql SELECT inside UPDATE

元气小坏坏 提交于 2019-11-30 14:42:34
UPDATE forms SET pos = (SELECT MIN(pos)-1 FROM forms) WHERE id=$id This doesn't work, error message: **You can't specify target table 'form' for update in FROM clause** I hope it's clear: I want to get the minimal element-1 from the same table and assign it to pos Consp is right that it's not supported. There's a workaround, however: UPDATE forms SET pos = (SELECT MIN(pos)-1 FROM (SELECT * FROM forms) AS x) WHERE id=$id A version that is probably faster: UPDATE forms SET pos = (SELECT pos-1 FROM (SELECT MIN(pos) AS pos FROM forms) AS x) where id=$id Your problem is stated plainly in the MySQL

How do I lock read/write to MySQL tables so that I can select and then insert without other programs reading/writing to the database?

时间秒杀一切 提交于 2019-11-30 06:35:16
问题 I am running many instances of a webcrawler in parallel. Each crawler selects a domain from a table, inserts that url and a start time into a log table, and then starts crawling the domain. Other parallel crawlers check the log table to see what domains are already being crawled before selecting their own domain to crawl. I need to prevent other crawlers from selecting a domain that has just been selected by another crawler but doesn't have a log entry yet. My best guess at how to do this is

MySQL: You can't specify target table 'tasks' for update in FROM clause

一笑奈何 提交于 2019-11-29 11:12:57
I have got MySQL error "You can't specify target table 'tasks' for update in FROM clause" running the following query: DELETE FROM tasks WHERE tasks.id IN ( SELECT tasks.id FROM tasks JOIN deadlines ON deadlines.id = deadline_id WHERE DATE_ADD(tasks.created_at, INTERVAL deadlines.duration DAY) <= NOW() ) How can I manage this? Thanx! You can wrap it in a subquery like so. The issue is that MySQL can't update rows that it's also querying. This will make MySQL use a temporary table implicitly to store the ids you want to delete. DELETE FROM tasks WHERE tasks.id IN ( SELECT id FROM ( SELECT tasks

How do I lock read/write to MySQL tables so that I can select and then insert without other programs reading/writing to the database?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 21:12:39
I am running many instances of a webcrawler in parallel. Each crawler selects a domain from a table, inserts that url and a start time into a log table, and then starts crawling the domain. Other parallel crawlers check the log table to see what domains are already being crawled before selecting their own domain to crawl. I need to prevent other crawlers from selecting a domain that has just been selected by another crawler but doesn't have a log entry yet. My best guess at how to do this is to lock the database from all other read/writes while one crawler selects a domain and inserts a row in