我是靠谱客的博主 默默小鸽子,最近开发中收集的这篇文章主要介绍用字典存储学生成绩查询_用SQL创建学生成绩数据库,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

  1. 创建数据库school,这个数据库中包含四个表:分别是学生表、教师表、课程表和成绩表。

9827fa4b337ea170e579d30385aa0e7b.png
语法:
create database school;(创建数据库school)
show databases;(查看是否已经创建好)
drop database school;(删除数据库school,这里不必删除)

2.设计创建学生表、教师表、课程表和成绩表。

d0add1d028b8aa04ef37285e2e0debd7.png
语法:
 use school;
    create table student(
                  sno varchar(20) not null,
                  sname varchar(20) not null,
                  ssex varchar(20) not null default'男',
                  sbirthday datetime,
                  sclass varchar(20));

f04db1ad808532ee76d77d120404c4b8.png
语法:
use school;
    create table teacher(
               tno varchar(20) not null,
               tname varchar(20) not null,
               tsex varchar(20) not null default'男',
               tbirthday datetime,
               prof varchar(20) not null,
               depart varchar(20));

114d3a61b547f1208ba82c7cfe364046.png
语法:
use school;
create table course(
             cno varchar(20) not null,
             cname varchar(20) not null,
             tno varchar(20) not null);

307e27a4dc04aebf87d3e944a3470303.png
语法:
use school;
   create table score(
sno varchar(20) not null, 
cno varchar(20) not null,
              degree decimal(4,1) not null);

显示所有表是否已经创建,可用以下语句:

Show tables from school;

9123ed5ee34e30daf6a9de0bd67f34e1.png

3.添加主键和外键约束

2fbcedabb4b39ad1ff7ce37a9a273b73.png
语法:

对学生表添加主键约束:

alter table student
add constraint primary key(sno);

对成绩表添加外键约束:

alter table student
add constraint foreign key(sno)
references student(sno);
alter table student
add constraint foreign key(cno)
references course(cno);

对成绩表添加主键约束:

Alter table student
Add constraint primary key(sno,cno);

用语法将以上的主键和外键都添加约束:

alter table student
add constraint primary key(sno);
alter table teacher
   add constraint primary key(tno);
 alter table course
  add constraint primary key(cno);
 alter table course
     add constraint foreign key(tno)
     references teacher(tno);
alter table score
  add constraint primary key(sno,cno);
alter table score
   add constraint foreign key(cno)
   references course(cno);

4.将以下学生信息插入到学生表中:

b04f92c18f2bd4c7b23933c7f6396a1d.png
语法:
insert into student(sno,sname,ssex,sbirthday,sclass)
      values(108,'曾华','男','1997-09-01',95033);
 insert into student(sno,sname,ssex,sbirthday,sclass)
      values(105,'匡明','男','1975-10-02',95031);
 insert into student(sno,sname,ssex,sbirthday,sclass)
      values(107,'王丽','女','1976-01-23',95033);
insert into student(sno,sname,ssex,sbirthday,sclass)
     values(101,'李军','男','1976-02-20',95033);
insert into student(sno,sname,ssex,sbirthday,sclass)
     values(109,'王芳','女','1975-02-10',95031);
 insert into student(sno,sname,ssex,sbirthday,sclass)
     values(103,'陆君','男','1974-06-03',95031);
select*from student;(查看是否所有信息都插入表中了)

结果如下:

69d72250e65ec7fea948a810b5be48ca.png

5.将以下信息插入到教师表中:

5c6ba81bfd51562cae32b362dbbed817.png
语法:
insert into teacher(tno,tname,tsex,tbirthday,prof,depart)
   values(804,'李诚','男','1958-12-02','副教授','计算机系');
insert into teacher(tno,tname,tsex,tbirthday,prof,depart)
values(856,'张旭','男','1969-03-12','讲师','电子工程系');
insert into teacher(tno,tname,tsex,tbirthday,prof,depart)
    values(825,'王萍','女','1972-05-05','助教','计算机系');
insert into teacher(tno,tname,tsex,tbirthday,prof,depart)
values(831,'刘冰','女','1977-08-14','助教','电子工程系');

6.以下信息输入课程表表中

