2020-09-09

2020.9.8

mysql数据库练习

1、用SQL语句创建以下三张表,要求字段属性要求如图所示。

win+r 键,打开cmd
连接自己得数据库,输入命令

学生表:t_student
1

create table t_student (
-> id int(16) not null primary key,
-> sname varchar(255) not null,
-> sex varchar(255),
-> age int(16),
-> phone varchar(255),
-> address varchar(255),
-> cid int(255),
-> remark varchar(255)
-> );

班级表:t_class

2

create table t_class (
-> id int(11) not null primary key,
-> cname varchar(255),
-> teacher varchar(255),
-> remark varchar(255)
-> );

成绩表:t_grade

3

create table t_grade (
-> id int(11) not null primary key,
-> sid int(11),
-> chinese varchar(255),
-> math varchar(255),
-> english varchar(255),
-> remark varchar(255)
-> );

2、 用SQL语句,分别在三张表中插入数据。

注意:学生表的id等于成绩表的sid,班级表的id等于学生表的cid

insert into t_student (id,sname,sex,age,phone,address,cid,remark) values (1,'李  白','男',37,' 19087673389','北京',1 ,'');
insert into t_student (id,sname,sex,age,phone,address,cid,remark) values (2,'杜甫','男',18,' 18733899393','南京',2 ,'');
insert into t_student (id,sname,sex,age,phone,address,cid,remark) values (3,'白居易','男',40,' 18763883838','汉江',3 ,'');

insert into t_class (id,cname,teacher,remark) values (1,'火箭班','流云’ ,'');
insert into t_class (id,cname,teacher,remark) values (2,'快乐班','浪晋' ,'');
insert into t_class (id,cname,teacher,remark) values (3,'神仙班','希希' ,'');


insert into t_grade (id,sid,chinese,math,english,remark) values (1,1,99,88,66,’');
insert into t_grade (id,sid,chinese,math,english,remark) values (2,2,58,100,88,’');
insert into t_grade (id,sid,chinese,math,english,remark) values (3,3,88,79,100,’');
3、 用SQL语句对班级表学生表进行多表联查,显示班级表的班级名称和学生的id,姓名

SELECT
a.id,
a.sname,
c.cname
FROM
t_student a
JOIN t_grade b ON a.id = b.sid
JOIN t_class c ON a.cid = c.id;

4、 用SQL语句对学生表成绩表多表联查,显示学生的id,sname和成绩表的语数外的成绩。

SELECT
a.id,
a.sname,
b.chinese,
b.math,
b.english
FROM
t_student a
JOIN t_grade b ON a.id = b.sid
JOIN t_class c ON a.cid = c.id;

5、 用SQL语句,对三张表进行多表联查,显示学生表的id,sname班级表的cname,成绩表的语数外成绩。

SELECT
a.id,
a.sname,
b.chinese,
b.math,
b.english,
c.cname
FROM
t_student a
JOIN t_grade b ON a.id = b.sid
JOIN t_class c ON a.cid = c.id;

注意,以上题目,请把sql语句写出来。

附加题

在测谈网的数据库上进行操作,把sql语句写出来:
目的:在工作中,同事是不会给你讲数据库的表关系的,一般都是让你自己去弄,自己通过查看注释+问同事的方式,弄清楚数据库的表关系。所以现在我给了大家一个系统的数据库,请大家梳理一下数据库的表关系,最好可以画出图。相关的必要信息,看下面的内容。
图的列子:

4

数据库信息:
IP:192.144.148.91
端口:3306
账号:
密码:
数据库名字:ljtestdb

测谈网前端前台地址:http://81.68.125.221:8080/ljindex/
表关系说明:
t_user表是用户表
t_article是文章表
t_article_user_status是记录的用户对文章进行点赞收藏的记录。
t_user_comments是用户评论的表。
注意:用户表的id就是其他表的uid。也就是说t_user.id = t_article.uid
以上每个表的字段的含义和作用,请通过DDL查看,有详细注释。

1、 通过SQL语句,查看你写的所有文章,不显示已经删除了的。

SELECT
*
FROM
t_user a
JOIN t_article b ON a.id = b.uid
JOIN t_user_comments c ON a.id = c.uid
JOIN t_article_user_status d ON a.id = d.uid
WHERE
b. STATUS = 0
AND c. STATUS = 0
AND b.author = ‘浪晋’;

2、 通过SQL语句,查看你对所有点赞过的文章。

SELECT
*
FROM
t_user a
JOIN t_article b ON a.id = b.uid
JOIN t_user_comments c ON a.id = c.uid
JOIN t_article_user_status d ON a.id = d.uid
where b.status=0 and d.gstatus=0 and b.author = ‘浪晋’;

3、 通过SQL语句,查看你所有收藏过的文章。

SELECT
*
FROM
t_user a
JOIN t_article b ON a.id = b.uid
JOIN t_user_comments c ON a.id = c.uid
JOIN t_article_user_status d ON a.id = d.uid
where b.status=0 and d. cstatus=0 and b.author = ‘浪晋’;

4、 通过SQL语句,查出你所有评论过的文章。

SELECT
*
FROM
t_user a
JOIN t_article b ON a.id = b.uid
JOIN t_user_comments c ON a.id = c.uid
JOIN t_article_user_status d ON a.id = d.uid
where b.status=0 and b.author = ‘浪晋’;

数据库知识补充:
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值