mysql连接查询作业答案_MySQL连表查询练习题
1.建庫
庫名:linux50 字符集:utf8 校驗規(guī)則:utf8_general_ci

create database linux4 charset utf8 default collate = utf8_general_ci;
2.建表
2.1表一
表名:student(學(xué)生表)
字段
數(shù)據(jù)類型要求
是否為空
注釋
sno
最多20位
否
學(xué)號(主鍵)
sname
可變長
否
學(xué)生姓名
sage
最小整數(shù),非負(fù)數(shù)
否
學(xué)生年齡
ssex
0,1
否
學(xué)生性別(1是男,0是女)默認(rèn)為男)
sbirthday
時間類型
默認(rèn)為空
學(xué)生生日
class
可變長
否
學(xué)生班級

student表:
create table if not exists student(
sno varchar(21) not null primary key comment '學(xué)號',
sname varchar(12) not null comment '學(xué)生姓名',
sage tinyint unsigned not null comment '學(xué)生年齡',
ssex enum('1','0') default '1' comment '學(xué)生性別',
sbirthday datetime comment '學(xué)生生日',
class varchar(10) not null comment '學(xué)生班級') default charset=utf8;
2.2表二
表名:course(課程表)
字段
數(shù)據(jù)類型要求
是否為空
注釋
cno
最多20位
否
課程號(主鍵)
cname
可變長
否
課程名稱
tno
可變長
否
教師編號

course表:
create table if not exists course(
cno varchar(21) not null primary key comment '課程號',
cname varchar(10) not null comment '課程名稱',
tno varchar(10) not null comment '教師編號') default charset=utf8;
2.3表三
表名:score(成績表)
字段
數(shù)據(jù)類型要求
是否為空
注釋
sno
最多20位
否
學(xué)號(主鍵)
cno
最多20位
否
課程號(主鍵)
mark
浮點數(shù)(4,1)
否
成績
注意:sno和cno在另外兩個表中是主鍵,在這里應(yīng)該是外鍵,不過咱們不需要創(chuàng)建,了解即可
score表:
create table if not exists score(
sno varchar(21) not null primary key comment '學(xué)號',
cno varchar(21) not null comment '課程號',
mark float(4,1) not null comment '成績') default charset=utf8;

2.4表四
表名:teacher(教師表)
字段
數(shù)據(jù)類型要求
是否為空
注釋
tno
最多20位
否
教師編號(主鍵)
tname
可變長
否
教師姓名
tage
最小整數(shù),非負(fù)數(shù)
否
教師年齡
tsex
0,1
否
教師性別(1是男,0是女)默認(rèn)為男)
prof
可變長
是
教師職稱
depart
可變長
否
教師部門

