DROP TABLE IF EXISTS `liuyan`; CREATE TABLE `liuyan` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(32) NOT NULL, `author` varchar(16) DEFAULT NULL, `addtime` datetime DEFAULT NULL, `content` text, `isdelete` char(1) NOT NULL DEFAULT \'0\', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of liuyan -- ---------------------------- INSERT INTO `liuyan` VALUES (\'1\', \'介绍\', \'大雄\', \'2017-02-14 09:59:37\', \'哥不是一匹好马,但也不是一头普通的毛驴\', \'0\'); INSERT INTO `liuyan` VALUES (\'2\', \'叮当猫\', \'熊熊\', \'2016-02-16 09:59:44\', \'你牙缝里有韭菜,扣出来贼哥吃\', \'0\'); INSERT INTO `liuyan` VALUES (\'3\', \'花花\', \'苗苗\', \'2017-05-28 09:59:52\', \'苗苗问花花:卖萌是褒义词还是贬义词?\', \'0\'); INSERT INTO `liuyan` VALUES (\'4\', \'霞哥\', \'大雄\', \'2017-08-29 09:59:57\', \'斗战色佛\', \'0\'); INSERT INTO `liuyan` VALUES (\'5\', \'晨晨\', \'逗比\', \'2010-06-22 10:00:03\', \'你笑起来像一朵菊花,菊花残,man腚伤\', \'0\');
1.创建留言数据库: liuyandb;
结果:
2.在liuyandb数据库中创建留言表liuyan,结构如下:
表名 |
liuyan |
留言信息表 |
|||
序号 |
字段名称 |
字段说明 |
类型 |
属性 |
备注 |
1 |
id |
编号 |
int |
非空 |
主键,自增1 |
2 |
title |
标题 |
varchar(32) |
非空 |
|
3 |
author |
作者 |
varchar(16) |
可以空 |
|
4 |
addtime |
留言时间 |
datetime |
非空 |
|
5 |
content |
留言内容 |
text |
非空 |
|
6 |
isdelete |
是否删除 |
char(1) |
非空 |
默认值 0 |
结果:
3.在留言表最后添加一列状态(status char(1) 默认值为0)
alter table liuyan change STATUS status char(1)
![](https://image.cha138.com/20210605/4ea8756179904f0da38a5c68a7052c0f.jpg)
4.修改留言表author的默认值为’youku’,设为非空
alter table liuyan modify author varchar(50) default \'youku\' not null;
5.删除liuyan表中的isdelete字段
alter table liuyan drop isdelete;
6.为留言表添加>5条测试数据 (例如:)
INSERT INTO `liuyan` VALUES (\'6\', \'标表题\', \'大mao\', \'2017-06-14 09:59:37\', \'h\', \'0\');
INSERT INTO `liuyan` VALUES (\'7\', \'OOOO\', \'ange好吗\', \'2016-04-16 09:59:44\', \'猪\', \'0\');
INSERT INTO `liuyan` VALUES (\'8\', \'ME\', \'刚刚\', \'2017-02-28 09:59:52\', \'楼主十足\', \'0\');
INSERT INTO `liuyan` VALUES (\'9\', \'IW\', \'卡卡\', \'2017-06-29 09:59:57\', \'万岁\', \'0\');
INSERT INTO `liuyan` VALUES (\'10\', \'WHO\', \'姑奶奶\', \'2010-24-22 10:00:03\', \'赞赞赞\', \'0\');
7. 要求将id值大于3的信息中author 字段值改为admin
update liuyan set author = \'admin\' where id > 3;
8. 删除id号为4的数据。
delete from liuyan where id=4;
附加题:
- 为留言表添加>10条测试数据,要求分三个作者添加数据
- 查询某一个作者的留言信息。
- 查询所有数据,按时间降序排序。
- 获取id在2到6之间的留言信息,并按时间降序排序
- 统计每个作者留了多少条留言,并对数量按从小到大排序。
- 将id为8、9的两条数据的作者改为’doudou’.
- 取出最新的三条留言。
- 查询留言者中包含”a”字母的留言信息,并按留言时间从小到大排序
- 删除”作者”重复的数据,并保留id最大的一个作者
MySQL 练习题2
1.表关系
2.下面:开始你的表演
1.查询所有人员信息
DROP TABLE IF EXISTS `ren`; CREATE TABLE `ren` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) NOT NULL, `age` int(8) DEFAULT NULL, `salary` int(8) DEFAULT NULL, `leader` int(11) NOT NULL DEFAULT \'0\', `menpai` char(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of liuyan -- ---------------------------- INSERT INTO `liuyan` VALUES (\'1\', \'张丰\', \'100\', \'10000\', \'0\', \'武当\'); INSERT INTO `liuyan` VALUES (\'2\', \'张无忌\', \'20\', \'8000\', \'0\', \'明教\'); INSERT INTO `liuyan` VALUES (\'3\', \'岳不群\', \'40\', \'6500\', \'0\', \'华山\'); INSERT INTO `liuyan` VALUES (\'4\', \'东方不败\', \'35\', \'12000\', \'0\', \'日月神教\'); INSERT INTO `liuyan` VALUES (\'5\', \'令狐冲\', \'21\', \'4000\', \'3\', \'华山\'); INSERT INTO `liuyan` VALUES (\'6\', \'林平芝\', \'18\', \'2000\', \'3\', \'华山\'); INSERT INTO `liuyan` VALUES (\'7\', \'金毛狮王\', \'50\', \'10000\', \'2\', \'明教\'); INSERT INTO `liuyan` VALUES (\'8\', \'张翠山\', \'46\', \'10000\', \'1\', \'武当\'); INSERT INTO `liuyan` VALUES (\'9\', \'张远桥\', \'55\', \'6500\', \'1\', \'武当\'); INSERT INTO `liuyan` VALUES (\'10\', \'Alex\', \'12\', \'350\', \'0\', \'python\');
2.只查询人员的姓名和年龄
select name,age from ren;
3.查询年龄为20岁的有哪些人员
select name from ren where age=20;
4.查询60岁以下的人员有哪些人员
select name from ren where age<60;
5.查询50岁以上并且工资大于8000的人员有哪些
select name from ren where age>50 and salary>8000;
6.查询姓[张]的人员有哪些
select * from ren where name like \'张%\';
7.查询哪些人员属于 武当/华山/嵩山#没有嵩山
select * from ren where menpai in(\'武当\',\'华山\')
8.查询工资在 5000-8900 的人员有哪些
select * from ren where salary between 5000 and 8900;
9.查询所有人员,要求按工资倒序排列
select * from ren order by salary desc;
10.查询令狐冲的领导人是谁
select * from ren where menpai=(select menpai from ren where name = \'令狐冲\') and leader=\'0\';
11.查询人员表中最高工资是多少
select max(salary) from ren;
12.查询人员表中最低工资是多少
-- 12.查询人员表中最低工资是多少
select min(salary) from ren;
13.查询所有人员的平均工资是多少
select AVG(salary) from ren;
14.查询所有人员的工资总和是多少
select sum(salary) from ren;
15.查询目前有多少个人员
select count(name) from ren;
16.查询当前武林中有哪些门派
方式一:select menpai from ren group by menpai;
方式二:select
DISTINCT
menpai
from
ren;
17.查询 武当派 最高工资是谁
select name from ren where menpai=\'武当\' and salary=(select max(salary) from ren where menpai=\'武当\');
18.查询各门派的平均工资是多少
select avg(salary),menpai from ren group by menpai;
19.查询当前武林中有哪些门派的平均工资大于8000 并按工资倒序排列
elect avg(salary),menpai from ren group by menpai having avg(salary)>8000 order by avg(salary) desc;
20.查询当前人员表的中的第3条数据到第7条数据
select * from ren LIMIT 2,5;
21.查询哪些门派下没有弟子
select * from ren group by menpai having count(*) =1;
22.查询武当派下有哪些弟子
select * from ren where menpai=\'武当\' and leader != 0;
23.查询各门派的工资总和按倒序/正序排列
倒序:select menpai,sum(salary) from ren group by menpai order by sum(salary) desc;
24.删除工资重复的人员,请保留年龄最大的一个人
25.将武当派 张三丰 修改为 张丰
update ren set name =\'张丰\' where name=\'张三丰\';
26.将所有门派大哥工资上调10%,但不包括Alex.
update
ren
set
salary = salary+salary*0.1
where
leader = 0
and
name
!=
\'Alex\'
;
27.查看哪些人员的门派已登记地理位置.
select name,address from ren,dept where dept.dname = ren.menpai;
28.查询所有人员门派的位置信息,不存在位置信息则不显示
SELECT name,address FROM ren LEFT JOIN dept on ren.menpai = dept.dname;
29.在湖北省内的门派中的人员有哪些.
SELECT
*
FROM
ren
INNER
JOIN
dept
on
ren.menpai = dept.dname
AND
dept.address =
\'湖北\'
;
30.在陕西省内门派中的工资小于5000,年龄大于20岁的人员有哪些,按主键倒序排列
SELECT
*
FROM
ren
INNER
JOIN
dept
on
ren.menpai = dept.dname
AND
dept.address =
\'陕西\'
and
ren.salary <5000
AND
ren.age >20
ORDER
BY
ren.id
DESC
;
![](https://image.cha138.com/20210605/3a7b5019b96840a0a96739af7ec01269.jpg)
MySQL 练习题3
1.创建表结构和表数据
![](https://image.cha138.com/20210605/df73e37b82644a298fc0801a192b396e.jpg)
-- 创建数据表 CREATE TABLE IF NOT EXISTS tdb_goods( goods_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -- 商品主键 goods_name VARCHAR(150) NOT NULL, -- 商品名称 goods_cate VARCHAR(40) NOT NULL, -- 商品类型 brand_name VARCHAR(40) NOT NULL, -- 商品品牌 goods_price DECIMAL(15,3) UNSIGNED NOT NULL DEFAULT 0, -- 商品价格 is_show BOOLEAN NOT NULL DEFAULT 1, -- 是否上架 is_saleoff BOOLEAN NOT NULL DEFAULT 0 -- 是否打折 ); -- 写入记录 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'R510VC 15.6英寸笔记本\',\'笔记本\',\'华硕\',\'3399\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'Y400N 14.0英寸笔记本电脑\',\'笔记本\',\'联想\',\'4899\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'G150TH 15.6英寸游戏本\',\'游戏本\',\'雷神\',\'8499\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'X550CC 15.6英寸笔记本\',\'笔记本\',\'华硕\',\'2799\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'X240(20ALA0EYCD) 12.5英寸超极本\',\'超级本\',\'联想\',\'4999\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'U330P 13.3英寸超极本\',\'超级本\',\'联想\',\'4299\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'SVP13226SCB 13.3英寸触控超极本\',\'超级本\',\'索尼\',\'7999\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'iPad mini MD531CH/A 7.9英寸平板电脑\',\'平板电脑\',\'苹果\',\'1998\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)\',\'平板电脑\',\'苹果\',\'3388\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\' iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版)\',\'平板电脑\',\'苹果\',\'2788\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'IdeaCentre C340 20英寸一体电脑 \',\'台式机\',\'联想\',\'3499\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'Vostro 3800-R1206 台式电脑\',\'台式机\',\'戴尔\',\'2899\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'iMac ME086CH/A 21.5英寸一体电脑\',\'台式机\',\'苹果\',\'9188\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )\',\'台式机\',\'宏碁\',\'3699\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'Z220SFF F4F06PA工作站\',\'服务器/工作站\',\'惠普\',\'4288\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'PowerEdge T110 II服务器\',\'服务器/工作站\',\'戴尔\',\'5388\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'Mac Pro MD878CH/A 专业级台式电脑\',\'服务器/工作站\',\'苹果\',\'28888\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\' HMZ-T3W 头戴显示设备\',\'笔记本配件\',\'索尼\',\'6999\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'商务双肩背包\',\'笔记本配件\',\'索尼\',\'99\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'X3250 M4机架式服务器 2583i14\',\'服务器/工作站\',\'IBM\',\'6888\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'玄龙精英版 笔记本散热器\',\'笔记本配件\',\'九州风神\',\'\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\' HMZ-T3W 头戴显示设备\',\'笔记本配件\',\'索尼\',\'6999\',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(\'商务双肩背包\',\'笔记本配件\',\'索尼\',\'99\',DEFAULT,DEFAULT); 创建表和数据
2. 求所有电脑产品的平均价格,并且保留两位小数,AVG,MAX,MIN,COUNT,SUM为聚合函数
1
|
SELECT ROUND( AVG (goods_price),2) AS avg_price FROM tdb_goods; |
3.查询所有价格大于平均价格的商品,并且按价格降序排序
1
2
3
4
5
|
SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price > ( SELECT ROUND( AVG (goods_price),2) AS avg_price FROM tdb_goods) ORDER BY goods_price DESC ; |
4.查询类型为“超记本”的商品价格
1
|
SELECT goods_price FROM tdb_goods WHERE goods_cate = \'超级本\' ; |
5.查询价格等于"超级本"价格的商品,并且按价格降序排列
1
2
3
4
5
|
SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price IN ( SELECT goods_price FROM tdb_goods WHERE goods_cate = \'超级本\' ) ORDER BY goods_price DESC ; |
6.创建“商品类别”表
1
2
3
4
5
6
7
|
CREATE TABLE IF NOT EXISTS tdb_goods_cates( cate_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, cate_name VARCHAR (40) ); |
7.查询tdb_goods表的类别记录,并且按"类别"分组
1
|
SELECT goods_cate FROM tdb_goods GROUP BY goods_cate; |
8.将分组结果写入到tdb_goods_cates数据表
1
|
INSERT tdb_goods_cates (cate_name) SELECT goods_cate FROM tdb_goods GROUP BY goods_cate; |
9.通过tdb_goods_cates数据表来更新tdb_goods表中的\'类别字段\'
1
2
3
4
5
6
7
|
-- 1.通过内连接得到两个表的结果 select * from tdb_goods INNER JOIN tdb_goods_cates ON goods_cate = cate_name; -- 2.通过上面的得到的临时表 |