设为首页 加入收藏

TOP

MySql基本语法及练习(4)(一)
2015-07-24 11:53:44 来源: 作者: 【 】 浏览:6
Tags:MySql 基本 语法 练习

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 stu

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇mysql旧表去重数据导入新表且赋予.. 下一篇MySQLWindowsZIP免安装版的设置和..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Announcing October (2025-12-24 15:18:16)
·MySQL有什么推荐的学 (2025-12-24 15:18:13)
·到底应该用MySQL还是 (2025-12-24 15:18:11)
·进入Linux世界大门的 (2025-12-24 14:51:47)
·Download Linux | Li (2025-12-24 14:51:44)