SQL server 数据库练习题及答案(练习1)

使用你的名字创建一个数据库

创建表:

班级表(clazz)

数据类型

约束等

字段描述

int

主键,自增

班级序号

varchar(32)

唯一键,不能为空

班级名称

varchar(32)

可空

班主任姓名

varchar(1024)

可空

班级简介

学生表(student)

字段名称

数据类型

约束等

字段描述

id

int

主键,自增

学生序号

name

varchar(32)

不能为空

学生姓名

sex

varchar(8)

不能为空,默认男

学生性别

age

int

不为空,年龄在(18,50]之间

学生年龄

clazz_id

int

外键(关联clazz表中的id字段)

所在班级序号

stu_num

varchar(32)

唯一键

学号

课程表(course)

字段名称

数据类型

约束等

字段描述

id

int

主键.自增

课程序号

name

varchar(32)

非空

课程名称

grade_point

float

非空

课程的学分绩点

description

varchar(1024)

非空

课程简介

选课表(selection)

字段名称

数据类型

约束等

字段描述

id

int

主键.自增

选课序号

course_id

int

外键(关联course表中的id字段)

外键,课程序号

student_id

int

外键(关联student表中的id字段)

外键,学生序号

score

float

成绩大于0

成绩

问题:

1:使用SQL语句完成数据库、表、约束的创建(create.sql)

2:使用SQL给每张表添加至少5条数据

3:查询出所有的学生信息并给每列设置中文列名

4:查询所有班级信息并将班级名称、班主任姓名两个字段按照“班级名称(班主任姓名)”显示

5:查询出班级中的所有班主任姓名,要求不能有重复名字

6:查询出前3名学生信息

7:查询出前15%学生信息

8:查询出性别为“男”的学生信息

9:查询出姓名不是“张三”的学生信息

10:查询出序号在10-30之间的学生信息

11:查询出序号不在10-30之间的学生信息

12:查询出序号为 1,3,4,5,8,9的学生信息

13:查询出学生序号为2并且课程序号为3的成绩

14:查询出学生姓名里包含“国”的学生信息

15:按照成绩降序输出成绩表中课程序号为3的所有信息

16:按照成绩升序输出成绩表中学生序号为2的所有信息

17:按照成绩降序、课程序号升序查询出成绩表中学生序号为1的所有信息

3:查询出课程序号为2的总分

4:查询出课程序号为2的平均分

5:查询出成绩表中的最高分

6:查询出成绩表中的最低分

7:查询出成绩表中课程序号为2的最高分

8:查询出成绩表中课程序号为2的最低分

9:查询出参加考试的人数

10:查询每个课程参加考试的人数

11:统计平均分中大于60的课程序号,并显示出平均成绩。

答案:

/*创建数据库*/
create database gaohongyuan
on primary 
(name="gaohongyuan_data",
filename="c:\gao\gaohongyuan_data.mdf", 
size=8MB,
maxsize=100MB, 
filegrowth=10%) 
log on 
(name="gaohongyuan_log.ldf", 
filename="c:\gao\gaohongyuan_log.ldf", 
size=1MB, 
filegrowth=10%)


use gaohongyuan/*切换数据库*/

/*创建班级表*/
create table clazz(
cla_id int identity(1,1) primary key not null,
cla_name varchar(32) unique not null,
cla_tea_name varchar(32),
cla_intro varchar(1024)
)
/*创建学生表*/
create table student(
stu_id int identity(10000,1) primary key not null,
stu_name varchar(32) not null,
stu_sex varchar(8) default('男') not null,
stu_age int check(stu_age>=18 and stu_age<=50),
cla_id int references clazz(cla_id),
stu_num varchar(32) unique not null
)
/*创建课程表*/
create table course(
course_id int identity(1,1) primary key not null,
cou_name varchar(32) not null,
grade_point float not null,
descripion varchar(1024) not null
)
/*创建选课表*/
create table selection(
sele_id int identity(1,1) primary key not null,
course_id int references course(course_id),
stu_id int references student(stu_id),
score float check(score>0)
)

