(3)、向教师表中添加数据
insert into course values('01','计算机','11') insert into course values('02','网络管理','12') insert into course values('03','专业英语','13') insert into course values('04','软件工程','14') ???? 查看记录
Select * from course
(4)、向课程表中添加数据
insert into teacher values('11','无意','计算机系','男','1973-4-5','教授') insert into teacher values('12','生活','计算机系','女','1975-12-1','副教授') insert into teacher values('13','没有','管理系','女','1975-3-3','副教授') insert into teacher values('14','离开','英语系','男','1973-5-5','教授')
????
查看记录
Select * from teacher
5、一些查询语句 (1)、查询成绩大于学号为101的学生的课程为02的成绩的所有列。
select * from score where degree>(select degree from score where sno='101' and cno='02')
(2)、查询课程号01大于课程号02的最大值、并以分数降序排序的成绩表中所有列
select * from score s where s.cno='01' and s.degree>=(select max(degree) from score y where y.cno='02' ) order by degree desc go
select max(degree) as \
(3)、查询性别为男的学号,姓名,班级,课程号和成绩的学生
select student.sno,student.sname,student.class,score.cno,score.degree from student,score where student.sno=score.sno and ssex='男'
(4)、查询成绩在60到80之间的所有列
select * from score where degree between 60 and 80
(5)、查询score表中至少有5名学生选修的并以0开头的课程的平均分
select avg(degree) as \平均分\from score where cno like '0%' group by cno having count(*)>=5
6、创建自定义数据类型
创建一个email自定义数据类型
exec sp_addtype email, 'varchar(20)' , 'null'
修改student表中的semail数据类型为email类型 alter table student alter column semail email 7、向表中添加字段
向student表添加type,semail,,b并且邮件地址有check约束 alter table student add type char(7)
alter table student add semail varchar(20) null constraint ck_sem check (semail like '%@%') alter table teacher add tel varchar(15) 8、创建视图 (1)、创建所有11班的学生信息的视图 create view student11 as
select * from student where class='11' 查看视图中的记录 select * from student11
(2)、创建视图course_degree,其中的内容是选修计算机课程的学生信息,包括(sno,sname,cno,cname,degree),创建时加上with check option create view course_degree(sno,sname,cno,cname,degree) as
select score.sno,sname,score.cno,cname,degree from course ,student, score where score.cno=course.cno and student.sno=score.sno and cname='计算机' with check option 查看视图中的记录
select * from course_degree
(3)、创建一个视图,其中的内容是成绩表中每门课程的 create view average as
select avg(degree) as '平均分' from score group by cno 查看视图中的记录
select * from average (4)、创建视图其中的内容是所有男教师和男学生的name,sex,birth create view man as
select sname as name,ssex as sex,sbirth as birth from student where ssex='男' union select tname,tsex,tbirth from teacher where tsex='男' 查看视图中的记录 select * from man
9、创建规则
规则的作用月CHECK约束的部分功能相同,在向表中的某列插入或更新数据时,用它来限制输入的新值的取值范围。而它与CHECK约束不同的是:
? CHECK约束是用CREATE TABLE语句在建表时指定的,而规则需要作为单独的
数据库对象来实现。
? 在一个列上只能使用一个规则。但可以使用多个CHECK约束。
? 规则可以应用于多个列,还可以应用于用户自定义的数据类型,而CHECK约束
只能应用于它定义的列。
(1)、创建一个degree_rule规则 create rule degree_rule as
@values>0
把此规则绑定到score表中degree列 exec sp_bindrule 'degree_rule','score.degree'
在向成绩表中添加记录时,如果成绩degree<0,则插入不成功。 (2)、创建一个tel_rule规则 create rule tel_rule as
@value like '[0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]' 把此规则绑定到teacher表中tel列 exec sp_bindrule 'tel_rule','teacher.tel'
在向教师表中添加记录时,如果电话号码不是0-9的数字,则插入不成功。
10、创建存储过程
存储过程是存储在服务器上的例行程序及过程,在SQL SERVER只能感定义某个过程,其中记录了一系列的造作,每次应用程序只需调用该过程就可完成该操作,这种SQL SERVER中定义的过程就被称为存储过程。它可以完成以下功能:
? 接受输入参数并返回多个输出值。
? 包含T-SQL语句用以完成特定的SQL SERVER操作,其中可以有对其他存储过
程的调用。
? 返回一个指示成功与否及失败原因的状态代码给调用它的过程。 存储过程不能接用过程名返回值,也不能直接在表达式中使用。 (1)、创建一个存储过程,来显示成绩表中的课程号在课程表中并且所任教师性别为男、所在部门是计算机系的成绩表中的列 create proc student_11 as
select * from score where cno in (select cno from course ,teacher where course.tno=teacher.tno and depart='计算机系' and tsex='男') 调用此存储过程 Exec student_11
(2)、创建一个带输入参数的存储过程,调用此存储过程时,给出一个学生名,显示出此学生的学号,姓名,所学课程号,课程名称和对应的成绩 create proc student_name @sname varchar(10) as
select student.sno,sname,score.cno,degree cname from student,score,course where student.sno=score.sno and score.cno=course.cno and sname=@sname 调用此存储过程,(此例是输出姓名为历史的学生的信息) exec student_name '历史'