练习题2

数据准备

1.1数据说明

做作业之前需要执行”ch1.2建表与数据插入”中的SQL语句;

建立相关表后才能做作业;


1.2建表与数据插入

/*建学生信息表student*/

drop table if exists student;

create table student

(

sno varchar(20) not null primary key,/*学生编号*/

sname varchar(20) not null,/*学生姓名*/

ssex varchar(20) not null,/*学生性别*/

sbirthday datetime,/*学生生日*/

class varchar(20)/*班级*/

);


/*建立教师表 */

drop table if exists teacher;

create table teacher

(

tno varchar(20) not null primary key,/*教师编号 */

tname varchar(20) not null,/*教师名称*/

tsex varchar(20) not null,/*教师性别*/

tbirthday datetime,/*教师生日*/

prof varchar(20),/*教师职称*/

depart varchar(20) not null/*教师部门*/

);


/*建立课程表course */

drop table if exists course;

create table course

(

cno varchar(20) not null primary key,/*课程编号 */

cname varchar(20) not null,/*课程名称*/

tno varchar(20) not null/*教师编号*/

);


/*建立成绩表*/

drop table if exists score;

CREATE TABLE score (

sno VARCHAR (20) NOT NULL ,/*学生编号 */

cno VARCHAR (20) NOT NULL,/*课程编号 */

degree DECIMAL /*成绩 */

);


/*添加学生信息*/

insert into student values('108','曾华','男','1977-09-01','95033');

insert into student values('105','匡明','男','1975-10-02','95031');

insert into student values('107','王丽','女','1976-01-23','95033');

insert into student values('101','李军','男','1976-02-20','95033');

insert into student values('109','王芳','女','1975-02-10','95031');

insert into student values('103','陆君','男','1974-06-03','95031');


/*添加教师信息 */

insert into teacher values('804','李诚','男','1958-12-02','副教授','计算机系');

insert into teacher values('856','张旭','男','1969-03-12','讲师','电子工程系');

insert into teacher values('825','王萍','女','1972-05-05','助教','计算机系');

insert into teacher values('831','刘冰','女','1977-08-14','助教','电子工程系');

insert into teacher values('859','张文','男','1968-10-14','教授','计算机系');


/*添加课程信息 */

insert into course values('3-105','计算机导论','825');

insert into course values('3-245','操作系统','804');

insert into course values('6-166','数字电路','856');

insert into course values('9-888','高等数学','831');


/*添加成绩信息 */

insert into score values('108','3-245','86');

insert into score values('105','3-245','75');

insert into score values('109','3-245','68');

insert into score values('103','3-105','92');

insert into score values('105','3-105','88');

insert into score values('109','3-105','76');

insert into score values('103','3-105','64');

insert into score values('105','3-105','91');

insert into score values('109','3-105','78');

insert into score values('103','6-166','85');

insert into score values('105','6-166','79');

insert into score values('109','6-166','81');


1.3表和字段说明

学生表student:

        学生表的sno字段与成绩表的sno字段关联

教师表teacher:

        教室表的tno字段与课程表的tno字段关联

课程表course:

        课程表的cno字段与成绩表的cno字段关联

        课程表的tno字段与教师表的tno字段关联

成绩表score:

        成绩表的sno字段与学生表的sno字段关联

        成绩表的cno字段与课程表的cno字段关联


作业

2.1teacher表增加新记录,信息如下:

tno:'861',

tname:'刘羽',

tsex:'女',

tbirthday:'1978-09-14',

prof:'助教',

depart:'电子工程系'

答案:

insert into teacher values('861', '刘羽', '女', '1978-09-14', '助教', '电子工程系');


2.2查询student表中的所有记录的sname、ssex和class列。

答案:

select sname, ssex, class from student;


2.3查询teacher表中的部门depart,要求不能显示重复值。

答案:

select distinct depart from teacher;


2.4查询score表中成绩degree在60到80之间的所有记录。

答案:

select * from score where degree between 60 and 80;


2.5将student表中姓名sname"王丽"的学生改名"王美丽"。

答案:

update student set sname = '王美丽' where sname = '王丽';


2.6查询score表存在有85分以上成绩degree的课程编号cno。

答案:

select cno from score where degree >= 85;


2.7查询score表中成绩degree为85,86或88的记录。

答案:

select * from score where degree in (85,86,88);


2.8查询student表中“95031”班class或性别ssex为“女”的同学记录。

答案:

select * from student where class = '95031' or ssex = '女';


2.9删除teacher表中教师编号tno等于'861'的记录。

答案:

delete from teacher where tno = '861';


2.10以class降序查询student表的所有记录。

答案:

select * from student order by class desc;


2.11以cno升序、degree降序查询score表的所有记录。

答案:

select * from score order by cno, degree desc;


2.12查询student表中class为“95031”的学生总人数。

答案:

select count(*) from student where class = '95031';


2.13查询student表中“95033”班和“95031”班全体学生的记录。

答案:

select * from student where class in ('95033','95031');


2.14查询student表中不姓“王”的同学记录。

答案:

select * from student where not sname like '王%';


2.15以年龄从小到大的顺序查询student表中的全部记录。

答案:

select * from student order by sbirthday desc;


2.16查询score表中每门课的平均成绩。

答案:

select cno, avg(degree) from score group by cno;

知识点:

数据分组:

select 聚合函数 from 表名 group by 字段;

select 聚合函数 from 表名 where 条件 group by 字段;

group by就是配合聚合函数使用的

where和group by 和order by的顺序

select * from 表名 where 条件 group by 字段 order by 字段


2.17查询score表中分数degree大于70,小于90的sno列。

答案:

select sno from score where degree> 70 and degree< 90;

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容