最近老师让做一个学生选课系统,主要用户有学生,老师和管理员,大家帮忙想想数据表该怎么设计(我们学校现在还没讲数据库呢,我了解的比较少,所以最好能详细点)

解决方案 »

  1.   

    参考:ASP+SQL选课系统
      

  2.   

    '参考一下''第一部分'
    --
    --创建数据库
    --
    create database 学生选课管理系统
    on
    (
        name = ChoiceCourse_dat,
        filename = 'C:\Documents and Settings\Administrator\桌面\yuanyangliu\ChoiceCourse.mdf',
        size = 5,
        maxsize= 10,
        filegrowth = 2
    )
    --创建日志文件
    log on
    (
        name=ChoiceCourse_log,
        filename='C:\Documents and Settings\Administrator\桌面\yuanyangliu\ChoiceCourse.ldf',
        size=3,
        maxsize=10,
        filegrowth=2
    )/*-----------------------------------------------------------------------------*/
    --创建教师信息表
    use 学生选课管理系统
    go
    create table 教师信息表
    (
        教师号 char(10) constraint Teacher_Prim primary key not null,
        姓名   char(10) not null ,
        性别   char(2) not null default '男',
        年龄   int  check ( 年龄 between 20 and 100),
        职称   char(10) not null default '讲师',
        工资   smallmoney ,
        岗位津贴  smallmoney ,
        系别   char(10) not null
    )
    go
    --为教师信息表创建索引
    create index 教师 on 教师信息表(教师号)--在教师信息表上创建视图
    create view 教师信息视图
    as
    select 教师号,姓名,性别,年龄,职称,系别
    from 教师信息表
    --drop table 教师信息表/*-----------------------------------------------------------------------------*/
    --创建学生信息表use 学生选课管理系统
    go
    create table 学生信息表
    (
        学号 char(10) constraint Student_Prim primary key not null,
        姓名 char(10) not null,
        性别 char(2) not null  default '男',
        年龄 int not null,
        系别 char(10) not null ,
    )
    go
    --为学生信息表创建索引
    create index 学生 on 学生信息表(学号)--创建学生信息视图
    create view 学生信息视图
    as
    select * from 学生信息表/*-----------------------------------------------------------------------------*/
    --创建课程信息表
    use 学生选课管理系统
    go
    create table 课程信息表
    (
        课程号  char(10) constraint Course_Prim primary key not null,
        课程名  char(10) not null,
        课时    int
    )
    go--索引在课程信息表
    create index 课程 on 课程信息表(课程号)--课程信息表视图
    create view 课程信息视图
    as
    select * from 课程信息表/*-----------------------------------------------------------------------------*/--创建选课关系表
    use 学生选课管理系统
    go
    create table 选课关系表
    (
        学号 char(10) not null ,
        课程号 char(10) not null,
        成绩  numeric(6,2),
        constraint Choice_Prim primary key (学号,课程号),
    )
    go
    --创建索引
    create index 选课 on 选课关系表(学号)--创建视图
    create view 选课关系视图
    as
    select * from 选课关系表/*-----------------------------------------------------------------------------*/
    --创建授课关系表
    use 学生选课管理系统
    go
    create table 授课关系表
    (
        教师号 char(10) not null,
        课程号 char(10) not null,
        constraint Teaching_Prim primary key (教师号,课程号),
    )
    go--drop table 授课关系表
    --索引
    create index 授课 on 授课关系表(教师号)
    --视图
    create view 授课关系视图
    as
    select * from 授课关系表/*-----------------------------------------------------------------------------*/
    --创建学生信息与学生选课信息汇总视图
    --drop view 学生信息汇总视图
    create view 学生信息汇总视图
    as
    select        学生信息表.学号,
                  姓名,
                  性别,
                  年龄,
                  系别, 
                  课程名,
                  成绩   from  学生信息表,课程信息表,选课关系表
    where 学生信息表.学号 =选课关系表.学号 and 选课关系表.课程号 = 课程信息表.课程号select * from 学生信息汇总视图--====================================信息 (增,删,查,改)====================================-------------教师信息表(增,删,查,改)存储过程------------------增
    create Procedure 添加教师信息
    (
         @教师号 char(10) ,
         @姓名 char(10),
         @性别 char(2),
         @年龄 int ,
         @职称 char(10),
         @工资 smallmoney,
         @岗位津贴 smallmoney,
         @系别 char(10)
    )
    as
    insert into 教师信息表 values(@教师号,@姓名,@性别,@年龄,@职称,@工资 ,@岗位津贴,@系别)--删create Procedure 教师号删除教师信息
    (
         @教师号 char(10)    
    )
    as
        begin
        if exists (select * from 授课关系表 where 教师号=@教师号)
            delete from 授课关系表 where 教师号=@教师号
            else
            print '此教师不存在'
        delete from 教师信息表 where 教师号=@教师号
        end
    --exec 教师号删除教师信息 'T1'
    --select * from 教师信息表--查
    --drop Procedure 姓名查找教师信息
    create Procedure 姓名查找教师信息
    (
         @姓名 char(10)    
    )
    as
        if (@姓名 is null)
        begin
            print '姓名不能为空,请重新输入姓名'
            return 13
        end
    select *  from 教师信息表
    where 姓名=@姓名
    exec 姓名查找教师信息 '李力'--drop Procedure 教师号查找教师信息
    create Procedure 教师号查找教师信息
    (
         @教师号 char(10)
        
    )
    as
        if (@教师号 is null)
        begin
            print '教师号不能为空,请重新输入教师号!'
            return 13
        end
    select *
    from 教师信息表
    where 教师号=@教师号exec 教师号查找教师信息 'T1'--改
    drop Procedure 教师号更新教师全部信息
    create Procedure 教师号更新教师全部信息
    (
         @教师号 char(10) ,
         @姓名 char(10),
         @性别 char(2),
         @年龄 int ,
         @职称 char(10),
         @工资 smallmoney  ,
         @岗位津贴 smallmoney ,
         @系别 char(10) 
    )
    as
        if (@教师号 is null)
        begin
            print '教师号不能为空,请重新输入教师号!'
            return 13
        end
       
        begin
        update 教师信息表
        set 姓名=@姓名
        where 教师号=@教师号
        update 教师信息表
        set 性别=@性别
        where 教师号=@教师号
        update 教师信息表
        set 年龄=@年龄
        where 教师号=@教师号
        update 教师信息表
        set 职称=@职称
        where 教师号=@教师号
        update 教师信息表
        set 工资=@工资
        where 教师号=@教师号
        update 教师信息表
        set 岗位津贴=@岗位津贴
        where 教师号=@教师号
        update 教师信息表
        set 系别=@系别
        where 教师号=@教师号       end
    exec 教师号更新教师全部信息 'T1','李王','男',57  ,'教授',2500  , 3000 ,'计算机'
    exec 教师号更新教师全部信息 'T1','李力','男',47  ,'教授',1500  , 3000 ,'计算机'
    select * from 教师信息表
    delete  from 教师信息表
      

  3.   

    '第二部分'-------------学生信息(增,删,查,改)存储过程----------------------增
    create Procedure 添加学生信息
    (
         @学号 char(10) ,
         @姓名 char(10),
         @性别 char(2),
         @年龄 int ,
         @系别 char(10)
    )
    as
    insert into 学生信息表 values(@学号,@姓名,@性别,@年龄,@系别)
    --删create Procedure 删除学生信息
    (
         @学号 char(10)    
    )
    as
        begin
        if exists (select * from 选课关系表 where 学号=@学号)
            delete from 选课关系表 where 学号=@学号
            else
            print '此学生不存在'
        delete from 学生信息表 where 学号=@学号
        end
    exec 删除学生信息 'S1'
    select * from 学生信息表
    select * from 学生信息汇总视图exec 添加学生信息 'S1','赵义','女', 17 ,'计算机'
    exec 添加选课信息 'S1','C1',90
    exec 添加选课信息 'S1','C2',85
    select * from 选课关系表
    --查
    --drop Procedure 学号查找学生信息
    create Procedure 学号查找学生信息
    (
         @学号 char(10))
    as
        if (@学号 is null)
        begin
            print '学号不能为空,请重新输入学号!'
            return 13
        end
    select *
    from 学生信息表
    where 学号=@学号
    exec 学号查找学生信息 'S1'
    --drop Procedure 姓名查找学生信息
    create Procedure 姓名查找学生信息
    (
         --@学号 char(10) output,
         @姓名 char(10)
         --@性别 char(2) output,
         --@年龄 int output,
         --@系别 char(10) output
    )
    as
        if (@姓名 is null)
        begin
            print '姓名不能为空,请重新输入姓名!'
            return 13
        end
    select *
    from 学生信息汇总视图
    where 姓名=@姓名exec 姓名查找学生信息 '钱尔'--改
    create Procedure 学号更新学生全部信息
    (
         @学号 char(10) ,
         @姓名 char(10),
         @性别 char(2),
         @年龄 int ,
         @系别 char(10)    
    )
    as
        if (@学号 is null)
        begin
            print '学号不能为空,请重新输入学号!'
            return 13
        end
       
        begin
        update 学生信息表
        set 姓名=@姓名
        update 学生信息表
        set 性别=@性别
        update 学生信息表
        set 年龄=@年龄
        update 学生信息表
        set 系别=@系别
        where 学号=@学号   
        end
    -------------课程信息表(增,删,查,改)存储过程--------------------
    --增
    create Procedure 添加课程信息
    (
         @课程号 char(10) ,
         @课程名 char(10),   
         @课时 int
        
    )
    as
    insert into 课程信息表 values(@课程号,@课程名,@课时)
    --查
    create Procedure 课程名查找课程信息
    (
         @课程名 char(10)
    )
    as
            if (@课程名 is null)
        begin
            print '课程名不能为空,请重新输入课程名!'
            return 13
        end
    select *  from 课程信息表
    where (课程名=@课程名)
    create Procedure 课程号查找课程信息
    (
         @课程号 char(10)
    )
    as
            if (@课程号 is null)
        begin
            print '课程号不能为空,请重新输入课程号!'
            return 13
        end
    select *  from 课程信息表
    where (课程号=@课程号)--改
    create Procedure 课程号更新课程全部信息
    (
         @课程号 char(10) ,
         @课程名 char(10),   
         @课时 int
    )
    as
            if (@课程号 is null)
        begin
            print '课程号不能为空,请重新输入课程号!'
            return 13
        end
       
        begin
        update 课程信息表
        set 课程名=@课程名
        update 课程信息表
        set 课时=@课时
        where 课程号=@课程号   
        end-------------选课关系(增,删,查,改)存储过程--------------------
    --增
    create Procedure 添加选课信息
    (
         @学号 char(10) ,
         @课程号 char(10),   
         @成绩  numeric(6,2)
        
    )
    as
      insert into 选课关系表 values(@学号,@课程号,@成绩)--删
    create Procedure 删除选课信息
    (
         @学号 char(10),
         @课程号 char(10)
    )
    as
    delete from 选课关系表 where(学号=@学号 and 课程号=@课程号)--查--改-------------授课关系(增,删,查)存储过程--------------------
    --增
    create Procedure 添加授课信息
    (
         @教师号 char(10) ,
         @课程号 char(10)  
    )
    as
      insert into 授课关系表 values(@教师号,@课程号)--删
    create Procedure 删除授课信息
    (
         @教师号 char(10),
         @课程号 char(10)   
    )
    as
    delete from 授课关系表 where(教师号=@教师号 and 课程号=@课程号)--查
    create Procedure 教师号查询授课信息
    (
        @教师号 char(10)
    )
    as
      select * from 授课关系表 where (教师号=@教师号)----------------------------------------------触发器----------------------------------------------
    --为教师信息表创建触发器
    create trigger 教师
    on 教师信息表
    for insert , update , delete
    as
    select * from 教师信息表
    exec 添加教师信息 'T6','洋洋','男',30 ,'教授',3300 , 5000 ,'信息'
    exec 教师号删除教师信息 'T6'
    --为学生信息表创建触发器
    create trigger 学生
    on 学生信息表
    for insert , update , delete
    as
    select * from 学生信息表
    exec 添加学生信息 'S7','洋洋','男', 20 ,'计算机'
    exec 删除学生信息 'S7'--课程信息表创建触发器
    create trigger 课程
    on 课程信息表
    for insert , update , delete
    as
    select * from 课程信息表exec 添加课程信息 'C9','UML',80--添加选课关系表触发器
    create trigger 选课
    on 选课关系表
    for insert , update , delete
    as
    select * from 选课关系表exec 添加选课信息 'S6','C9',89
    exec 删除选课信息 'S6','C9'--授课关系触发器
    create trigger 授课
    on 授课关系表
    for insert , update , delete
    as
    select * from 授课关系表
    exec 添加授课信息 'T6','C9'
    exec 删除授课信息 'T6','C9'
    select * from 授课关系表
    ----------------------------------------------数据添加---------------------------------------------
    --添加教师信息
    exec 添加教师信息 'T1','李力','男',47  ,'教授',1500  , 3000 ,'计算机'
    exec 添加教师信息 'T2','王平','女',28  ,'讲师', 800  , 1200 ,'信息'
    exec 添加教师信息 'T3','刘伟','男',30  ,'讲师', 900  , 1200 ,'计算机'
    exec 添加教师信息 'T4','张雪','女',51  ,'教授',1600  , 3000 ,'自动化'
    exec 添加教师信息 'T5','张兰','女',39 ,'副教授',1300 , 2000 ,'信息'
    --select * from 教师信息视图
    --select * from 教师信息表--添加学生信息
    exec 添加学生信息 'S1','赵义','女', 17 ,'计算机'
    exec 添加学生信息 'S2','钱尔','男', 18 ,'信息'
    exec 添加学生信息 'S3','孙珊','女', 20 ,'信息'
    exec 添加学生信息 'S4','李思','男', 21 ,'自动化'
    exec 添加学生信息 'S5','周武','男', 19 ,'计算机'
    exec 添加学生信息 'S6','吴丽','女', 20 ,'自动化'--select * from 学生信息表
    --select * from 学生信息视图--添加课程信息表
    exec 添加课程信息 'C1','程序设计',60
    exec 添加课程信息 'C2','微机原理',80
    exec 添加课程信息 'C3','数字逻辑',60
    exec 添加课程信息 'C4','数据结构',80
    exec 添加课程信息 'C5','数据库',60
    exec 添加课程信息 'C6','编译原理',60
    exec 添加课程信息 'C7','操作系统',60
    --select * from 课程信息表
    --select * from 课程信息视图
    --添加选课信息
    exec 添加选课信息 'S1','C1',90
    exec 添加选课信息 'S1','C2',85
    exec 添加选课信息 'S2','C5',57
    exec 添加选课信息 'S2','C6',80
    exec 添加选课信息 'S2','C7',95
    exec 添加选课信息 'S2','C4',70
    exec 添加选课信息 'S3','C1',75
    exec 添加选课信息 'S3','C2',70
    exec 添加选课信息 'S3','C4',85
    exec 添加选课信息 'S4','C1',93
    exec 添加选课信息 'S4','C2',85
    exec 添加选课信息 'S4','C3',83
    exec 添加选课信息 'S5','C2',89
    --select * from 选课关系表--添加授课信息
    exec 添加授课信息 'T1','C1'
    exec 添加授课信息 'T1','C4'
    exec 添加授课信息 'T2','C5'
    exec 添加授课信息 'T3','C1'
    exec 添加授课信息 'T3','C5'
    exec 添加授课信息 'T4','C2'
    exec 添加授课信息 'T4','C3'
    exec 添加授课信息 'T5','C5'
    exec 添加授课信息 'T5','C7'
    select * from 授课关系表