使用约束详解[转]
来源:互联网 发布: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]'
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]'
- 使用约束详解[转]
- Xml文件约束规则使用详解
- ORACLE的索引和约束详解(转)
- Oracle约束详解
- 外键约束详解
- SQLite数据库约束详解
- 差分约束详解
- Mysql中约束详解
- 差分约束详解
- SQLite数据库约束详解
- 差分约束详解
- oracle约束详解
- ConstraintLayout约束控件详解
- Oracle约束(Constraint)详解
- Oracle 约束详解
- 差分约束 详解
- SQL:创建使用约束
- 创建和使用约束
- 浅谈JAVA程序破解(原创)
- 存储过程编写经验和优化措施
- 窃以为软件的最大追求是在合适的地方做正确的事
- 微软评测Linux 性能出新招谁更能适应老爷机
- 幸福是什么
- 使用约束详解[转]
- 类似MSN、Foxmail消息提示控件
- JPEG图象解码源代码(Delphi源码)
- 中国象棋源代码
- 八数码难题源代码
- 背包问题的遗传算法解法
- 多年收集的车牌识别方面的论文集锦
- LinuxKernel中文版
- join 使用详解方式[转]