SQL通俗表轉分區表的辦法。本站提示廣大學習愛好者:(SQL通俗表轉分區表的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是SQL通俗表轉分區表的辦法正文
CREATE TABLE Sale(
[Id] [int] identity(1,1) NOT NULL, --主動增加
[Name] [varchar](16) NOT NULL,
[SaleTime] [datetime] NOT NULL,
constraint [PK_Sale] primary key clustered --創立主鍵
(
[Id] ASC
)
)
--拔出一些記載
insert Sale ([Name],[SaleTime]) values ('張三','2009-1-1')
insert Sale ([Name],[SaleTime]) values ('李四','2009-2-1')
insert Sale ([Name],[SaleTime]) values ('王五','2009-3-1')
insert Sale ([Name],[SaleTime]) values ('錢六','2010-4-1')
insert Sale ([Name],[SaleTime]) values ('趙七','2010-5-1')
insert Sale ([Name],[SaleTime]) values ('張三','2011-6-1')
insert Sale ([Name],[SaleTime]) values ('李四','2011-7-1')
insert Sale ([Name],[SaleTime]) values ('王五','2011-8-1')
insert Sale ([Name],[SaleTime]) values ('錢六','2012-9-1')
insert Sale ([Name],[SaleTime]) values ('趙七','2012-10-1')
insert Sale ([Name],[SaleTime]) values ('張三','2012-11-1')
insert Sale ([Name],[SaleTime]) values ('李四','2013-12-1')
insert Sale ([Name],[SaleTime]) values ('王五','2014-12-1')
alter table Sale drop constraint PK_Sale
--創立主鍵,但不設為集合索引
alter TABLE Sale add constraint PK_Sale primary key nonclustered
(
[ID] ASC
) ON [PRIMARY]
--創立分區函數
GO
create partition function [pf_Sale](SaleTime) as range left for values(N'2010-5-1T00:00:00.000', N'2012-9-1T00:00:00.000'
GO
--創立分區構造
GO
create partition scheme [pt_Sale] as partition [pf_Sale] TO ([Sale1], [Sale3], [Sale2])
GO
--創立一個應用[pt_Sale]架構
GO
create table [dbo].[AvCache](
[Id] [int] identity(1,1) NOT NULL, --主動增加
[Name] [varchar](16) NOT NULL,
[SaleTime] [datetime] NOT NULL,
)on [pt_Sale](SaleTime) --留意這裡應用[pt_Sale]架構,依據SaleTime 分區
-- 檢查應用情形
SELECT *, $PARTITION.[pt_Sale](SaleTime)
FROM dbo.[AvCache]