quote: Originally posted by WoodHouseThat is the rule of partition, ie partition column must be part of the primary key
That's not quite true. You can partition a table on a field that's not in the primary key. But to do so, the primary key must be declared using a specific filegroup. Here's an example. Here, the table is partitioned across filegroups FG1, FG2, and FG3, and the PK is stored on FG4. Notice the PK is the WidgetID (INT) field, yet the partition is on the DateAdded (DATETIME) field, which is not part of the primary key. To run this script, you'll need to create the folder 'D:\test'USE masterGOCREATE DATABASE PartitionTestON PRIMARY ( NAME = db_dat, FILENAME = 'D:\test\db.mdf', SIZE = 2MB),FILEGROUP FG1 ( NAME = FG1_dat, FILENAME = 'D:\test\FG1.ndf', SIZE = 2MB),FILEGROUP FG2 ( NAME = FG2_dat, FILENAME = 'D:\test\FG2.ndf', SIZE = 2MB),FILEGROUP FG3 ( NAME = FG3_dat, FILENAME = 'D:\test\FG3.ndf', SIZE = 2MB),FILEGROUP FG4 ( NAME = FG4_dat, FILENAME = 'D:\test\FG4.ndf', SIZE = 2MB)LOG ON ( NAME = db_log, FILENAME = 'D:\test\db_log.ldf', SIZE = 2MB)GOUSE PartitionTestGOCREATE PARTITION FUNCTION partfunc(DATETIME) ASRANGE LEFT FOR VALUES ('20100101', '20100201')GOCREATE PARTITION SCHEME partscheme AS PARTITION partfunc TO([FG1], [FG2], [FG3])GOCREATE TABLE Widgets ( WidgetID INT NOT NULL IDENTITY(1, 1) PRIMARY KEY CLUSTERED ON [FG4], WidgetName VARCHAR(100) NOT NULL, DateAdded DATETIME NOT NULL) ON partscheme(DateAdded)GOINSERT INTO Widgets (WidgetName, DateAdded)SELECT 'Widget 1', '20091001' UNION ALL SELECT 'Widget 2', '20091001' UNION ALL SELECT 'Widget 3', '20100101' UNION ALL SELECT 'Widget 4', '20100103' UNION ALL SELECT 'Widget 5', '20100112' UNION ALL SELECT 'Widget 6', '20100201' UNION ALL SELECT 'Widget 7', '20100202' UNION ALL SELECT 'Widget 8', '20100208' GOSELECT * FROM Widgets /*USE master GODROP DATABASE PartitionTest*/------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |