码迷,mamicode.com
首页 > 其他好文 > 详细

Partition table的switch条件2:Partition 的隐式Check约束 和Nullability

时间:2015-12-11 22:24:06      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

Partition column允许为Null,Null是最小值,存在于Partition Number=1的partition中。

Any data with a NULL in the partition column will reside in the leftmost partition. NULL is considered smaller than the minimum value of the data type’s values.

 

Partition Function 定义了Partition column在每一个分区的value range,这种value range就像每一个Partition的数据都有一个check 约束,用于限定Partition column的取值范围。

Partition Number=1,Partition column允许存在null;

其他Partition,Partition column不允许存在null。

 

在check 约束中,如果出现Unknown,Check约束视为True。例如 constraint CK__TableName_ID check(ID>1 and ID<10), 如果ID=Null,那么表达式ID>1 and ID<10 返回Unknown(或null),Check约束逻辑运算的结果是True,即不违反check约束。

 

1,创建实例数据

-- create parition function
CREATE PARTITION FUNCTION pf_int_Left (int)
AS 
RANGE LEFT 
FOR VALUES (10,20);

--create partition scheme
CREATE PARTITION SCHEME PS_int_Left
AS 
PARTITION pf_int_Left
TO ([primary], [primary], [primary]);

--create partitioned table
create table dbo.dt_partition
(
ID int,
Code int
)
on PS_int_Left (ID)

--Create staging table
create table dbo.dt_SwitchStaging
(
ID int,
Code int
)
on [primary]


--insert data into dbo.dt_partition
insert into dbo.dt_partition
(
ID,
Code
)
values(15,2),(1,1)

2,将partition 2的数据switch 到 dbo.dt_SwitchStaging

--swith partition 2 to staging table
alter table dbo.dt_partition
switch partition 2 
to dbo.dt_SwitchStaging

将staging table中的数据swith into partition 2

--switch staging table to partition 2
alter table dbo.dt_SwitchStaging
switch
to dbo.dt_partition partition 2

Msg 4982, Level 16, State 1, Line 2
ALTER TABLE SWITCH statement failed. Check constraints of source table ‘DB_Study.dbo.dt_SwitchStaging‘ allow values that are not allowed by range defined by partition 2 on target table ‘DB_Study.dbo.dt_partition‘.

为staging table增加check 约束,partition 2的value range是 ID>10 and ID<=20。

--add check constraint
alter table dbo.dt_SwitchStaging
with check
add constraint CK__dt_SwitchStaging_ID check(ID >10 and ID<=20)

执行swith 代码,依然出错,错误消息和上面相同,错误原因是忽略了Partition 2的partition column 不能为空的约束。

--add check constraint
alter table dbo.dt_SwitchStaging
with check
add constraint CK__dt_SwitchStaging_ID check(ID >10 and ID<=20 and ID is not null)


3,Partition Number=1 允许parition column 为null

--empty staging table
truncate table dbo.dt_SwitchStaging
go

--drop constraint
alter table dbo.dt_SwitchStaging
drop constraint CK__dt_SwitchStaging_ID 
go

alter table dbo.dt_partition
switch partition 1
to dbo.dt_SwitchStaging
go

--add check constraint
alter table dbo.dt_SwitchStaging
with check
add constraint CK__dt_SwitchStaging_ID check(ID <=10 )
go

--insert null
insert into dbo.dt_SwitchStaging
(ID,Code)
values(null,1)
go

--switch staging table to partition 1
alter table dbo.dt_SwitchStaging
switch
to dbo.dt_partition partition 1
go

执行成功,创建的Check 约束允许 ID为null,并且 partition column也允许为Null。

 

Partition table的switch条件2:Partition 的隐式Check约束 和Nullability

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/5040109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!