20309 发表于 2016-11-2 07:21:52

SQL server控制表中的列数

  问题:
  例如   有表    
  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)   
   
页: [1]
查看完整版本: SQL server控制表中的列数