MySQL 练习题1 参考答案

本文详细介绍了一个留言板数据库的设计与实现过程,包括数据库与表的创建、字段的增删改、数据的插入与更新、以及各类查询操作等核心内容。

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

1
create database liuyandb;

 

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

1
2
3
4
5
6
7
8
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)

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

 

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

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


5.删除liuyan表中的isdelete字段

1
alter table liuyan drop isdelete;

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

1
2
3
4
5
6
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

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

8.删除id号为4的数据

1
delete from liuyan where id=4;

附加题

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

.....

2.查询所有留言信息

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1
2
3
4
5
6
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
)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值