SQL server 2005 相关操作

CREATE  DATABASE  MyDB ON
( NAME='MyDB_Data', 
  FILENAME='F:\MyDB.mdf',
  SIZE=4MB, MAXSIZE=10MB,  FILEGROWTH=10% )
LOG ON
( NAME='MyDB_Log',
  FILENAME='F:\MyDB_Log.ldf',
  SIZE=4MB, MAXSIZE=5MB, FILEGROWTH=10% );
GO
USE MyDB;
GO
CREATE TABLE Student
 ( Sno CHAR(9) PRIMARY KEY, /*列级完整性约束条件Sno是主码*/
   Sname CHAR(20) UNIQUE,  /*Sname取唯一值*/
   Ssex CHAR(2),
   Sage SMALLINT,
   Sdept CHAR(20)
 );
GO
CREATE TABLE Course
( Cno CHAR(4) PRIMARY KEY,  /*列级完整性约束条件Cno是主码*/
  Cname CHAR(40), 
  Cpno CHAR(4),   /*Cpno为先修课*/
  Ccredit SMALLINT,
  FOREIGN KEY (Cpno) REFERENCES Course(Cno)
);
GO
CREATE TABLE SC
 ( Sno CHAR(9), 
   Cno CHAR(4), 
   Grade SMALLINT,
   PRIMARY KEY (Sno,Cno), /*主码由两个属性组成,必须作为表级完整性来定义*/
   FOREIGN KEY (Sno) REFERENCES Student(Sno),/*表级完整性约束条件:Sno是外码,被参照表是Student*/
   FOREIGN KEY (Cno) REFERENCES Course(Cno)  /*表级完整性约束条件:Cno是外码,被参照表是Course */
 );
GO
INSERT INTO Student VALUES ('200215121','李勇','男',20,'CS');
INSERT INTO Student VALUES ('200215122','刘晨','女',19,'CS');
INSERT INTO Student VALUES ('200215123','王敏','女',18,'MA');
INSERT INTO Student VALUES ('200215125','张立','男',19,'IS');
INSERT INTO Student VALUES ('200215126','张芳','女',18,'CS');
INSERT INTO Student VALUES ('200215127','王平','男',19,'MA');
INSERT INTO Student VALUES ('200215128','赵晓丽','女',19,'IS');
INSERT INTO Student VALUES ('200215129','李婷婷','女',20,'CS');
INSERT INTO Student VALUES ('200215130','刘楠','男',18,'MA');
INSERT INTO Student VALUES ('200215131','赵乐乐','男',21,'CS');
INSERT INTO Student VALUES ('200215132','张三丰','男',19,'IS');


