编辑代码

-- 创建单位表及完整性约束
create table Deparment(
    dName char(20) primary key,
    dTele char(11) not null,
    tNo char(10) not null,
    tName char(20) not null
);

-- 创建教师表及完整性约束
create table teacher(
    tNo char(10) primary key,
    tName char(20) not null,
    tSex char(2) check (tSex in('男','女')),
    tDuty char(20) check (tDuty in('助教','讲师','副教授','教授')),
    cNo char(4)
);

-- 创建课程表及完整性约束
create table course(
    cNo char(4) primary key,
    cName char(20) not null,
    dName char(20) not null,
    tNo char(9)
);
-- 创建学生表及完整约束
create table student(
    sNo char(10) primary key,
    dName char(10),
    sName char(10),
    sSex char(2) check(sSex in('男','女')),
    sAge int not null,
    cName char(20) not null
);



-- 创建视图
-- 创建学号为160501的学生的信息的视图(原代码条件值与需求“160501”不符,这里按需求调整,若要保持原内容可改回 sNo = 1  )
create view S1
as
select sNo,sName,sSex,sAge
from student
WHERE sNo = '160501';

-- 创建索引
-- 为数据库中 student 和 course 两个表建立索引(这里按原写法,实际可结合主键等合理设置索引字段 )
CREATE UNIQUE INDEX studentIndex on student;
CREATE UNIQUE INDEX courseIndex on course;

-- 创建存储过程
-- 建立存储过程,输入学号得到该学生的所有信息(原代码存在字段拼写错误,oNo 应为 sNo ,已修正 )
create procedure st
    @studentNo char(4)
as
select *
from student
where student.sNo = @studentNo;