MySQL連表查詢練習題


1.建庫

庫名:linux50 字符集:utf8 校驗規則:utf8_general_ci

create database linux4 charset utf8 default collate = utf8_general_ci;

2.建表

2.1表一

表名:student(學生表)

字段 數據類型要求 是否為空 注釋
sno 最多20位 學號(主鍵)
sname 可變長 學生姓名
sage 最小整數,非負數 學生年齡
ssex 0,1 學生性別(1是男,0是女)默認為男)
sbirthday 時間類型 默認為空 學生生日
class 可變長 學生班級

student表:
create table if not exists student(
sno varchar(21) not null primary key comment '學號',
sname varchar(12) not null comment '學生姓名',
sage tinyint unsigned not null comment '學生年齡',
ssex enum('1','0') default '1' comment '學生性別',
sbirthday datetime comment '學生生日',
class varchar(10) not null comment '學生班級') default charset=utf8;

2.2表二

表名:course(課程表)

字段 數據類型要求 是否為空 注釋
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(成績表)

字段 數據類型要求 是否為空 注釋
sno 最多20位 學號(主鍵)
cno 最多20位 課程號(主鍵)
mark 浮點數(4,1) 成績

注意:sno和cno在另外兩個表中是主鍵,在這里應該是外鍵,不過咱們不需要創建,了解即可

score表:
create table if not exists score(
sno varchar(21) not null primary key comment '學號',
cno varchar(21) not null comment '課程號',
mark float(4,1) not null comment '成績') default charset=utf8;

2.4表四

表名:teacher(教師表)

字段 數據類型要求 是否為空 注釋
tno 最多20位 教師編號(主鍵)
tname 可變長 教師姓名
tage 最小整數,非負數 教師年齡
tsex 0,1 教師性別(1是男,0是女)默認為男)
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;

練習題

#插入數據練習:

1.將自己班級小組所有人員信息插入到student表中(數據自定義)
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.將曾導、徐導、李導信息插入教師表中(數據自定義)
insert into teacher(tno,tname,tage,tsex,prof,depart) 
values('001','曾導',18,1,'校長','linux'),
('002','徐導',19,1,'教學總監','linux'),
('003','李導',20,1,'講師','python');

3.將linux、python學科插入到課程表中(數據自定義)
insert into course(cno,cname,tno) 
values('1','linux','001'),
('2','linux','002'),
('3','python','003');

4.將分數插入到成績表中(數據自定義)
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');

#查詢練習:

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.查詢教師所有的單位即不重復的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表中成績為85.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班或性別為“女”的同學記錄。
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班的學生人數。
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.查詢”曾志高翔“教師任課的學生成績。
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 |
+--------+-------+-----------+------+
| 曾導 | linux | 李洋 | 99.5 |
| 曾導 | linux | 溫俊林 | 80.5 |
| 曾導 | linux | 周恆華 | 85.5 |
| 曾導 | linux | 張松濤 | 84.5 |
| 曾導 | linux | 李暉 | 89.5 |
| 曾導 | linux | 包政 | 89.5 |
+--------+-------+-----------+------+

11.查詢linux課程所有男生的成績並且查出對應課程的教師名,職稱,及所在部門。
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 |
+--------+--------+--------+-------+------+------+
| 曾導 | 校長 | linux | linux | 1 | 99.5 |
| 曾導 | 校長 | linux | linux | 1 | 85.5 |
| 曾導 | 校長 | linux | linux | 1 | 84.5 |
| 曾導 | 校長 | 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 |
+--------+--------+--------+-------+------+------+
| 曾導 | 校長 | linux | linux | 1 | 99.5 |
| 曾導 | 校長 | linux | linux | 1 | 89.5 |
| 曾導 | 校長 | linux | linux | 1 | 85.5 |
| 曾導 | 校長 | linux | linux | 1 | 84.5 |
+--------+--------+--------+-------+------+------+


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM