CREATE TABLE Sale(
[Id] [int] identity(1,1) NOT NULL, --自動(dòng)增長
[Name] [varchar](16) NOT NULL,
[SaleTime] [datetime] NOT NULL,
constraint [PK_Sale] primary key clustered --創(chuàng)建主鍵
(
[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
--創(chuàng)建主鍵,但不設(shè)為聚集索引
alter TABLE Sale add constraint PK_Sale primary key nonclustered
(
[ID] ASC
) ON [PRIMARY]
--創(chuàng)建分區(qū)函數(shù)
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
--創(chuàng)建分區(qū)結(jié)構(gòu)
GO
create partition scheme [pt_Sale] as partition [pf_Sale] TO ([Sale1], [Sale3], [Sale2])
GO
--創(chuàng)建一個(gè)使用[pt_Sale]架構(gòu)
GO
create table [dbo].[AvCache](
[Id] [int] identity(1,1) NOT NULL, --自動(dòng)增長
[Name] [varchar](16) NOT NULL,
[SaleTime] [datetime] NOT NULL,
)on [pt_Sale](SaleTime) --注意這里使用[pt_Sale]架構(gòu),根據(jù)SaleTime 分區(qū)
-- 查看使用情況
SELECT *, $PARTITION.[pt_Sale](SaleTime)
FROM dbo.[AvCache]