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

数据库作业

时间:2015-04-22 13:32:45      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

create database 作业
use 作业
--创建学生表
create table Student
(
Sno char(3) primary key  not null,
Sname char(8) not null,
Ssex char(2) not null,
Sbirthday datetime ,
Class char(5)
)

--创建课程表
create table Course
(
Cno char(5) primary key not null,
Cname char(10) not null,
Tno char(3) references Teacher(Tno) not null
)

--创建分数表
create table Score
(
Sno char(3) references Student(Sno) not null,
Cno char(5) references Course(Cno)not null,
Degree decimal(4,1)
)

--创建老师表
create table Teacher
(
Tno char(3) primary key not null,
Tname char(4) not null,
Tsex char(2) not null,
Tbirthday datetime ,
Prof char(6),
Depart varchar(10) not null
)

--给Student插入一系列数据
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)
update Student set Sbirthday=1977-09-01 where Sbirthday=1977-09-01
update Student set Sbirthday=1975-10-02 where Sbirthday=1975-10-02
update Student set Sbirthday=1976-01-23 where Sbirthday=1976-01-23
update Student set Sbirthday=1976-02-20 where Sbirthday=1976-02-20
update Student set Sbirthday=1975-02-10 where Sbirthday=1975-02-10
update Student set Sbirthday=1974-06-03 where Sbirthday=1974-06-03
--给Course 插入一系列数据
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)

--给Score插入一系列数据
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)
insert into Score values(101,6-166,85)
insert into Score values(107,6-166,79)
insert into Score values(108,6-166,81)

--给Teacher 插入一系列数据
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,助教,电子工程系)
update Teacher set Tbirthday =1958-12-02 where Tbirthday=1958-12-02
update Teacher set Tbirthday =1969-03-12 where Tbirthday=1969-03-12
update Teacher set Tbirthday =1972-05-05 where Tbirthday=1972-05-05
update Teacher set Tbirthday =1977-08-14 where Tbirthday=1977-08-14
select *from Teacher 
select *from Student
select *from Course
select *from Score

--①查询Student表中的所有记录的Sname、Ssex和Class列。
select Sname,Ssex,Class from Student 
--②查询教师所有的单位即不重复的Depart列。
select distinct Depart from Teacher
--③查询Student表的所有记录。
select * from Student
--④ 查询Score表中成绩在60到80之间的所有记录。
select *from Score where Degree between 60 and 80
--⑤ 查询Score表中成绩为85,86或88的记录。
select *from Score where Degree in(85,86,88)
--⑥查询Student表中“95031”班或性别为“女”的同学记录。
select*from Student where Class=95031 or Ssex=
--⑦ 以Class降序查询Student表的所有记录。
select *from Student order by Class desc
--⑧以Cno升序、Degree降序查询Score表的所有记录。
select *from Score order by Cno asc ,Degree desc
--⑨查询“95031”班的学生人数。--聚合函数:针对数据列,计算求和或者计数等一系列算术性操作
select COUNT(*) from Student where Class=95031  --count对计算的结果筛选个数,条数,返回一个数值
--sum(), avg(),max(),min()  括号里面放的是列 ,只有count(*)的括号里面放*
--查一下3-105的最高分  as:起别名
select MAX(degree)as maxfen from Score where Cno=3-105
--10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
select sno,cno from score where degree = (select MAX(degree) from Score )
--排序的方法 select top 1 *from Score order by Degree desc
--11、 查询每门课的平均成绩。 
select AVG(degree) as pingjunfen,cno from Score group by Cno --当分组和聚合结合的时候,先分组,然后对每一组分别进行聚合
--12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数,并且将成绩这一列排序
select cno,AVG(degree)as degress from Score where Cno like 3% group by Cno having COUNT(*)>=5 order by degress
--13、查询分数大于70,小于90的Sno列。
Select Sno from Score where Degree >70 and Degree <90
--14、查询所有学生的Sname、Cno和Degree列。  --表链接
--逗号隔开查两个表,形成笛卡尔积,然后进行where筛选,通过两个表主外键关系筛选
--select sname,cno,degree from Student,Score where student.sno=Score.sno
--正规方法
select sname,Cno,degree from Score  join Student on Score.Sno=Student.Sno
--inner left(把左表全部显示全,右表有关系的显示出来,没关系的显示为空) right 
select Student.Sno,sname,ssex,Student.Class,Score.Sno,Score.Cno,Score.Degree from Student left join Score on Student.Sno=Score.Sno
--15、查询所有学生的Sno、Cname和Degree列。
select score.Sno,cname,degree from Score join Course on score.Cno=Course.Cno
--16、查询所有学生的Sname、Cname和Degree列。
select sname,cname,degree from Score join student on Student.Sno=Score.Sno  join Course on Course.Cno=Score.Cno

 

数据库作业

标签:

原文地址:http://www.cnblogs.com/lk-kk/p/4446891.html

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