40725a78f981b7a62155c8bfaffaf93f.png
语法:
insert into course(cno,cname,tno)
   values('3-105','计算机导论',825);
insert into course(cno,cname,tno)
   values('3-245','操作系统',804);
insert into course(cno,cname,tno)
  values('6-166','数据电路',856);
 insert into course(cno,cname,tno)
    values('19-888','高等数学',831);

7.将以下信息输入成绩表中:

d51a74acde4eb7f29e58d5065dc66d2d.png
语法:
insert into score(sno,cno,degree)
values(103,'3-245',86);
 insert into score(sno,cno,degree)
   values(105,'3-245',75);
 insert into score(sno,cno,degree)
   values(109,'3-245',68);
 insert into score(sno,cno,degree)
   values(103,'3-105',92);
 insert into score(sno,cno,degree)
   values(105,'3-105',88);
 insert into score(sno,cno,degree)
   values(109,'3-105',76);
 insert into score(sno,cno,degree)
   values(101,'3-105',64);
 insert into score(sno,cno,degree)
   values(107,'3-105',91);
 insert into score(sno,cno,degree)
   values(108,'3-105',78);
 insert into score(sno,cno,degree)
   values(101,'6-166',85);
 insert into score(sno,cno,degree)
   values(107,'6-166',79);
insert into score(sno,cno,degree)
   values(108,'6-166',81);

8.要求查询学生表中的sname、ssex和sclass列。

语法:
select sname,ssex,sclass
         from student;

89df473d374f1b7b38ded4fdd9b0e1f3.png

9.查询学生表中的所有记录。

语法:

Select*from student;

结果如下:

d534428f0f7e71692c1b5a28df107f3b.png

10.查询教师表中不同的系名,以及每个系有多少老师。(重点)

分析:

  • 按系名分组(group by)
  • 求每组有多少老师,即每组有几行?(count)
语法:
Select depart,count(*)
From teacher
Group by depart

2ec53ddcddd02d8c228e549c4b16bf0f.png

11.查询成绩表中分数在60分到80分之间的所有记录

分析:

  • 查询成绩表中的某些记录
  • 这些记录一定满足60分到80分
语法:
Select*from score
Where degree  between 60 and 80;

6db3cfb8b428eaabfe3be55914b9cbb1.png

12.查询成绩表中分数为85,86或88的记录。

分析:

  • 查询成绩表中的某些记录
  • 这些记录应满足成绩为85,86或88

这里用where子句:

方法a:使用关键字in

方法b:使用关键字or

语法a:
Select*
from score
Where degree in(85,86,88);

f190e43c22bc1f4ba6b89f9bb55bbe76.png
语法b:
Select*
from score
Where degree =85 or degree =86 or degree =88;

1ad519275f3b0c1a2b4ccb018fd53733.png

注:这里的or表示并且,不能用and(and表示交集)

13.查询学生表中“95031”班或性别为“女”的记录。

分析:

  • 查询某些同学的记录
  • where子句筛选符合条件的行
语法a:
select*
from student
where sclass='95031' or ssex='女';

7a52c43cf4d5cab148cb2b68e7b58089.png
语法b:
  • 查询95031这个班级的所有同学的记录
  • 查询性别为女的所有记录
  • 用关键字union连接以上的查询语句。
select*
from student 
where sclass='95031'
union
select*
from student 
where ssex='女';

4ddafcad197f78a1f17762ffcc08fa6c.png

14.查询所有不姓王的同学记录。

SQL语句:
 select*
from student
where sname not like '王%';

28b0e80491d717b53393202d3f113d69.png

15.以class降序来查询所有学生记录。

分析:

  • 查询所有学生记录
  • 用order by子句对查询结果进行降序排序
SQL语句:
 select*
from student
 Order by sclass desc;

7a6fcd2374f47e2d1e8ba0b9942a4eba.png

最后

以上就是默默小鸽子为你收集整理的用字典存储学生成绩查询_用SQL创建学生成绩数据库的全部内容,希望文章能够帮你解决用字典存储学生成绩查询_用SQL创建学生成绩数据库所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(110)

评论列表共有 0 条评论

立即
投稿
返回
顶部