首页 > 技术文章 > SQLserver数据库作业

xgcl 2022-05-04 20:46 原文

任务要求

  1. 用SQL语句在学生数据库Student中创建数据表StudentInfo,属性包括:学号,姓名,性别,身高,年级,成绩。指定主键为学号,且为唯一值。各个字段都不能为空。

  2. 在数据表StudentInfo里面填写不少于10条记录。

  3. 用SQL语句在学生表中进行查询:

    1. 分性别统计人数和平均身高;
    2. 分年级统计平均分75分以上的年级学生人数和平均成绩;

完整代码

create database Student
use Student
create table StudentInfo
(
	学号 char(20) not null primary key,
	姓名 char(8) not null,
	性别 char(2) not null,
	身高 float not null,
	年级 char(10) not null,
	成绩 float not null
)
insert into StudentInfo values
('201933036016','柴妍暄','女','165','18通信','67'),
('201933036017','车楠贵','男','178','20通信','56'),
('201933036018','周会','女','168','19通信','78'),
('201933036019','刘良航','男','176','20通信','87'),
('201933036010','丘光国','男','156','19通信','79'),
('201933036011','蒋辰博','男','169','21通信','90'),
('201933036012','马思','男','170','20通信','67'),
('201933036013','殷栋之','女','175','20通信','87'),
('201933036014','丁广','女','174','21通信','86'),
('201933036024','丁一广','男','174','21通信','86'),
('201933036034','丁二广','女','174','21通信','89');
select count('学号') as 人数,avg(身高) as 平均身高,性别 from StudentInfo group by 性别
select count(学号) as 人数,avg(成绩) as 平均成绩,年级 from StudentInfo group by 年级 having avg(成绩)>75

任务要求

  1. 用SQL语句在图书销售数据库中创建图书、售书网站、售书三个数据表StudentInfo,各个表的属性如下:
    • 图书(图书编号,书名,出版社,价格,出版时间)
    • 售书网站(售书网站编号,名称,所在城市,成立时间)
    • 售书(图书编号,售书网站编号,销量)
  2. 在售书网站和图书数据表中各填写不少于5条记录,其中售书网站的记录中至少要包含名称为当当的。售书表填写不少于10条记录。
  3. 用SQL语句完成查询:
    1. 查询当当销售的书名和总售价
    2. 汇总各个售书网站的总销量,分别显示为售书网站名称,销量
    3. 汇总各个售书网站的50元以上的书的总销量,分别显示为售书网站名称,销量

完整代码

create database 图书销售
use 图书销售
create table 图书(
	图书编号 char(30) not null,
	书名 char(40) not null,
	出版社 char(20) not null,
	价格 float not null,
	出版时间 datetime not null,
)
create table 售书网站(
	售书网站编号 char(30) not null,
	名称 char(20) not null,
	所在城市 char(20) not null,
	成立时间 datetime not null,
)
create table 售书(
	图书编号 char(30) not null,
	售书网站编号 char(30) not null,
	销量 int not null,
)
insert into 售书网站 values
('dangdang','当当','北京','1999-11-01'),
('taobao','淘宝','杭州','1978-03-25'),
('jingdong','京东','北京','1996-03-26'),
('kongfuzi','孔夫子旧书网','南京','1996-01-26'),
('pinduoduo','拼多多','上海','2006-03-06');
insert into 图书 values
('ISBN-123456','《Hadoop海量数据处理》','人民邮电出版社','45','2019-08-07'),
('ISBN-123426','《数据库原理及应用》','机械工业出版社','52.3','2014-03-07'),
('ISBN-133456','《现场总线及其应用技术》','机械工业出版社','26.9','2012-08-27'),
('ISBN-143456','《物联网安全导论》','清华大学出版社','65','2019-02-07'),
('ISBN-127456','《计算机网络》','电子工业出版社','55.7','2016-03-02');
insert into 售书 values
('ISBN-123456','dangdang',63),
('ISBN-123426','dangdang',232),
('ISBN-127456','taobao',45),
('ISBN-143456','dangdang',263),
('ISBN-133456','pinduoduo',35),
('ISBN-123456','taobao',63),
('ISBN-123456','kongfuzi',462),
('ISBN-127456','kongfuzi',242),
('ISBN-123456','jingdong',124),
('ISBN-143456','kongfuzi',143);
select * from 图书
select * from 售书网站
select * from 售书
select 图书.书名, 图书.价格*售书.销量 as 总售价 from 售书,图书 where 售书网站编号='dangdang' and 图书.图书编号=售书.图书编号
select 售书网站.名称 as 售书网站名称, sum(售书.销量) as 总销量 from 图书,售书网站,售书 where 图书.图书编号=售书.图书编号 and 售书网站.售书网站编号=售书.售书网站编号 group by 售书网站.名称
select 售书网站.名称 as 售书网站名称, sum(售书.销量) as 总销量 from 图书,售书网站,售书 where 图书.价格>50 and 图书.图书编号=售书.图书编号 and 售书网站.售书网站编号=售书.售书网站编号 group by 售书网站.名称

推荐阅读