GO
USE PartTest
GO
- create filegroup
ALTER DATABASE PartTest ADD FILEGROUP FG2009AndBefore
ALTER DATABASE PartTest ADD FILEGROUP FG2010
ALTER DATABASE PartTest ADD FILEGROUP FG2011AndAfter
- add data file to each filegroup
ALTER DATABASE PartTest ADD FILE (NAME = N'FY2009AndBefore ', FILENAME = N'D: DATAPartTestFY2009AndBefore.ndf') TO FILEGROUP FG2009AndBefore
ALTER DATABASE PartTest ADD FILE (NAME = N'FY2010 ', FILENAME = N'D: DATAPartTestFY2010.ndf') TO FILEGROUP FG2010
ALTER DATABASE PartTest ADD FILE (NAME = N'FY2011AndAfter ', FILENAME = N'D: DATAPartTestFY201AndAfter.ndf') TO FILEGROUP FG2011AndAfter
Step 2: Create the partition function and partition scheme
USE PartTest
GO
CREATE PARTITION FUNCTION PFunc_NGD (DATETIME) AS RANGE RIGHT FOR VALUES ('2010-01-01', '2011-01-01')
GO
CREATE PARTITION SCHEME PScheme_NGD AS PARTITION PFunc_NGD TO (FG2009AndBefore, FG2010, FG2011AndAfter)
The partition function called PFunc_NGD defines the boundary value for the segments, which is the first day of 2010 and the first day of 2011. Like when you cut a string, only 2 cuts are needed to divide the string into 3 segments. Here, there are only two boundary values. Therefore the range of values of the segments will be as follows:
Paragraph 1: Before to 2009-12-31 23:59:59
Paragraph 2 : 2010-01-01 00:00:00 to 2010-12-31 23:59:59
Paragraph 3: 2011-01-01 00:00:00 later
Then the scheme PScheme_NGD uses PFunc_NGD function to 'attach' the segments to each filegroup . Thus paragraph 1 will go to FG2009AndBefore , paragraph 2 to FG2010 and paragraph 3 to FG2011AndAfter .
Note, the partition function is not the same as the user-defined function . In Management Studio, you see the partition function and partition scheme in the Database / Storage section .
Another note is that a partition function can be used for multiple partition schemes , both are generic objects in the database, not attached to a specific table. When defining a table (see step 4) you need to specify which partition scheme to use.
Step 4: Create a table using the partition scheme
USE PartTest
GO
CREATE TABLE dbo.BanHang (
BangHang_ID INT IDENTITY,
Right Now, DATETIME,
MaSP INT,
INTLuong INT,
INT
) ON PScheme_NGD (Right Delivery)
GO
CREATE CLUSTERED INDEX CI_BanHang_NGD ON dbo.BanHang (RightGiaoDich) ON PScheme_NGD (Right Delivery)
The ' ON PScheme_NGD (RightGiaoDich) ' clause in the two table creation and index creation commands above specifies the BanHang table and the index CI_BanHang_NGD created on the scheme PScheme_NGD , which means that it manages the data allocation. So BanHang table has been segmented. You can check which data is recorded:
SELECT $ PARTITION.PFunc_NGD ('2008-07-24')
SELECT $ PARTITION.PFunc_NGD ('2009-12-31')
SELECT $ PARTITION.PFunc_NGD ('2010-01-01')
SELECT $ PARTITION.PFunc_NGD ('2010-11-25')
SELECT $ PARTITION.PFunc_NGD ('2011-03-16')