create database test_0425 -- 创建数据库
go
use test_0425
go
-- 建表
create table student
(
stno int primary key,
stsex char(4) not null,
stname char(10) not null,
stspecialty char(20) not null,
stscore int not null
)
go
-- 设置检查约束,性别只能输入 男或女
alter table student
add constraint Check_stsex check(stsex in ('男','女'))
go
-- 设置默认约束,未输入性别时候 默认男
alter table student
add constraint Default_stsex_man default '男' for stsex
go
insert into student values(001,'男','赵一','数学',99)
insert into student values(002,'男','赵二','数学',88)
insert into student values(003,'男','赵三','数学',97)
insert into student values(004,'男','赵四','数学',86)
insert into student values(005,'男','赵五','数学',45)
insert into student values(006,'男','赵六','数学',66)
insert into student values(007,'女','赵器','数学',77)
insert into student values(008,'女','赵把','语文',99)
insert into student values(009,'女','赵久','语文',100)
insert into student values(010,'女','赵时','语文',85)
insert into student values(011,'女','赵事宜','语文',75)
insert into student values(013,'女','赵十二','语文',66)
insert into student values(015,'女','赵十三','语文',55)
insert into student values(018,'女','赵十四','语文',32)
insert into student values(020,'女','赵失误','语文',0)
insert into student (stno,stsex,stname,stspecialty,stscore)
values(017,'女','赵时期','语文',86)
select *from student
if exists(select *from sysobjects where name='porc_fun_avge' and type='p')
drop procedure porc_fun_avge
go
-- 设计计算专业平均值的存储过程
create proc porc_fun_avge
(
@fun_stspecial char(20)
)
as
select @fun_stspecial as '学科',AVG(stscore) as '平均分' from student where stspecialty=@fun_stspecial
exec porc_fun_avge '数学'
exec porc_fun_avge '语文'
-- 设计分段存储过程
if exists(select *from sysobjects where name='porc_fun_avge' and type='p')
drop procedure proc_subsection_stscore
go
create proc proc_subsection_stscore
(
@score int,
@name char(10) out
)
as
if @score>60
select stname as '姓名',stscore as '分数', '及格' as '评价' from student
else
select stname as '姓名',stscore as '分数', '不及格' as '评价' from student
go
-- 调用/执行存储过程
declare @stscore int,
@stsname char(10)
set @stscore=65
exec proc_subsection_stscore @stscore,@stsname out