/*插入数据*/
select * from clazz/*查询语句*/
insert clazz values
('班级1','张强','一班'),
('班级2','李华','二班'),
('班级3','小明','三班')

select * from student/*查询语句*/
insert  values
('张三','男','21','1','s001'),
('李四','男','20','1','s002'),
('王五','男','19','1','s003'),
('赵六','女','22','1','s004'),
('刘七','女','21','1','s005'),
('陈八','男','20','2','s006'),
('政九','女','19','2','s007'),
('孙十','女','22','2','s008'),
('冯十一','女','21','2','s009'),
('吴十二','男','22','3','s010')

select * from course/*查询语句*/
insert course values
('数学','4.0','数学课程'),
('英语','3.5','英语课程'),
('语文','4.0','语文课程')

select * from selection/*查询语句*/
insert selection(stu_id,course_id,score) values
(10000,1,'78'),
(10000,2,'89'),
(10000,3,'90'),
(10001,1,'45'),
(10001,2,'67'),
(10001,3,'90'),
(10002,1,'98'),
(10002,2,'99'),
(10002,3,'100'),
(10003,1,'98'),
(10003,2,'92'),
(10003,3,'90'),
(10004,1,'11'),
(10004,2,'24'),
(10004,3,'16'),
(10005,1,'67'),
(10005,2,'56'),
(10005,3,'66'),
(10006,1,'45'),
(10006,2,'67'),
(10006,3,'89'),
(10007,1,'90'),
(10007,2,'89'),
(10007,3,'97'),
(10008,1,'76'),
(10008,2,'87'),
(10008,3,'88'),
(10009,1,'54'),
(10009,2,'62'),
(10009,3,'61')

select stu_name 姓名,stu_sex 性别,stu_age 年龄,cla_id 班级序号, stu_num 学号 from student
select cla_name + cla_tea_name '班级名称(班主任姓名)' from clazz
select distinct cla_tea_name from clazz
select top 3 stu_id 学号,score 成绩 from selection order by score desc
select top 15 percent stu_id 学号,score 成绩 from selection order by score desc
select * from student where not stu_sex='男'
select * from student where stu_name!='张三'
select * from student where stu_id between 10003 and 10006
select * from student where stu_id not in(10003,10004,10005,10006)
select * from student where stu_id in(10001,10003,10005,10008,10009)
select * from selection where stu_id=10002 and course_id=3
select * from student where stu_name like '%十%'
select * from selection where course_id=3  order by score desc
select * from selection where course_id=2 order by score asc
select * from selection where stu_id=10001 order by score desc,course_id asc
select sum(score) from selection where course_id=2
select AVG(score) from selection where course_id=2
select MAX(score) from selection 
select MIN(score) from selection
select MAX(score) from selection where course_id=2
select min(score) from selection where course_id=2
select course_id, count(stu_id) from selection group by course_id
select course_id,avg(score) from selection group by course_id having avg(score)>60

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
SQL Server数据库练习题是一组练习,旨在帮助学习者熟悉SQL Server数据库的基本操作和概念。这些练习包括创建数据库、设计表、填空题和选择题等形式。引用 在SQL Server中,数据库是用来存储和管理数据的。数据可以以关系型的方式组织,并通过表的设计来表示不同的实体和关系。引用 数据库管理系统(DBMS)是用来管理和操作数据库的软件系统。SQL Server是一种常用的关系型数据库管理系统,它提供了丰富的功能和语言来操作数据库对象,如表、视图、存储过程等。引用 数据库管理员(DBA)是负责管理和维护数据库系统的专业人员。他们负责监控数据库的性能、备份和恢复数据、优化查询等任务。 数据库系统是一个包含数据库数据库管理系统和相关应用程序的完整系统。它提供了数据存储、数据管理和数据处理等功能,帮助用户有效地组织和利用数据。 练习题的目的是让学习者通过实践来掌握SQL Server数据库的基本操作和概念,以便能够更好地应用和管理数据库系统。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Sql Server 数据库练习题(一)](https://blog.csdn.net/weixin_43960383/article/details/124815520)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SQL server练习题](https://blog.csdn.net/qq_42418169/article/details/105948470)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GHY我与春风皆过客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值