当前位置:   article > 正文

SQL的学习学生表_sql数据库期末大作业学生表

sql数据库期末大作业学生表

1.创建数据库

create database school;

2.创建学生表,成绩表,课程表

  1. create table student(
  2. Sno int primary key not null,
  3. Sname varchar(20) not null,
  4. Ssex varchar(2) null,
  5. Birthday datetime null,
  6. Sdept varchar(20) null);

 

  1. create table course(
  2. Cno int primary key not null,
  3. Cname varchar(40) null,
  4. Cpno int null,
  5. Credit dec(3,1) null,
  6. foreign key(Cpno) references course (Cno));
  1. create table SC(
  2. Sno int not null,
  3. Cno int not null,
  4. Grade dec(4,1) null,
  5. foreign key(Sno) references student (Sno),
  6. foreign key(Cno) references course(Cno),
  7. );

3.插入数据(举个例子)格式如下

  1. insert
  2. into SC(Sno,Cno,Grade)
  3. values('202215129','1005',59);

 4.修改数据(格式如下)

    例子:把202215129的1005课程分数改成99分。

  1. update SC
  2. set Grade=99
  3. where Sno in
  4. (
  5. select Sno
  6. from student
  7. where Sno='202215129'
  8. );

   结果,全部分数都变成99分了。

 修改:(加上限制条件课程号即可)

  1. update SC
  2. set Grade=98
  3. where Cno='1005'and Sno in
  4. (
  5. select Sno
  6. from student
  7. where student.Sno='202215129'
  8. );

 5.修改基本表(格式)

  1. alter table student
  2. drop column address;

例子1,删除多余的列address

原来的图片:

 运行后:

 例子2:在学生表中增加入学时间

  1. alter table student
  2. add S_entrance date;

6.删除数据(打个样)

  1. Delete student
  2. where SSex is Null

7.单表查询

例子1:计算并查询学生年龄大于20岁的人

  1. select Sname 姓名,Birthday , YEAR(GETDATE())-YEAR(Birthday) as age
  2. from student
  3. where age>20;

例子2:计算学生年龄并且排序

  1. select Sname 姓名,Birthday , YEAR(GETDATE())-YEAR(Birthday) as age
  2. from student
  3. order by age desc;

结果:

 例子3:查询成绩不合格学生以及学生的成绩

  1. select distinct student.Sno 学号,Sname,Cno 课程号,Grade 分数
  2. from student,SC
  3. where student.Sno=SC.Sno and Grade<60;

结果:

例子4:查询选择全部课程的学生(格式打个样,两种思路)

  1. select SC.Sno,Sname
  2. from SC inner join student on SC.Sno=student.Sno
  3. group by Sname,SC.Sno
  4. having count(Cno)=(select count(*)from course)

  1. select * from student where Sno in(
  2. select Sno from SC
  3. group by Sno
  4. having count(Cno)=(select count(*)from course))

 8.连接查询(注意粉红色部分的格式)

例子1:查询所有学生学号、姓名、性别、及总学分

  1. select student.Sno,student.Sname,Ssex,sum(credit) as 总学分
  2. from student
  3. inner join SC on SC.Sno=student.Sno
  4. inner join course on SC.Cno=course.Cno
  5. group by student.Sno,student.Sname,Ssex;

 例子2:查询各科选修人数,课程最高分,最低分,及格人数,不及格人数,平均分

  1. select course.cno,cname,count(sno) as 选课人数,MAX(GRADE) AS 最高分,min(grade) as 最低分,avg(grade) as 平均分,
  2. sum(case when Grade >60 then 1 else 0 end) as 及格人数,
  3. sum(case when Grade <60 then 1 else 0 end) as 不及格人数
  4. from course left join SC on course.cno=SC.cno
  5. group by course.cno,cname

9.嵌套查询

例子:查询计算机学院(CS)比数学学院(MA)某一个同学年龄小的学生(出生日期越大,年龄越小)

  1. select * from student
  2. where Sdept='CS' and Birthday > any(select Birthday from student where Sdept='MA')
  1. select * from student
  2. where Sdept='CS' and Birthday > (select min(Birthday) from student where Sdept='MA')

10.视图创建(格式如下)

create view Vgrade as

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/516608
推荐阅读
相关标签
  

闽ICP备14008679号