SQLServer2005 高校入门教学系列
SQL 语句基础
--查询学生中选修课程号 为1 的学生姓名
select sname
from student
where exists(
select *
from sc
where student.sno = sc.sno
and cno='1'
)
--查询选修了全部课程的学生姓名
select sname
from student
where exists(
select *
from course
where exists(
select *
from sc
where sc.cno = course.cno
and sc.sno = student.sno
)
)
-- 左值链接, 外链接
select student.sno,sname,ssex,sage,sdept,cno,grade
from student left outer join sc on (student.sno = sc.sno)
--建立唯一性索引
create unique index stu on student(sno asc,cno desc)
--having 代表着选择条件
select sno from sc group by sno
having sno <> '200215121'
-- 修改数据表字段类型
alter table student alter column comment char(255)
alter table sc alter column grade smallint
-- 添加数据表列
alter table student add comment char(50)
alter table student add S_enterence datetime
alter table course add unique(cname)
-- 模糊精确条件
select *
from course
where cname like 'DB\_design' escape '\'
--查询课程名中含有DB_design的课程名
--建立存储过程
create procedure proc_insert_student
@sno1 char(9),
@sname char(20),
@ssex char(2) = '男',
@sage int,
@sdept char(20)
as
begin
insert into student(sno,sname,ssex,sage,sdept)
values(@sno1,@sname,@ssex,@sage,@sdept)
end
create proc proc_avgrade_sc
@sno char(9),
@savg int output -- out
as
begin
select @savg = avg(grade)
from sc where sno = @sno
end
--执行存储过程
exec proc_insert_student '2002015126','张新阳','男',19,'CS'
exec proc_insert_student @sno1 = '2002015128',@sname = '尼阳',@sage = 22,@sdept = 'IS'
declare @avg int
set @avg = 0
exec proc_avgrade_sc '200215121',@avg output
--输出参数必须是output 类型
--select @avg 平均成绩
--删除存储过程
drop prodc [procedure_name]