我可以在SQL Server 2005中更改分区表吗?

时间:2009-01-19 20:59:32

标签: sql-server sql-server-2005 partitioning

我正在研究一个包含时间序列数据的表的水平分区。我发现2005年的分区比2000年容易得多,但我似乎无法找到答案:

我可以添加/删除分区表的列吗?

是否需要特殊步骤,因为它已分区?

2 个答案:

答案 0 :(得分:2)

除了执行涉及分区表的SWITCH操作之外,ALTER TABLE还可用于更改分区表的列,约束和触发器的状态,就像它用于非分区表一样。但是,此语句不能用于更改表本身的分区方式。要重新分区分区表,请使用ALTER PARTITION SCHEME和ALTER PARTITION FUNCTION。此外,您无法更改分区表的列的数据类型。

在这里阅读更多内容: http://technet.microsoft.com/en-us/library/ms190273.aspx

答案 1 :(得分:1)

我找不到一个确定的答案(我发现doc @ E.J.Brennan的文字有点密集且不清楚)。所以我添加到this example并对其进行了测试,是的,您可以添加/删除列

USE adventureworks
go

create partition function YearPF(datetime) as range right for values ('20050101');

-- Now we need to add filegroups that will contains partitioned values
alter database adventureworks add filegroup YearFG1;
alter database adventureworks add filegroup YearFG2;

-- Now we need to add file to filegroups
alter database adventureworks add file (name = 'YearF1', filename = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdvWorksF1.ndf') to filegroup YearFG1;
alter database adventureworks add file (name = 'YearF2', filename = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdvWorksF2.ndf') to filegroup YearFG2;

-- Here we associate the partition function to 
-- the created filegroup via a Partitioning Scheme
create partition scheme YearPS as partition YearPF to (YearFG1, YearFG2)

-- Now just create a table that uses the particion scheme
create table PartitionedOrders
(
  Id int not null identity(1,1),
  DueDate DateTime not null,
) on YearPS(DueDate)

-- And now we just have to use the table!
insert into PartitionedOrders values('20020101')
insert into PartitionedOrders values('20030101')
insert into PartitionedOrders values('20040101')
insert into PartitionedOrders values('20050101')
insert into PartitionedOrders values('20060101')

-- Now we want to see where our values has falled
select *, $partition.YearPF(DueDate) from PartitionedOrders

-- see if we can add a column
ALTER TABLE PartitionedOrders ADD NewColumn INT NULL

-- add some more records, populating the new column
insert into PartitionedOrders values('20010101', 1)
insert into PartitionedOrders values('20070101', 2)

-- see that they were inserted properly
select *, $partition.YearPF(DueDate) from PartitionedOrders

ALTER TABLE PartitionedOrders DROP COLUMN NewColumn

-- see that the column dropped
select *, $partition.YearPF(DueDate) from PartitionedOrders

/* clean up
drop table PartitionedOrders
drop partition scheme YearPS;
drop partition function YearPF;
alter database adventureworks remove file YearF1;
alter database adventureworks remove file YearF2;
alter database adventureworks remove  filegroup YearFG1;
alter database adventureworks remove filegroup YearFG2;
*/