sql语句(5)

2020-02-22 10:49

IDENTITY字段数据类型只能是(int, bigint, smallint, tinyint, decimal, or numeric(x,0))

IDENTITY字段必须是not null约束

4.3.1Identity 语法:

IDENTITY( [,

,

])

column_name, 代码: //使用Identity

CREATE TABLE MyTable (

key_col int NOT NULL IDENTITY (1,1), abc char(1) NOT NULL )

INSERT INTO MyTable VALUES ('a') INSERT INTO MyTable VALUES ('b') INSERT INTO MyTable VALUES ('c')

----------------------------------------------------------- 4.4约束Constraints 4.4.1缺省约束(default)

AS

4.4.2非空约束(not null) 4.4.3规则约束(rule) 4.4.4检查约束(check) 4.4.5唯一约束(unique) 4.4.6主键约束(primary key) 4.4.7外键约束(foreign key) 4.4.8商业规则(business rule)

以下面两个表为例进行演示 create table tb_hr_bm( bm varchar(20) not null , remark varchar(100) default '' )

create table tb_hr_gz( id int not null,

name varchar(30) not null, hrid char(18) null, workage int null , bm varchar(20) null, gz real null,

remark varchar(100) null )

hrid=身份证号码 workage=工作年数 gz=工资金额

----------------------------------------------------------- 4.4.1缺省约束(default)

语法:CREATE DEFAULT default_name AS expression 代码:CREATE DEFAULT zip_default AS 94710 ----------------------------------------------------------- 4.4.2非空约束(not null)

//表的主键和其它必填字段必须为not null.

语法:create table (column-name datatype not null... ) 代码:create table tb_hr_gz(id int not null,...) ----------------------------------------------------------- 4.4.3规则约束(rule)

语法:CREATE RULE rulename AS condition 代码:

//邮编号码6位100000-999999 //建立一个自定义zip类型

CREATE TYPE zip FROM CHAR(6) NOT NULL //建立一个规则约束

CREATE RULE zip_rule AS @number >100000 and @number < 999999

//绑定规则约束到zip类型 EXEC sp_bindrule zip_rule, 'zip' //应用自定义zip类型 2> CREATE TABLE address( city CHAR(25) NOT NULL, zip_code ZIP,

street CHAR(30) NULL )

-----------------------------------------------------------

4.4.4检查约束(建立/删除) //检查约束建立 语法:

alter table name

add constraint <检查约束名> check<取值范围表达式> 代码:

//工资添加取值范围0 ~ 1000000 方法1:

create table tb_hr_gz(

gz real default 0.0 check(gz >=0 and gz <=1000000),

... ) 方法2:

alter table tb_hr_gz

add constraint tb_hr_gz_ck check(gz >=0 and gz <=1000000)

//检查约束删除 语法:

alter table name drop constraint <检查约束名> 代码:

//删除工资的检查约束

alter table tb_hr_gz drop constraint tb_hr_gz_ck ----------------------------------------------------------- 4.4.5唯一约束 4.4.5.1.唯一约束添加 语法:

alter table name add constraint <唯一约束名> unique<列名> 代码:

//列如身份证号码是唯一的!

alter table tb_hr_gz Add constraint tb_hr_gz_uk unique(hrid)

4.4.5.2.唯一约束删除


sql语句(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:享受健康的网络生活教学设计

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: