mysql模拟题1_MySQL 练习题1 参考答案

1.创建留言数据库: liuyandb;

create database liuyandb;

2.在liuyandb数据库中创建留言表liuyan,

create table liuyan(

id int auto_increment primary key comment '编号',

title varchar(32) not null comment '标题',

author varchar(16) null comment '作者',

addtime datatime not null comment '留言时间',

content text not null comment '留言内容',

isdelete char(1) not null default 0 comment '是否删除'

)engine=innodb default charset=utf8;

注意: comment :表示注释

3.在留言表最后添加一列状态(status char(1) 默认值为0)

alter table liuyan add status char(1) default 0;

4.修改留言表author的默认值为’youku’,设为非空

alter table liuyan modify author varchar(16) not null default 'youku';

5.删除liuyan表中的isdelete字段

alter table liuyan drop isdelete;

6.为留言表添加>5条测试数据

insert into liuyan values

(null,'介绍','大雄','2017-02-14 09:59:37','哥不是一匹好马,但也不是一头普通的毛驴',null),

(null,'叮当猫','熊熊','2016-02-16 09:59:44','你牙缝里有韭菜,扣出来贼哥吃',null),

(null,'花花','苗苗','2017-05-28 09:59:52','苗苗问花花:卖萌是褒义词还是贬义词?',null),

(null,'霞哥','大雄','2017-08-29 09:59:57','斗战色佛',null),

(null,'晨晨','逗比','2010-06-22 10:00:03','你笑起来像一朵菊花,菊花残,man腚伤',null);

7.要求将id值大于3的信息中author字段值改为admin

update liuyan set author='admin' where id>3;

8.删除id号为4的数据

delete from liuyan where id=4;

附加题

1.为留言表添加>15条测试数据,要求分三个用户添加

.....

2.查询所有留言信息

select id,title,author,addtime,content,status from liuyan;

3.查询某一用户的留言信息

select id,title,author,addtime,content,status from liuyan where author='用户名称';

4.查询所有数据,按时间降序排序

select id,title,author,addtime,content,status from liuyan order by addtime desc;

5.获取id在2到6之间的留言信息,并按时间降序排序

select id,title,author,addtime,content,status from liuyan where id between 2 and 6;

6.统计每个用户留了多少条留言,并对数量按从小到大排序。

select author,count(id) as'留言条数' from liuyan group by author order by count(id) desc

7.将id为8、9的两条数据的作者改为’doudou’.

update liuyan set author ='doudou' where id in(8,9);

8.取出最新的三条留言。(使用limit)。

select * from (select id,title,author,addtime,content,status from liuyan ORDER BY addtime desc) haha LIMIT 3

9.查询留言者中包含”a”字母的留言信息,并按留言时间从小到大排序

select id,title,author,addtime,content,status from liuyan where author like'%a%' order by addtime asc;

10.删除”作者”重复的数据,并保留id最大的一个作者

delete from liuyan where author in(

select author from (select author from liuyan group by author having count(1)>1) a

)

and id not in(

select id from (select max(id) id from liuyan group by author having count(1)>1) b

)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值