简体   繁体   中英

mySQL Unexpected token on delete

I have problem with multi delete query in PHP myadmin(storage engine InnoDB). This is my database: 数据库架构

This is my query:

DELETE placement, employees
FROM placement, employees
WHERE placement.id_emp = employees.id_emp
AND employees.id_emp = 113;

Errors:

3 errors were found during analysis.

Unexpected token. (near "placement" at position 7) Unexpected token. (near "," at position 16) Unexpected token. (near "employees" at position 18)

1451 - Cannot delete or update a parent row: a foreign key constraint fails ( employment . placement , CONSTRAINT placement_ibfk_1 FOREIGN KEY ( id_emp ) REFERENCES employees ( id_emp ))

I wrote that query according to this tutorial: http://www.mysqltutorial.org/mysql-delete-statement.aspx

Could anyone help me?

如果您使用"DELETE FROM placement, employees USING placement, employees WHERE ..."怎么办?

Even it is old post, but may be someone will need help in future also, so updating here-

There seems 2 issues.

First foreign key issue: to take care this issue you need to either first delete records from child table then you can delete from master table. But if you want to bypass it (which is not recommended) you can do as per below:

SET FOREIGN_KEY_CHECKS = 0;
<YOUR DELETE STATEMENT>; 
SET FOREIGN_KEY_CHECKS = 1;

Note: it will skip foreign key check.

Second issue seems with SQL syntax, even I did not verify it. So you can write code as per below:

If you want to delete data from single table like from employees then use it:

DELETE employees.*
FROM placement, employees
WHERE placement.id_emp = employees.id_emp
  AND employees.id_emp = 113;

If you want to delete data from both tables like from employees as well placement then use it:

DELETE placement.*, employees.*
FROM placement, employees
WHERE placement.id_emp = employees.id_emp
  AND employees.id_emp = 113;

Put; at the end of the line before this code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM