1.下列表结构,利用SQL语句在mydb数据库中创建topic表。

create table topic(
id int comment '专题编号',
title varchar(255)comment '专题名称',
intro varchar(255)comment '专题介绍',
start_time int comment '专题开始时间',
end_time int comment '专题结束时间'
);

2.为mydb.goods表新增total(库存量)和add_time(发布时间)字段。
create table goods(
id int comment '编号',
name varchar(32) comment '商品名',
price int comment '价格',
description varchar(255) comment '商品描述'
);
alter table goods add total int comment '库存量',
add add_time int comment '发布时间';


3.请设计一张学生表,选择合理的数据类型保存学号、姓名、性别、出生日期、入学日期、家庭住址信息。
create table student(
sno int unsigned not null auto_increment comment '学号',
sname varchar(20)not null comment '姓名',
ssex char(1)not nul1default '男' comment '性别',
sbirthday date not null comment '出生日期',
sadmissiondate date not nul1comment '入学日期',
sfamilyadressvarchar(50) comment '地址',
primary key(sno),
unique(sno),
index(sname)
);

4.请设计一张留言表,用于保存网站留言版中游客发表的留言。
create table message(
uid int unsigned not null auto_increment comment '用户id',
umane varchar(20) not null comment '用户昵称',
title varchar(255) not null comment '评论内容',
addtime datetime not null comment '评论时间',
primary key(uid),
unique(uid),
index(uname)
);

5.依据sh_goods的结构与数据,在mydb数据库中创建一张tm_goods表,并将价格在20到50之间的商品价格减5元,库存量再新增300件。
create table mydb.tm_goods like shop.sh_goods;
insert into mydb.tm_goods select * from shop.sh_goods;
update mydb.tm_goods set price=price-5,stock=stock+300
where price between 20 and 50;


6.在sh_goods表中查询评分小于4的商品的不同分类id。
select distinct category_id from sh_goods
where score<4;

7.在数据库shop中,完成商品表(sh_goods)与商品评价表(sh_goods_comment)各种需求的查询操作。
1、查询商品id等于8且有效的评论内容。
2、查询每个用户评论的商品数量。
3、查询最新发布的5条有效商品评论信息。
4、查询评论过两种以上不同商品的用户id及对应的商品id。
5、结合sh_goods和sh_goods_comment表,查询没有任何评论信息的商品id和name。
6、结合sh_goods和sh_goods_comment表,查询商品评分为5星的商品评论信息。
1、select id,content from sh_goods_comment
where goods_id = 8 and is_show = 1;
2、select user_id,count(goods_id) from sh_goods_comment
group by user_id;
3、select id,content,user_id,goods_id from sh_goods_comment
where is_show = 1 order by create_time desc limit 5;
4、select user_id,group_concat(goods_id) from sh_goods_comment
group by user_id having count(distinct goods_id) >= 2;
5、select id,name from sh_goods
where id not in (select distinct goods_id from sh_goods_comment);
6、select id,content from sh_goods_comment
where goods_id in (select id from sh_goods where score = 5);






导入数据库文件chapter04.sql 导入语句:source chapter04.sql的文件路径;
解决文件乱码:show variables like 'character%';
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
输入完成后重启数据库软件
这篇博客详细介绍了如何在MySQL8.0中进行数据库操作,包括创建topic、goods、student和message表,修改goods表结构,数据迁移,以及执行复杂的查询操作,如查询评分低于4的商品分类ID,查询商品评论等。
6825

被折叠的 条评论
为什么被折叠?



