码迷,mamicode.com
首页 > 数据库 > 详细

4.22 数据库 课堂作业

时间:2015-04-22 23:29:32      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

create database s000
create table student
(
sno int  primary key  not null,--学生的学号 number的简称
sname varchar(50) not null,--学生的名字
ssex varchar(50) not null,--学生的性别
sbirthday date ,--学生的出生年月
class varchar(50), --学生所在的班级
)

go
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);

go
select*from student


create table course--创建一个课程表
(
cno varchar(50)primary key  not null,--课程号
cname varchar(50) not null,--课程的名字
tno varchar(50) references teacher(tno) --教工编号 外键受主键约束
)
go
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‘);

go
select *from course


create table score--创建一个成绩表
(
sno int references student (sno) not null,--学号 
cno varchar(50) references course(cno) not null,--课程号
degiee decimal(4,1)--成绩

)
go
insert into score  values(103,‘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(101,‘3-105‘,64);
insert into score  values(107,‘3-105‘,91);
insert into score  values(108,‘3-105‘,78);
alter table score add code int identity(1,1)
alter table score drop column code

go
select *from score
create table teacher
(
tno varchar(50) primary key not null,--创建一个老师的主码
tname varchar(50) not null,--名字
tsex varchar(50) not null,--性别
tbirthday date ,--老师的生日
Prof varchar(50),--教师的职称
depart varchar(50)--教师所在的部门
)
go
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‘,‘助教‘,‘电子工程系‘);
go
select * from teacher
select * from teacher
select * from course
select * from student
select * from score

--1、查询学生表中的名字,性别和班级
select sname ,ssex,class from student 
--2.--把教师表中部门不重复的表现出来
select distinct depart from teacher
--4、 查询Score表中成绩在60到80之间的所有记录
select *from score where degiee between 60 and 80
--5、 查询Score表中成绩为85,86或88的记录。
select *from score where degiee in(85,86,88)
--6、 查询Student表中“95031”班或性别为“女”的同学记录。
select *from student where class=‘95031‘ or ssex=‘女‘
--7、 以Class降序查询Student表的所有记录。
select *from student order by class desc
--8、 以Cno升序、Degree降序查询Score表的所有记录。
select *from score order by cno ,degiee desc
--9、 查询“95031”班的学生人数。  聚合函数;针对数据列,计算求和,或者计算等一系列算数型
select *from student where class=95031
--10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)☆

--select *from score where degiee=92 and cno=‘3-105‘
select top 1 *from score order by degiee desc
--11、 查询每门课的平均成绩。
select AVG(degiee) from score group by cno--group by 分组 avg 求平均分


--12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
 
select cno, avg(degiee) as xxx from score where cno like‘3%‘ group by cno having count(*)>=3 order by xxx
--13、查询分数大于70,小于90的Sno列。
select sno from score where degiee between 70 and 90--找出四个表中分数大于70,小于90的分数

--14、查询所有学生的Sname、Cno和Degree列。
--select *from score
--select sname ,cno,degiee from student ,score where student.sno=score.sno--笛卡尔积方法,一定要是主外键
--用主外键关系进行筛选,用逗号分开两个表,形成笛卡尔积方法,进行where筛选 
--笛卡尔积  用两个新表合成一个表.   十条以上不要用笛卡尔积
select sname,cno,degiee from score
join student on score.sno=student.sno
--join ...on...

--15、查询所有学生的Sno、Cname和Degree列。
select *from score
select *from course
select sno,cname,degiee from score
join course  on score.cno=course.cno


--16、查询所有学生的Sname、Cname和Degree列。
select *from score--找出列的表
select *from student
select *from course
select sname,cname,degiee from score
join student on student.sno=score.sno
join course on course.cno=score.cno




--17、 查询“95033”班学生的平均分。
select avg(degiee)as ‘平均分‘ from student,score where class=‘95033‘

 

4.22 数据库 课堂作业

标签:

原文地址:http://www.cnblogs.com/w-wz/p/4448851.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!