热门文章
 
Transact_SQL索引
应用程序中的高级SQL注入
sql server数据转换
子查询
使用约束
case 的用法
生成测试数据 T-SQL系列
经典查询排序案例
获取排队顺序 T-SQL教案
查找不同时间段间的差异 T-
 推荐文章
 
join 方式
SQL Server中保存和
SELECT TOP N 问
SQL Server中的全文
sql server 由于登
SQL Server2005
怎样将Varbinary数据
MS SQL SERVER
数据分组
distinct id的取法
SQL Server安全防范
MD5算法的T-SQL实现
改变自增字段的初始值
sql server 数据恢
使用派生表
几个SQL日志有关的概念
 
你现在的位置:您现在的位置是: 中国ASP>>数据库>>sql server
使用约束

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]')


相关信息:

.Net下调用SqlServer2k存储过程
SQLServer分页查询通用存储过程
access升级到SQLServer如何调整字段类型及函数:JET SQL 和T-SQL中的同义词
Transact_SQL索引
应用程序中的高级SQL注入
sql server数据转换
子查询
使用约束
case 的用法

 

中国ASP技术 ASP.ORG.CN 版权所有 2004-2008