使用约束详解[转]

来源:互联网 发布:linux nano 退出 编辑:程序博客网 时间:2024/05/12 19:47
使用约束
1.使用 primary key 约束
  其值能唯一的标识表中的每一行。这样的一列或多列成为表的主键,通过它可强制表的实体完整性。
  job_id int primary key clustered
  emp_id empid constraint pk_emp_id primary key nonclustered
2.使用foreign key 约束
   约束引用其他的表
  job_id samllint not null  references jobs(job_id)
  foreign key(job_id) references jobs(job_id)
  constraint fk_sales foreign key(stor_id,orde_num,title_id)
  references sales(stor_id,ord_num,title_id)
3.使用unique 约束
  unqiue约束用于强制非主键列的唯一性,允许存在空值(应该只有一个)
  person varchar(30) null unique nonclustered
  constraint u_store unique nonclustered(stor_name,city)
4.使用default定义
  使用insert和update语句时,如果没有提供值,则使用默认值。
  提供了默认值,  用dbgrid 编辑必须在onnewrecord 事件加上默认值的赋值,否则提示错误
 ‘row can not be located for updating.some values has been changed since it was last read ' 
  default(getdate())
  创建一个产品价格表,并且设置产品的改价者为当时增修改数据的用户
  create table price
  (
   prod_id char(5),
   sup_id   char(5),
   unit_price money,
   modifier char(5)
   modi_date datetime default getdate(),
   primary key(prod_id,sup_id)
   default user for modifier
   )
5.使用check约束
  check(min_lvl>=10) 
  check(max_lvl<=250)
  constraint ck_emp_id check (emp_id like '[a-z][a-z][a-z][1-9][0-9][0-9][0-9][0-9]'
      or emp_id like [a-z][a-z][1-9][0-9][0-9][0-9][0-9]')
  check (pub_id in ('1389','0736','0877') or pub_id like'99[0-9][0-9]'
原创粉丝点击