--创建环境(用自定义函数创建约束,看见有人这么试过:) )Create Table STUDENT (
   学号 varchar(10),
   姓名 varchar(10),
   年龄 int,
   班号 int
)
go
Create Table CLASS (
   班号 int,
   专业名 varchar(10),
   系名 varchar(10),
   入学年份 int
)
go
Create Table DEPARTMENT (
   系号 int,
   系名 varchar(10)
)
go--函数
--创建函数
create function f_check()
returns int
as
begin
      declare @num int
      select @num=count(学号) from STUDENT 
             GROUP BY 班号
      return @num
end
go--添加约束
alter table STUDENT add constraint student_学号_check 
check(dbo.f_check()<=20)alter table STUDENT add constraint student_年龄_check 
check(年龄>=16 and 年龄<=30)
--删除环境
Drop table STUDENT,CLASS,DEPARTMENT