在MySQL中,删除关联的多张表,可以使用下面SQL
mysql> delete e, d from emp e inner join dept d on e.deptno=d.deptno where e.deptno=10;
Query OK, 4 rows affected (0.01 sec)
delete后面的e, d是表的缩写名
执行成功后,两张表中的对应数据会被删除
mysql> select * from dept;
+--------+------------+---------+
| deptno | dname | loc |
+--------+------------+---------+
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
| 50 | Research | BeiJing |
+--------+------------+---------+
4 rows in set (0.00 sec)
mysql> select * from emp where deptno=10;
Empty set (0.00 sec)
mysql> delete e, d from emp e inner join dept d on e.deptno=d.deptno where e.deptno=10;
Query OK, 4 rows affected (0.01 sec)
delete后面的e, d是表的缩写名
执行成功后,两张表中的对应数据会被删除
mysql> select * from dept;
+--------+------------+---------+
| deptno | dname | loc |
+--------+------------+---------+
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
| 50 | Research | BeiJing |
+--------+------------+---------+
4 rows in set (0.00 sec)
mysql> select * from emp where deptno=10;
Empty set (0.00 sec)
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26506993/viewspace-2128714/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/26506993/viewspace-2128714/