SQLServer数据库约束(1)_表内约束(单表的五类约束)_附自增属性以及全局唯一性标识符类型作为主键
2014-02-27 12:00
316 查看
非空约束:
--建表时设置 if exists(select name from sysobjects where name='tablename' and xtype='u') drop table tablename go create table tablename ( rowname1 int not null, rowname2 int, ) go --此类约束不适合在建表后追加 alter table tablename alter rowname2 int not null go --另外如果该列已经存在了数据,并且没有主键约束与唯一性约束,最好在追加not null约束时增加默认约束
主键约束:
自增属性:
添加自增属性的列,其数据类型必须是 int、bigint、smallint、tinyint 或 decimal,或者是小数位数为 0 的 numeric 数据类型,并且约束为不可为空if exists(select name from sysobjects where name='tablename' and xtype='u') drop table tablename go create table tablename ( rowname1 int identity not null, ) go
identity(seed,growth)
即初始值为seed,之后每次加growth作为该列的值,默认不指定的话为(1,1)
主键列的几种形式:
首先主键列不能为空,不能有重复值——唯一、非空1、 字符型主键列数据,其内容需要用户输入,无论是否包括中文;
a) 如果不包括中文,则使用单字节字符编码
b) 如果数据中有中文存在,则一定要使用双字节编码
c) 如果是变长的数据,也以按照以上两点执行。
--建表时设置 if exists(select name from sysobjects where name='tablename' and xtype='u') drop table tablename go create table tablename ( rowname1 char(2) not null/*constraint pk_name*/ primary key,--不加非空约束也可执行,系统会为主键字段添加not null约束 ) go
--建表后追加 if exists(select name from sysobjects where name='tablename' and xtype='u') drop table tablename go create table tablename ( rowname1 char(2) not null,--必须设置非空约束,否则追加主键约束时报错 ) go alter table tablename add --constraint pk_name primary key(rowname1) go
2、 字符型主键列数据,其内容需要用户输入,无论是否包括中文;
a) 如果不包括中文,则使用单字节字符编码
b) 如果数据中有中文存在,则一定要使用双字节编码
c) 如果是变长的数据,也以按照以上两点执行。
--建表时设置 if exists(select name from sysobjects where name='tablename' and xtype='u') drop table tablename go create table tablename ( rowname1 int identity /*constraint pk_name*/ primary key, ) go
--建表后追加 if exists(select name from sysobjects where name='tablename' and xtype='u') drop table tablename go create table tablename ( rowname1 int identity, ) go alter table tablename add --constraint pk_name primary key(rowname1) go
3、用全局唯一性标识符类型作主键:
--建表时设置 if exists(select name from sysobjects where name='tablename' and xtype='u') drop table tablename go create table tablename ( rowname1 uniqueidentifier /*constraint pk_name*/ primary key, ) go
--建表后追加 if exists(select name from sysobjects where name='tablename' and xtype='u') drop table tablename go create table tablename ( rowname1 uniqueidentifier not null ) go alter table tablename add --constraint pk_name primary key(rowname1) go
数据类型为该类型的字段插入值一般使用newid()函数,不建议自己生成
从另一角度对主键进行分类:
1、 自然主键,该列与其他列没有依赖关系;
2、 业务主键,该列数据与其他列数据之间有着依赖的关系;
默认约束:
--建表时设置 if exists(select name from sysobjects where name='tablename' and xtype='u') drop table tablename go create table tablename ( rowname1 int /*constraint pk_name*/ default 1, ) go
--建表后追加 if exists(select name from sysobjects where name='tablename' and xtype='u') drop table tablename go create table tablename ( rowname1 int ) go alter table tablename add --constraint pk_name default 1 for rowname1 go
唯一性约束:
--建表时设置 if exists(select name from sysobjects where name='tablename' and xtype='u') drop table tablename go create table tablename ( rowname1 int /*constraint pk_name*/ unique, ) go
--建表后追加 if exists(select name from sysobjects where name='tablename' and xtype='u') drop table tablename go create table tablename ( rowname1 int ) go alter table tablename add --constraint pk_name unique(rowname1) go
检查约束:
--建表时设置 if exists(select name from sysobjects where name='tablename' and xtype='u') drop table tablename go create table tablename ( rowname1 int /*constraint pk_name*/ check(rowname1>0),--第一种形式 做比较来判断 rowname2 int check(rowname2 between 1 and 100),--第二种形式 限定区间 rowname3 nchar(1) check(rowname3 in ('男','女')),--限定范围 rowname4 nchar(1) check(rowname4 like '[0-9]'),--第三种形式 使用正则验证 ) go
删除约束:
删除所有的约束方法都一样alter table student drop constraint pk_name go
alter table student drop constraint pk_name1,pk_name2,.. go
相关文章推荐
- swift的全局和局部变量以及类型属性
- oracle中查询表的信息,包括表名,字段名,字段类型,主键,外键唯一性约束信息
- MySql入门之一:DDL操作创建表(添加主键, 外键约束以及基本的数据类型)
- oracle中查询表的信息,包括表名,字段名,字段类型,主键,外键唯一性约束信息
- MySql之DDL操作创建表(添加主键, 外键约束以及基本的数据类型)
- js3:数据类型,数组,String各个属性,以及字符串表达式用eval计算
- ORACLE: 查询(看)表的主键、外键、唯一性约束和索引
- SQL Server里的主键、唯一性约束、聚集索引
- mysql把主键定义为自动增长标识符类型
- 查看库中表的主键,标识列,计算列,字段类型,允许空,默认值等属性
- C语言中标识符的作用域、命名空间、链接属性、生命周期、存储类型
- mybatis插入一个对象后获取表中自增的主键Id并且传入到插入的的对象中,方便将对象中其他属性赋值给其他以前表主键Id作为非空字段的表
- 使用DTD文档对 xml 1 作业中的xml文档进行约束,其中河北省只能依次包含石家庄市、张家口市、保定市这三个市,保定市可以有多个或者1个,给保定市定义属性编号为ID类型,必须有;属性name为字符
- Oracle中主键、外键、索引、序列、唯一性约束的创建
- C#泛型、反射-根据对象名获取该类型以及其属性
- ORACLE10G中列属性NULLABLE与CHECK约束及主键约束的关系2
- mysql 学习之列类型以及属性特点详解
- Ubuntu 14.04 + QT 计算器开发实例1-----全局变量的使用以及double、字符串类型互转
- 要求从用户输入的多行文本中提取学生的姓名、学号及登录日期,并封装到 Student 类中作为类的私有属性。创建一个 Student 类型的对象数组,对学号进行 升序排序并输出
- 主键约束和唯一性约束的区别