使用约束详解

使用约束
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]'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值