1.创建一个员工表(并指明字符集为UTF8)
drop table if exists employee;
create table employee(
id int,
name varchar(20),
gender varchar(6),
birthday date,
entry_date date,
job varchar(30),
salary float(5,1),
resume text
);
2.插入数据:
insert into employee(id,name,gender,birthday,entry_date,job,salary,resume)
values(1,‘jack‘,‘male‘,‘2011-10-8‘,‘2011-12-31‘,‘software‘,5000.1,‘hello‘);
3.在上面员工表的基本上增加一个image列。
alter table employee add image blob;
4.修改job列,使其长度为60。
alter table employee
modify job varchar(60) default ‘teacher‘;
5.删除gender列。
alter table employee drop gender;
6.表名改为user。
rename table employee to user;
注意:对于MySql而言,不能修改数据库的名字,但是可以修改表名
7.修改表的字符集为gbk。
alter table user
character set UTF8;
8.列名name修改为username。
alter table user
change column name username varchar(20);
9.向user表插入一条中文记录
insert into user(username,id,birthday,entry_date,job,salary,resume)
values(‘杰克‘,2,‘2011-10-8‘,‘2011-12-31‘,‘software‘,5000.1,‘你好‘);
insert into user values(3,‘马利‘,‘2011-10-8‘,‘2011-12-31‘,‘software‘,5000.1,‘你好‘,NULL);
insert into user values(4,‘马利‘,‘2011-10-8‘,‘2011-12-31‘,‘software‘,5000.1,NULL,NULL);
insert into user(id,username,birthday,entry_date,job,salary,image)
values(5,‘马利‘,‘2011-10-8‘,‘2011-12-31‘,‘software‘,5000.1,NULL);
10.修改客户端输入和输出使用的编码方式,与WindowXP平台一致
set character_set_client=gbk;
set character_set_results=gbk;
11.将所有员工薪水修改为6000元。
update user set salary = 6000;
12.将姓名为’马利’的员工薪水修改为7000元。
update user set salary = 7000 where username = ‘马利‘;
13.将’jack’的薪水在原有基础上增加1000元。
update user set salary = salary + 1000 where username = ‘jack‘;
14.删除表中名称为’jack’的记录。
delete from user where username = ‘jack‘;
15.删除表中所有记录。
delete from user;
16.使用truncate删除表中记录。
truncate table user;
17.查询表中所有学生的信息。
select * from student;
select id,name,math,chinese,english from student;
select name,id,math,chinese,english from student;
select name,math from student;
18.查询表中所有学生的姓名和对应的英语成绩。
select name,english from student;
19.过滤表中重复数据。
select distinct english from student;
select distinct name,english from student;
20.在所有学生分数上加10分特长分。
select name,math+10 from student;
select name as 姓名,math+10 as 数学 from student;
21.统计每个学生的总分。
select name,math+chinese+english
from student;
22.使用别名表示学生分数。
select name,math+chinese+english as 总分
from student;
23.查询姓名为’张小明’的学生成绩
select * from student
where name = ‘张小明‘;
24.查询英语成绩大于90分的同学
select * from student
where english > 90;
24.查询总分大于200分的所有同学
select name,chinese+math+english as 总分
from student
where chinese+math+english > 200;
25.查询英语分数在 80-90之间的同学。
select *
from student
where english>=80 and english<=90;
或
select *
from student
where english between 80 and 90;
26.查询数学分数为89,90,91的同学。
select *
from student
where math=89 or math= 90 or math=91;
或
select *
from student
where math [not] in(89,90,91);
27.查询所有姓’李’的学生成绩。
select *
from student
where name LIKE ‘李%‘;
select * from student
where name LIKE ‘%李‘;
select * from student
where name LIKE ‘%李%‘;
28.在网站开发中多条件查询中常用到
select * from student
where name LIKE ‘%%‘;
select * from student
where name LIKE ‘__李‘;
select * from student
where math IS [NOT] NULL;
29.查询数学分>80且语文分>80的同学。
select *
from student
where math >80 and chinese>80;
30.对数学成绩排序后输出。
升序:
select *
from student
order by math asc;
降序:
select *
from student
order by math desc;
对总分降序后输出。
select name,math+chinese+english as 总分
from student
order by math+chinese+english desc;
31.对姓’李’的学生总分降序输出。
select name,math+chinese+english as 总分
from student
where name LIKE ‘李%‘
order by math+chinese+english desc;
32.统计一个班级共有多少学生?
select count(*) as 总人数
from student;
33.统计数学成绩大于80的学生有多少个?
select count(*) as 总人数
from student
where math > 80;
34.统计总分大于250的人数有多少?
select count(*) as 总人数
from student
where (math+chinese+english) > 250;
select count(english) as 总人数
from student;//13
select count(math) as 总人数
from student;
35.统计一个班级数学总成绩。
select sum(math)
from student;
select sum(name)
from student;//0
36.统计一个班级语文、英语、数学各科的总成绩。
select sum(math) as 数学总分,sum(chinese) as 语文总分,sum(english) as 英语总分
from student;
37.统计一个班级语文、英语、数学的成绩总和。
select sum(math)+sum(chinese)+sum(english) as 班级总分
from student;
38.统计一个班级语文成绩平均分。
select sum(math)/count(math)
from student;
select sum(math)/count(*)
from student;
总结:
(1).delete from 或truncate table或drop table的各自的区别:
delete from:按行删除表中的所有记录,但会保留表,适合删除数据量不大的数据,可按条件删除
truncate table:复制原表结构-〉一次性删除整表 -> 自动恢复原表结构,适合删除数据量较大的数据,不能按条件删除
drop table:删除表本身
删除记录时,一定要留意表间的关联关系
(2).排序:NULL值为最小,使用order by子句,默认升序,order by子句必须放置在最后
(3).复合函数
(1)count()函数,统计之用,不统计NULL值
(2)sum()函数,统计和之用,不要统计非数值,如果统计非数值,返回0
(4).合计函数
avg()
max(),min(),当max()和min()函数位于日期类型时,分别取得最近日期和最早日期
MySql基本语法及练习(4),布布扣,bubuko.com
原文地址:http://blog.csdn.net/u011662320/article/details/38390563