还是接着上一篇博文,我们继续。。。
本文主要练习更新操作,在mysql中实现。
内容相对上一篇博文较少,这是那三张表:
修改“计算机学院”的“李勇”同学的名字为“ 李咏”
如果仅有一个sno主键那么:
update s set sname='李咏' where sno='20130101' and sdept ='computer';
--必须写成-->
alter table s drop primary key;
alter table s add primary key(sname);
update s set sname='李咏' where sname='李勇' and sdept ='computer';
这样的话,后期诸多的更新操作会很麻烦,所以干脆把所有涉及到更新的字段统统设置成primary key的属性
alter table s drop primary key;
alter table s add primary key(sname,sno,sid,sdept,sage);
将选修了
10001
课程的学生的分数设置为
60
分
update sc set grade=60 where cno='1001';
将选修了“大学英语”课程的学生的分数增加
5
分
update sc set grade=grade+5 where sc.cno=(select cno from c where c.cname='大学英语');
将“计算机学院”的学生的“高等数学”课程的成绩设置为
NULL
Update sc set grade=null where cno=(select cno from c where cname='高数')
and sno in (select sno from s where sdept='computer');
/* cno和sno必有一个是=,不能同时是in */
删除计算机学院年龄大于
25
岁的男生
Delete from s where sdept='computer' and sage>25 and ssex='男';