为3000元。
?update emp set salary=3000 where name='zs';
?将姓名为’ls’的员工薪水修改为4000元,job改为ccc。
?update emp set salary=4000,job='ccc' where name='zs';
?将wu的薪水在原有基础上增加1000元。
?update emp set salar=salary+4000 where name='wu';
?
?3.删除表操作
delete from tab_name [where ....]
*如果不跟where语句则删除整张表中的数据
*delete只能用来删除一行记录,不能值删除一行记录中的某一列值(这是update操作)。
*delete语句只能删除表中的内容,不能删除表本身,想要删除表,用drop
*同insert和update一样,从一个表中删除记录将引起其它表的参照完整性问题,在修改
数据库数据时,头脑中应该始终不要忘记这个潜在的问题。
*TRUNCATE TABLE也可以删除表中的所有数据,词语句首先摧毁表,再新建表。此种方式删除的数据不能在事务中恢复。
*实验:
?删除表中名称为’zs’的记录。
?delete from emp where name='黄忠';
?删除表中所有记录。
?delete from emp;
?使用truncate删除表中记录。
?truncate table emp;
?
? 4.select操作
?
? (1)select [distinct] *|field1,field2,......from tab_name 其中from指定从哪张表筛选,*表示查找所有列,也可以指定一个列列表明确指定要查找的列,distinct用来剔除重复行。
?*实验:
? 查询表中所有学生的信息。
? select * from exam;
? 查询表中所有学生的姓名和对应的英语成绩。
? select name,english from exam;
? 过滤表中重复数据。
? select distinct english from exam;
?
? (2)select 也可以使用表达式,并且可以使用 as 别名
? 在所有学生分数上加10分特长分显示。
? select name,english+10,chinese+10,math+10 from exam;
? 统计每个学生的总分。
? select name,english+chinese+math from exam;
? 使用别名表示学生总分。
? select name,english+chinese+math as 总成绩 from exam;
? select name,english+chinese+math 总成绩 from exam;
? select name english from exam;
?
? (3)使用where子句,进行过滤查询。
*练习:
? 查询姓名为XXX的学生成绩
? select * from exam where name='张飞';
? 查询英语成绩大于90分的同学
? select name,english from exam where english>90;
? 查询总分大于200分的所有同学
? select name,math+english+chinese as 总成绩 from exam where math+english+chinese>200 ;
? *where字句中可以使用:
?*比较运算符:
?> < >= <= <> ?
?between 10 and 20 值在10到20之间 ?
?in(10,20,3)值是10或20或30
?like '张pattern' pattern可以是%或者_,如果是%则表示任意多字符,此例中张三丰 张飞 张abcd ,如果是_则表示一个字符张_,张飞符合。张
?Is null
?
?*逻辑运算符
? 在多个条件直接可以使用逻辑运算符 and or not
? *实验
查询英语分数在 80-100之间的同学。
?select name ,english from exam where english between 80 and 100;
查询数学分数为75,76,77的同学。
select name ,math from exam where math in (75,76,77);
查询所有姓张的学生成绩。
select * from exam where name like '张%';
查询数学分>70,语文分>80的同学。
select name from exam where math>70 and chinese >80;
查找缺考数学的学生的姓名
select name from exam where math is null;
? (4)Order by 指定排序的列,排序的列即可是表中的列名,也可以是select 语句后指定的别名。
? Asc 升序、Desc 降序,其中asc为默认值
? ORDER BY 子句应位于SELECT语句的结尾。
?*练习:
? 对数学成绩排序后输出。
? select * from exam order by math;
? 对总分排序按从高到低的顺序输出
? select name ,(ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0)) 总成绩 from exam order by 总成绩 desc;
? 对姓张的学生成绩排序输出
? select name ,(ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0)) 总成绩 from exam where name like '张%' order by 总成绩 desc;
? (5)聚合函数:技巧,先不要管聚合函数要干嘛,先把要求的内容查出来再包上聚合函数即可。
?count(列名)
? 统计一个班级共有多少学生?先查出所有的学生,再用count包上
select count(*) from exam;
? 统计数学成绩大于70的学生有多少个?
select count(math) from exam where math>70;
? 统计总分大于250的人数有多少?
select count(name) from exam where (ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0))>250;
?sum(列名)
? 统计一个班级数学总成绩?先查出所有的数学成绩,再用sum包上
select sum(math) from exam;
? 统计一个班级语文、英语、数学各科的总成绩
select sum(math),sum(english),sum(chinese) from exam;
? 统计一个班级语文、英语、数学的成绩总和
select sum(ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0)) as 总成绩 from exam;
? 统计一个班级语文成绩平均分
?select s