日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql连接查询作业答案_MySQL连表查询练习题

發(fā)布時間:2025/4/16 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql连接查询作业答案_MySQL连表查询练习题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。