27、SQL Server 数据架构创建之数据列的创建
2013-06-14 23:32
232 查看
数据列的创建
一、数据类型1、字符型
char(n)
nchar(n)
varchar(n) varchar(max)
nvarchar(n) --unicode编码 当有多国语言时,可选择使用。
nvarchar(max)
text
ntext
sysname
2、数字数据类型
bit
tinyint
smallint
int
bigint
decimal | numeric
money
smallmoney
float
real
3、日期/时间
datetime
smalldatetime
4、其他数据类型
timestamp 随机值
uniqueidentifiler
binary(n)
binary(max)
varbinary(n)
varbinary(max)
image
sql_variant
二、计算列(表达式列)
如:
create table a ( s1 int, s2 int, s3 as s1+s2 )
三、更约束和默认值
1、主键约束 2、外键约束 3、为空性 4、Check约束 5、唯一约束 6、列默认值
注:如同前面介绍创建主键和外键的方法
not null、check()、unique、default()
1、列的为空性
null
not null
如:
create table a ( s1 int null, s2 int not null )
2、唯一约束
在创建表时在列后加unique
或者
使用alter table add constraint添加
如:
create table Student ( id int not null, name varchar(20), address varchar(20) unique )
或
创建表后(前提是没有添加约束)
alter table Student add constraint Student_UQ_address unique(address)
3、Check约束
同上有两个方法:
如:
create table Student ( id int not null, name varchar(20), address varchar(20), sex char(2) check(sex in('男','女')) )
或
alter table Student add constraint Stu_CHECK_sex check(sex in('男','女'))
4、默认值选项
同上有两种方法
1、default 值
2、default 值 for 列名
注:在创建表时,可以将所有的约束全部定义在表定义时的最后。如:
create table Student ( id int, name varchar(20), address varchar(20), sex char(2), constraint Stu_Check_SEX check(sex in('男','女')), constraint Stu_PK_id primary key (id), )
相关文章推荐
- 27、SQL Server 数据架构创建之数据列的创建
- 24、SQL Server 数据架构创建
- 25、SQL Server 数据架构创建之数据库文件的创建
- 26、SQL Server 数据架构创建之表格的创建
- 【SQL server】创建零时表格,实现数据的分析
- sql server 2005 创建新的一列,并插入数据
- 【SQL Server学习笔记】Service Broker创建异步的、数据驱动的消息应用程序
- 通过创建SQL Server 2005到 Oracle10g 的链接服务器实现异构数据库数据转换方案
- SQL SERVER 使用存储过程创建、删除用户定义数据类型
- SQL Server 数据的创建、增长、收缩
- SQL server 创建与删除数据库,数据表,约束的创建与删除语法
- sql server 数据库系统整理——数据表的创建和管理
- SQL Server 向数据库中创建表并添加数据
- SQL Server Mobile 学习(二):通过 VS2005 创建和连接及操作 SQL Server Mobile 数据库
- SQL server 数据库的表的创建与使用T-SQL语句操控数据表
- 为sql server 表数据生成创建的储存过程(生成insert 脚本) (转)
- EF架构~mysql数据库无法创建数据模型
- SQL Server 2005不能导入数据(无法创建SSIS 运行时对象)
- sql server 批量修改数据表和存储过程的所有者(2000)或架构(2005)
- 从现有数据创建 XML 架构和数据集