數據庫原理學習記錄2
問題1:You can’t specify target table ‘student’ for update in FROM clause
通過百度翻譯:
參考博客:
https://blog.csdn.net/qq_29672495/article/details/72668008
該文提出**“將SELECT出的結果再通過中間表SELECT一遍”**
模仿嘗試:
例1:
delete from student
where 姓名=any(
select student.姓名
from (select student.姓名
from student left join score
on student.學號=score.學號
where score.課號 is null) student
);
Query OK, 8 rows affected (0.01 sec)
例2:
mysql> delete from teacher
-> where 工資<(
-> select avg(工資)
-> from teacher
-> );
ERROR 1093 (HY000): You can't specify target table 'teacher' for update in FROM clause
mysql> delete from teacher
-> where 工資<any(
-> select avg(工資)
-> from teacher
-> );
ERROR 1093 (HY000): You can't specify target table 'teacher' for update in FROM clause
mysql> delete from teacher
-> where 工資<(
-> select avg(工資)
-> from (select avg(工資) from teacher)teacher
-> );
Query OK, 0 rows affected (0.00 sec)
問題2:Subquery returns more than 1 row
通過百度翻譯:
資料查找:
https://zhidao.baidu.com/question/327417776459413245.html
mysql> delete from student
-> where 姓名=(
-> select student.姓名
-> from (select student.姓名
-> from student left join score
-> on student.學號=score.學號
-> where score.課號 is null) student
-> );
ERROR 1242 (21000): Subquery returns more than 1 row
mysql> delete from student
-> where 姓名=any( //此處添加 any
-> select student.姓名
-> from (select student.姓名
-> from student left join score
-> on student.學號=score.學號
-> where score.課號 is null) student
-> );
Query OK, 8 rows affected (0.01 sec)
問題3:Every derived table must have its own alias
通過百度翻譯:每個派生表都必須有自己的別名
在解決問題1,進行再select時容易出現
解決方法:取別名
問題4:Cannot delete or update a parent row: a foreign key constraint fails
百度翻譯:無法刪除或更新父行:外鍵約束失敗
原因:老師故意挖的坑
解決方法:取消外鍵,迅速刪除,接上外鍵,一氣呵成
參考博客:
https://blog.csdn.net/qq_39403545/article/details/86649026