teacher表:
create table if not exists teacher(
tno varchar(21) not null primary key comment '教師編號',
tname varchar(10) not null comment '教師姓名',
tage tinyint unsigned not null comment '教師年齡',
tsex enum('0','1') not null default '1' comment '教師性別',
prof varchar(10) comment '教師職稱',
depart varchar(15) not null comment '教師部門') default charset=utf8;
練習(xí)題
#插入數(shù)據(jù)練習(xí):
1.將自己班級小組所有人員信息插入到student表中(數(shù)據(jù)自定義)
insert into student(sno,sname,sage,ssex,sbirthday,class)
values(1,'李洋',18,1,19980422,'1'),
(2,'溫俊林',19,0,19970422,'1'),
(3,'周恒華',20,1,19950422,'1'),
(4,'張松濤',22,1,19940422,'2'),
(5,'李暉',20,0,19960622,'2'),
(6,'包政',23,1,19930422,'2');
2.將曾導(dǎo)、徐導(dǎo)、李導(dǎo)信息插入教師表中(數(shù)據(jù)自定義)
insert into teacher(tno,tname,tage,tsex,prof,depart)
values('001','曾導(dǎo)',18,1,'校長','linux'),
('002','徐導(dǎo)',19,1,'教學(xué)總監(jiān)','linux'),
('003','李導(dǎo)',20,1,'講師','python');
3.將linux、python學(xué)科插入到課程表中(數(shù)據(jù)自定義)
insert into course(cno,cname,tno)
values('1','linux','001'),
('2','linux','002'),
('3','python','003');
4.將分?jǐn)?shù)插入到成績表中(數(shù)據(jù)自定義)
insert into score(sno,cno,mark)
values('1','1','99.5'),
('2','1','80.5'),
('3','1','85.5'),
('4','1','84.5'),
('5','1','89.5'),
('6','1','89.5');
#查詢練習(xí):
1.查詢student表中的所有記錄的sname、ssex和class列。
mysql> select sname,ssex,class from student;
+-----------+------+--------+
| sname | ssex | class |
+-----------+------+--------+
| 李洋 | 1 | 1 |
| 溫俊林 | 1 | 1 |
| 周恒華 | 1 | 1 |
| 張松濤 | 1 | 2 |
| 李暉 | 1 | 2 |
| 包政 | 1 | 2 |
+-----------+------+--------+
2.查詢教師所有的單位即不重復(fù)的depart列。
mysql> select depart from teacher;
+--------+
| depart |
+--------+
| linux |
| linux |
| python |
+--------+
3.查詢student表的所有記錄。
mysql> select * from student;
+-----+-----------+------+------+---------------------+--------+
| sno | sname | sage | ssex | sbirthday | class |
+-----+-----------+------+------+---------------------+--------+
| 1 | 李洋 | 18 | 1 | 1998-04-22 00:00:00 | 1 |
| 2 | 溫俊林 | 19 | 0 | 1997-04-22 00:00:00 | 1 |
| 3 | 周恒華 | 20 | 1 | 1995-04-22 00:00:00 | 1 |
| 4 | 張松濤 | 22 | 1 | 1994-04-22 00:00:00 | 2 |
| 5 | 李暉 | 20 | 0 | 1996-06-22 00:00:00 | 2 |
| 6 | 包政 | 23 | 1 | 1993-04-22 00:00:00 | 2 |
+-----+-----------+------+------+---------------------+--------+
4.查詢score表中成績在80到90之間的所有記錄。
mysql> select * from score where mark>80 and mark<90;
+-----+------+------+
| sno | cno | mark |
+-----+------+------+
| 2 | 1 | 80.5 |
| 3 | 1 | 85.5 |
| 4 | 1 | 84.5 |
| 5 | 1 | 89.5 |
| 6 | 1 | 89.5 |
+-----+------+------+
5.查詢score表中成績?yōu)?5.5,89.5或80.5的記錄。
mysql> select * from score where mark=85.5 or mark=89.5 or mark=80.5;
+-----+------+------+
| sno | cno | mark |
+-----+------+------+
| 2 | 1 | 80.5 |
| 3 | 1 | 85.5 |
| 5 | 1 | 89.5 |
| 6 | 1 | 89.5 |
+-----+------+------+
6.查詢student表中1班或性別為“女”的同學(xué)記錄。
mysql> select * from student where class=1 or ssex='0';
+-----+-----------+------+------+---------------------+-------+
| sno | sname | sage | ssex | sbirthday | class |
+-----+-----------+------+------+---------------------+-------+
| 1 | 李洋 | 18 | 1 | 1998-04-22 00:00:00 | 1 |
| 2 | 溫俊林 | 19 | 0 | 1997-04-22 00:00:00 | 1 |
| 3 | 周恒華 | 20 | 1 | 1995-04-22 00:00:00 | 1 |
| 5 | 李暉 | 20 | 0 | 1996-06-22 00:00:00 | 2 |
+-----+-----------+------+------+---------------------+-------+
7.以class降序查詢Student表的所有記錄。
mysql> select * from student order by class desc;
+-----+-----------+------+------+---------------------+-------+
| sno | sname | sage | ssex | sbirthday | class |
+-----+-----------+------+------+---------------------+-------+
| 4 | 張松濤 | 22 | 1 | 1994-04-22 00:00:00 | 2 |
| 5 | 李暉 | 20 | 0 | 1996-06-22 00:00:00 | 2 |
| 6 | 包政 | 23 | 1 | 1993-04-22 00:00:00 | 2 |
| 1 | 李洋 | 18 | 1 | 1998-04-22 00:00:00 | 1 |
| 2 | 溫俊林 | 19 | 0 | 1997-04-22 00:00:00 | 1 |
| 3 | 周恒華 | 20 | 1 | 1995-04-22 00:00:00 | 1 |
+-----+-----------+------+------+---------------------+-------+
8.以cno升序、mark降序查詢Score表的所有記錄
mysql> select * from score order by cno asc,mark desc;
+-----+------+------+
| sno | cno | mark |
+-----+------+------+
| 1 | 1 | 99.5 |
| 5 | 1 | 89.5 |
| 6 | 1 | 89.5 |
| 3 | 1 | 85.5 |
| 4 | 1 | 84.5 |
| 2 | 1 | 80.5 |
+-----+------+------+
9.查詢2班的學(xué)生人數(shù)。
mysql> select * from student where class=2;
+-----+-----------+------+------+---------------------+-------+
| sno | sname | sage | ssex | sbirthday | class |
+-----+-----------+------+------+---------------------+-------+
| 4 | 張松濤 | 22 | 1 | 1994-04-22 00:00:00 | 2 |
| 5 | 李暉 | 20 | 0 | 1996-06-22 00:00:00 | 2 |
| 6 | 包政 | 23 | 1 | 1993-04-22 00:00:00 | 2 |
+-----+-----------+------+------+---------------------+-------+
10.查詢”曾志高翔“教師任課的學(xué)生成績。
mysql> select teacher.tname,course.cname,student.sname,score.mark from teacher,course,student,score where student.sno=score.sno and course.cno=score.cno and course.tno=teacher.tno;
+--------+-------+-----------+------+
| tname | cname | sname | mark |
+--------+-------+-----------+------+
| 曾導(dǎo) | linux | 李洋 | 99.5 |
| 曾導(dǎo) | linux | 溫俊林 | 80.5 |
| 曾導(dǎo) | linux | 周恒華 | 85.5 |
| 曾導(dǎo) | linux | 張松濤 | 84.5 |
| 曾導(dǎo) | linux | 李暉 | 89.5 |
| 曾導(dǎo) | linux | 包政 | 89.5 |
+--------+-------+-----------+------+
11.查詢linux課程所有男生的成績并且查出對應(yīng)課程的教師名,職稱,及所在部門。
mysql> select teacher.tname,teacher.prof,teacher.depart,course.cname,student.ssex,score.mark from teacher,course,student,score where student.sno=score.sno and course.cno=score.cno and course.tno=teacher.tno and ssex='1';
+--------+--------+--------+-------+------+------+
| tname | prof | depart | cname | ssex | mark |
+--------+--------+--------+-------+------+------+
| 曾導(dǎo) | 校長 | linux | linux | 1 | 99.5 |
| 曾導(dǎo) | 校長 | linux | linux | 1 | 85.5 |
| 曾導(dǎo) | 校長 | linux | linux | 1 | 84.5 |
| 曾導(dǎo) | 校長 | linux | linux | 1 | 89.5 |
+--------+--------+--------+-------+------+------+
12.把11題查出的成績按照降序排序。
mysql> select teacher.tname,teacher.prof,teacher.depart,course.cname,student.ssex,score.mark from teacher,course,student,score where student.sno=score.sno and course.cno=score.cno and course.tno=teacher.tno and ssex='1' order by mark desc;
+--------+--------+--------+-------+------+------+
| tname | prof | depart | cname | ssex | mark |
+--------+--------+--------+-------+------+------+
| 曾導(dǎo) | 校長 | linux | linux | 1 | 99.5 |
| 曾導(dǎo) | 校長 | linux | linux | 1 | 89.5 |
| 曾導(dǎo) | 校長 | linux | linux | 1 | 85.5 |
| 曾導(dǎo) | 校長 | linux | linux | 1 | 84.5 |
+--------+--------+--------+-------+------+------+
總結(jié)
以上是生活随笔為你收集整理的mysql连接查询作业答案_MySQL连表查询练习题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql istransient_由于
- 下一篇: eplan增加其他到工具栏_EPLAN增