GO
INSERT INTO Course VALUES ('6','数据处理',NULL,2); /*注意插入记录顺序,因先行课Cpno参照课程号Cno,应将先行课先插入到表中*/
GO
INSERT INTO Course VALUES ('4','操作系统','6',3);
GO
INSERT INTO Course VALUES ('7','PASCALL','6',4);
GO
INSERT INTO Course VALUES ('5','数据结构','7',4);
GO
INSERT INTO Course VALUES ('1','数据库','5',4);
INSERT INTO Course VALUES ('2','数学',NULL,2);
INSERT INTO Course VALUES ('3','信息系统','1',4);
GO
INSERT INTO SC VALUES ('200215121','1',92);
INSERT INTO SC VALUES ('200215121','2',85);
INSERT INTO SC VALUES ('200215121','3',88);
INSERT INTO SC VALUES ('200215122','2',90);
INSERT INTO SC VALUES ('200215122','3',80);
INSERT INTO SC VALUES ('200215123','1',88);
INSERT INTO SC VALUES ('200215123','5',92);
INSERT INTO SC VALUES ('200215121','5',89);
INSERT INTO SC VALUES ('200215121','6',93);
INSERT INTO SC VALUES ('200215122','4',96);
INSERT INTO SC VALUES ('200215122','6',85);
INSERT INTO SC VALUES ('200215123','3',91);
INSERT INTO SC VALUES ('200215123','2',90);
INSERT INTO SC VALUES ('200215123','6',87);
INSERT INTO SC VALUES ('200215125','3',50);
INSERT INTO SC VALUES ('200215125','2',68);
INSERT INTO SC VALUES ('200215125','4',87);
INSERT INTO SC VALUES ('200215126','3',85);
INSERT INTO SC VALUES ('200215126','4',75);
INSERT INTO SC VALUES ('200215126','1',77);
INSERT INTO SC VALUES ('200215126','7',82);
INSERT INTO SC VALUES ('200215127','3',51);
INSERT INTO SC VALUES ('200215127','2',62);
INSERT INTO SC VALUES ('200215127','4',81);
INSERT INTO SC VALUES ('200215127','6',70);
INSERT INTO SC VALUES ('200215128','3',50);
INSERT INTO SC VALUES ('200215128','5',40);
INSERT INTO SC VALUES ('200215128','1',82);
INSERT INTO SC VALUES ('200215128','2',98);
INSERT INTO SC VALUES ('200215129','7',72);
INSERT INTO SC VALUES ('200215130','2',83);
INSERT INTO SC VALUES ('200215130','5',45);
INSERT INTO SC VALUES ('200215130','6',70);
INSERT INTO SC VALUES ('200215130','7',62);
INSERT INTO SC VALUES ('200215131','1',55);
INSERT INTO SC VALUES ('200215131','2',62);
INSERT INTO SC VALUES ('200215131','4',88);
INSERT INTO SC VALUES ('200215131','3',80);
INSERT INTO SC VALUES ('200215132','7',52);
INSERT INTO SC VALUES ('200215132','5',60);
INSERT INTO SC VALUES ('200215132','3',86);
INSERT INTO SC VALUES ('200215132','2',98);
INSERT INTO SC VALUES ('200215132','1',42);
GO
SELECT * FROM Student;
SELECT * FROM Course;
SELECT * FROM SC;
GO
/*(1)查询信息工程学院(IS)的所有学生的姓名。
(2)查询学生选课成绩合格的成绩信息。
(3)查询学分为3或4的课程名称。
(4)查询所有课程名称中含有“数据”的课程号。
(5)查询所有姓王的学生信息。
(6)查询所有选课记录的课程号(不重复显示)。
(7)查询成绩为88、90、92分的学生的成绩信息,查询结果按成绩升序排列。
(8)查询成绩在80到90之间(包括80和90分),的学生的成绩信息,查询结果按成绩降序排列。
(9)统计所有学生的平均年龄。
(10)统计学生的总人数。
(11)查询所有选课的学生的学号及其平均成绩,按平均成绩降序排列。
(12)查询所有学生的姓名和出生年份,并给该出生年份属性列命名为出生年份。
(13)查询成绩前3名的学生选课情况。*/
select Sname from Student where Sdept='IS';
GO
select * from Student as one,Course as two,SC as three
where one.Sno=three.Sno and two.Cno=three.Cno
and three.Grade>=60;
GO


select  Sname 姓名,Ssex,Sage,Sdept,Cname,Cpno,Ccredit
 from Student as one,Course as two,SC as three
where one.Sno=three.Sno and two.Cno=three.Cno
and three.Grade>=60;
GO
select Cname from Course where Ccredit=3 or Ccredit=4;
GO
select Cname from Course where Ccredit in(3,4);
GO
select Cno 课程号 from  Course where Cname like '%数据%';
GO


SELECT * FROM Student;
SELECT * FROM Course;
SELECT * FROM SC;
GO
//上次复制到此处
select * from Student where Sname like '王%';
GO
select distinct Cno 课程号 from SC;
GO
select Sno 学号,Cno 课程号,Grade as 筛选成绩 from SC 
where Grade in (88,92,90) order by Grade asc ;
GO
select * from SC where Grade between 80 and 90 order by Grade desc;
GO
select avg (Sage) as 平均年龄 from Student; 
GO
select count(distinct Sname) as 总人数 from Student;
GO
select distinct Sno as 学号,avg(Grade) as 平均成绩
 from SC group by Sno order by 平均成绩 desc;


select Sname, 出生年份=2014-Sage from Student;
GO
select top 3 * from SC  order by Grade desc;
GO


