您的位置:首页 > 数据库

sql 练习系列:数据的更新操作

2015-11-23 11:48 519 查看
还是接着上一篇博文,我们继续。。。

本文主要练习更新操作,在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='男';
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: