MySQL代码运行返回异常的情况

暂时还不知道原因,做个笔记,找到原因之后在每个问题后补充原因

1.不明白为什么第一种情况的代码没有返回而第二个有

select cop.student.*, cop.sc.* from cop.student, cop.sc where student.sno=sc.sno;
	/*查询每个学生及其选修课程的情况*/   
	/*等值连接'='*/ 
select student.sno, sname, sex, sage, sdept, cno, grade from cop.student, cop.sc; #where student.sno=sc.sno;
	/*查询每个学生及其选修课程的情况*/ 
	 /*等值连接——自然连接*/

原因:更新student表的sno的时候没有更新sc表的sno
2. 没有返回

select first.cno, second.cpno from cop.course first, cop.course second where first.cpno=second.cno;
	/*查询每一门课的间接先修课*/

原因:course表的cpno列和teacher列的数据写反了
3.只返回了没有选修的课程的学生的情况

select student.sno, sname, sex, sage, sdept, cno, grade from cop.student LEFT OUTer join cop.sc on (student.sno=sc.sno);
	/*查询每个学生及其选修课程的情况并且把没有选修的也列出来*/
	/*外连接*/

原因:更新student表的sno的时候没有更新sc表的sno
4.没有返回

select student.sno, sname, cname, grade from cop.student, cop.sc, cop.course where student.sno=sc.sno and sc.cno=course.cno;
	/*查询每个学生的学号、姓名、选修的课程名及成绩*/
select sname /*外层查询/父查询*/ from cop.student where sno in (select sno /*内层查询/子查询*/  from cop.sc where cno='2');
	/*子查询的限制:不能使用order by子句*/
select sname from cop.student, cop.sc where student.sno=sc.sno and sc.cno='2';

原因:更新student表的sno的时候没有更新sc表的sno
5.Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.

update cop.sc set grade=0 where 'CS'=(select sdept from cop.student where student.sno=sc.sno);
	/*将计算机科学系全体学生的成绩置零*/

百度看到有人说:
where后跟的条件不是主键id,就会出现这种错误
解决方式有两种:
1、SET SQL_SAFE_UPDATES = 0;执行该命令更改mysql数据库模式。
2、在where判断条件中跟上主键id 例如:delete from firstmysqldatabase.user where UserName=‘zhangsan’ and ID>=0;
运行这两种解决方式的代码之后再运行代码块的代码之后都发现sc表的grade列数据都被清零了。。。
不行就再百度呗,又看到一个方法,好的照做:在“workbench”左边栏点击“SQL Editor”,看到最后一行是“Safe Updates……”是打钩的,取消打钩,缺消打钩之后,点击“OK”按钮,重启workbench,然而重新运行发现又是全都清零。。。
我现在怀疑是代码本来就是让它全部清零,然而我看不出来,我辣鸡。。。

你可能感兴趣的:(MySQL)