MySQL02数据库练习题
一、语言环境:
实现要求:使用PHP的MySQL的数据库知识完成以下作业。
1.在命令行模式下登录MySQL数据库,使用SQL实现下面要求(写出对应sql语句):
1)创建留言数据库: liuyandb;
2)在liuyandb数据库中创建留言表liuyan,结构如下
3)在留言表最后添加一列状态(status tinyint 默认值为0),
4)修改留言表author的默认值为’youku’,设为非空。
5)删除liuyan表中的isdelete字段。
6)mysql> alter table liuyan drop isdelete;
7)
8)为留言表添加>5条测试数据.
9)mysql> insert into liuyan values(null,'介绍','大雄
','1000','哥不是一匹好马,但也
10)不是一头普通的毛驴',null),(null,'叮当猫','熊熊
','2000','你牙缝里有韭菜,扣出来贼
11)哥吃',null),(null,'花花','苗苗','3000','苗苗问花花:卖
萌是褒义词还是贬义词',nul
12)l),(null,'霞哥','雄大','4000','斗战色佛
',null),(null,'晨晨','逗比','5000','你笑
13)起来像一朵菊花,菊花残,man腚伤',null);
14)Query OK, 5 rows affected sec)
15)要求将id值大于3的信息中author字段值改为admin
16)
17)mysql> update liuyan set author='admin' where id>3;
18)删除id号为4的数据。
19)mysql> delete from liuyan where id=4;
附加题:
20)为留言表添加>15条测试数据,要求分三个用户添加.
21)查询所有留言信息。
22)查询某一用户的留言信息。
23)mysql> select * from liuyan where author='大雄';
24)查询所有数据,按时间降序排序。
25)获取id在2到6之间的留言信息,并按时间降序排序。
26)统计每个用户留了多少条留言,并对数量按从小到大排序。
27)mysql> select count(id) from liuyan group by author order
by count(id) asc;
28)将id为8、9的两条数据的作者改为’doudou’.
29)取出最新的三条留言。
(使用limit)。
30)查询留言者中包含”d”字母的留言信息,并按留言时间从小
到大排序。
mysql> create table liuyan(
-> id int auto_increment primary key,
-> title varchar(32) not null,
-> author varchar(16) null,
-> addtime varchar(12) not null,
-> content text not null,
-> isdelete tinyint not null default 0)engine=myisam default charset=utf8;
Query OK, 0 rows affected sec)
mysql> alter table liuyan add status tinyint default 0 after isdelete; Query OK, 0 rows affected sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc liuyan;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(32) | NO | | NULL | |
| author | varchar(16) | YES | | NULL | |
| addtime | varchar(12) | NO | | NULL | |
| content | text | NO | | NULL | |
| isdelete | tinyint(4) | NO | | 0 | |
| status | tinyint(4) | YES | | 0 | |
+----------+-------------+------+-----+---------+----------------+
7 rows in set sec)
3.
mysql> alter table liuyan modify author varchar(16) not null default 'youku'; Query OK, 0 rows affected sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc liuyan;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(32) | NO | | NULL | | | author | varchar(16) | NO | | youku | | | addtime | varchar(12) | NO | | NULL | | | content | text | NO | | NULL | | | isdelete | tinyint(4) | NO | | 0 | | | status | tinyint(4) | YES | | 0 | | +----------+-------------+------+-----+---------+----------------+ 7 rows in set sec)。