如何用SQL建立一个学生成绩管理系统数据库?

如何用SQL建立一个学生成绩管理系统数据库?,第1张

首先在SQL中利用企业管理器或向导建立一个数据库,命名为学生管理系统,

启动SQL Sever服务,运行企业管理器,单击要创建数据库的服务器左边的加号图标,展开树形目录,在“数据库”节点上右击鼠标,在d出的快捷菜单中选则“新建数据库”命令,然后按照提示一步步建立数据库,不再详细叙述。

假设学生管理系统下有三个表,分别为学生表、课程表、修课表,表的结构分别如下:

学生表(student) (

学号(sno) 普通编码定长字符类型,长度7,主码,

姓名(sname) 普通编码定长字符类型,长度8,非空,

性别(ssex) 统一编码定长字符类型,长度1,

年龄(sage) 微整型,

所在系(sdept) 统一编码可变长字符类型,长度20

课程表(course) (

课程号(cno) 普通编码定长字符类型,长度6,主码,

课程名(cname) 统一编码定长字符类型,长度10,非空,

学分(credit) 小整型,

学期(semester) 小整型

修课表(sc)(

学号(sno) 普通编码定长字符类型,长度7,主码,外码

课程号(cno) 普通编码定长字符类型,长度6,主码,外码

成绩(grade) 小整型,

修课类别(type)普通编码定长字符类型,长度4

则创建表的语句分别为:

create table Student(

Sno char(7) primary key,

Sname char(8) not null,

Ssex nchar(1),

Sage tinyint,

Sdept nvarchar(20)

)

create table Course(

Cno char(6) primary key,

Cname nchar(10) not null,

Credit smallint,

Semester smallint

)

create table SC(

Sno char(7),

Cno char(6),

Grade smallint,

Type char(4),

primary key(Sno,Cno),

Foreign key(Sno)    References Student (Sno),

Foreign key(Cno)    References Course (Cno)

)

各表的结构大体如此,如有变化可自行修改。 以上数据库和表就基本建立好了,然后就可以通过数据导入或SQL语句等向数据库中添加学生的各项具体数据了。

SQL语言,是结构化查询语言(Structured Query Language)的简称。SQL语言是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统;同时也是数据库脚本文件的扩展名。

---------更新成绩status=2 未提交成绩 可修改成绩 不能插入更新 学生不可查看, status=1成绩已提交 不可修改成绩 学生可以查看

------插入数据-------------

--用户信息表

insert into userinfo values('20101000','123',1)

insert into userinfo values('20101004','123',2)

insert into userinfo values('20101152100','123',3)

select*from userinfo

--学生信息表

insert into studinfo values('20101152100','素雅','女','计科1班')

select *from studinfo

--教师信息表

insert into techerinfo values('20101004','李大为','男')

select *from techerinfo

--成绩表

insert into studscoreinfo values('5','1003','20101152100','20101003','汇编','99',1)

select *from studscoreinfo

---教师管理成绩--------------

--已提交时

update studscoreinfo set studscore='86' where courseid='1002' and studno='20101152103' and status=2

print '已提交不能修改成绩'

select*from studscoreinfo

--未提交时

update studscoreinfo set studscore='90' where courseid='1001' and studno='20101152083' and status=1

print '已修改成绩'

--提交成绩

--改为未提交

update studscoreinfo set status=2 where courseid='1001' and studno='20101152083'

--改为提交

update studscoreinfo set status=1 where courseid='1001' and studno='20101152083'

select *from studscoreinfo

----------------学生----------

---已提交可查看成绩时

select studno,teachno,studscore,course,studscore,status

from userinfo U,studscoreinfo S where U.username=S.studno and U.role=3 and S.studno='20101152083' and S.status=1

print '查询成功!'

---未提交不可查看成绩时

select studno,teachno,studscore,course,studscore,status

from userinfo U,studscoreinfo S where U.username=S.studno and U.role=3 and S.studno='20101152083' and S.status=2

print '还不可查询'

----------系统管理员 可对学生信息,教师信息,成绩信息等进行管理----------

select *from userinfo

select *from studinfo

select *from techerinfo

select *from studscoreinfo

--学生信息表---

--增加

insert into userinfo values('20101152101','123',3)

insert into studinfo values('20101152101','陆琼','女','计科2班')

select *from studinfo

--修改更新

update studinfo set studsex='男' where studno='20101152101'

select *from studinfo

--删除

delete from studinfo where studno='20101152101'

select *from studinfo

--教师信息表----

--增加

insert into userinfo values('20101005','123',2)

insert into techerinfo values('20101005','烧饼','男')

select *from techerinfo

--修改

update techerinfo set techname='烧包谷' where techname='烧饼'

select *from techerinfo

--删除

delete from techerinfo where teachno='20101005'

select *from techerinfo

--成绩信息表-----

--增加

insert into studscoreinfo values('6','1004','20101152100','20101002','数据结构','70',1)

select *from studscoreinfo

--修改

update studscoreinfo set studscore='100' where studno='20101152100' and courseid='1004'

select *from studscoreinfo

--删除

delete from studscoreinfo where studno='20101152100'

select *from studscoreinfo


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6693035.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-26
下一篇2023-03-26

发表评论

登录后才能评论

评论列表(0条)

    保存