//第三次作业
/*查询所有选课的学生的学号及其平均成绩,按平均成绩降序排列。*/
select Sno 学号,avg(Grade) 平均成绩 from 
SC group by Sno order by 平均成绩 desc;
GO
/*查询每个系的男生和女生的人数,显示系名、性别和对应的人数*/
/*CASE 是SQL 用来做为if-then-else 之类逻辑的关键字。CASE 的语法如下:
SELECT CASE (" 栏位名") WHEN "条件1" THEN " 结果1" WHEN "条件2" THEN " 结果2" ... [ELSE "
结果N"] END
FROM " 表格名"
"条件" 可以是一个数值或是公式。ELSE 子句则并不是必须的 */
select Sdept 系别,sum(case Ssex when '男' then 1 else 0 end) as 男生人数,
sum(case Ssex when '女'  then 1 else 0 end) as 女生人数   from Student
group by Sdept;
/*求出至少被三名学生选修的课程号及其选修的人数*/
select Cno 课程号,count(*)  选修人数 
from SC group by Cno having count(*)>=3;
GO
/*查询至少有两门成绩在85分以上的学生的学号和平均成绩,并按平均分的降序排列。*/
select Sno 学号,avg(Grade) 平均成绩 from SC where Grade>=85 
group by Sno having count(*)>=2  order by 平均成绩  desc;
GO
/*查询至少选修了三门课程的学生学号及其选修的门数*/
select Sno 学号,count(*) 选修门数 from SC group by Sno having count(*)>=3;
GO
/*查询每个学生的学号,其选修的门数,平均分,最高分和最低分*/
select Sno 学号,count(*) 选修门数 ,
max(Grade) 最高分,min(Grade) 最低分,avg(Grade) 平均分
from SC group by Sno;
GO
/*查询选修了“信息系统”课程的学生学号和姓名。*/
select Student.Sno 学号,Sname 姓名 from Student,Course,SC
where Student.Sno=SC.Sno and Course.Cno=SC.Cno
and Course.Cname='信息系统';
GO
/*查询学生的姓名及选修的课程名及成绩。*/
select  Sname 姓名,Cname 课程名,Grade 成绩
from Student,Course,SC 
where Student.Sno=SC.Sno and Course.Cno=SC.Cno


/*查询所有选修数学课程且成绩高于85分的学生
的姓名和对应该课程的课程名和成绩。*/
select Sname 姓名,Cname 该课程,Grade 该成绩
from Student,Course,SC where Student.Sno=SC.Sno
and Course.Cno=SC.Cno and Cname='数学' and
Grade>=85;


/*查询每门课程的课程名,选修人数和平均分*/
select Cname 课程,count(*) 选修人数,avg(Grade) 平均分
from Student,Course,SC where Student.Sno=SC.Sno and
Course.Cno=SC.Cno group by Cname; 


/*查询每个学生选修的门数的平均分,显示学生姓名,
选修门数,平均分数并按选修门数的升序和平均分的降序排序。*/
select Sname 姓名,count(*) 选修门数,avg(Grade) 平均分
from Student,Course,SC where Student.Sno=SC.Sno and
Course.Cno=SC.Cno group by Sname order by 选修门数 asc,
平均分 desc ;
/*查询学号为200215121的学生的姓名和选修的课程名及成绩*/
select Sname 姓名,Cname 课程名,Grade 成绩
from Student,Course,SC where Student.Sno=SC.Sno and
Course.Cno=SC.Cno and SC.Sno='200215121'


/*查询选修“信息系统”且成绩高于90分的学生学号。*/
select Cname 课程名,Student.Sno 学号,Grade 成绩
from Student,Course,SC where Student.Sno=SC.Sno and
Course.Cno=SC.Cno and Cname='信息系统' and Grade>90;


/*查询选修学分为3或4且参加考试的学生学号、课程号*/
select SC.Sno 学号,Cname 课程名
from Student,Course,SC where Student.Sno=SC.Sno and
Course.Cno=SC.Cno and Ccredit in(3,4)


/*分别显示同一个系的男生姓名和女生姓名以及系名*/
select a.Sdept,a.Sname 男,b.Sname 女 from
(select * from Student where Ssex='男') a,
(select * from Student where Ssex='女') b
where a.Sdept=b.Sdept;

你可能感兴趣的:(SQL server 2005 相关操作)