问题:

例如   有表    
  student  
  {  
      lesson   varchar(3)  
      name   varchar(3)  
  }  
      要让   distinct(lesson)   不能超过10  
   
   
  能通过数据库表的约束   限制吗?  
   
   
  如何实现

问题的解决:

--创建表  
  create   table   student  
  (  
      lesson   varchar(3),  
      name   varchar(3)  
  )  
   
  --创建函数  
  create   function   f_check()  
  returns   int  
  as  
  begin  
              declare   @num   int  
              select   @num=count(distinct   lesson)   from   student  
              return   @num  
  end  
  go  
   
  --添加约束  
  alter   table   student   add   constraint   student_lesson_check   check(dbo.f_check()<=10)   
   

评论
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

心似海
搜索本博客
博客分类
最近加入圈子
存档
最新评论