Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Partition key should be part of Primary key Expand / Collapse
Author
Message
Posted Monday, October 04, 2010 9:35 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, March 23, 2011 10:41 AM
Points: 3, Visits: 24
Hi,

I would like to know, when I'm creating a partition on a table, partition field should be part of primary key or unique key?

More specifically, I have table with existing 2 columns (example ColumnA, ColumnB) in the primary key. I would like create a partition on ColumnC. Is it necessary to add ColumnC as a part of primary key?

Any comments will be appreciated.
Post #997646
Posted Monday, October 04, 2010 12:19 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, May 06, 2013 6:51 AM
Points: 989, Visits: 1,790
I wouldn't think so. If, for instance, you partition on date, you won't want the date to be in the primary key. The partition value is not used to uniquely identify the record, but merely to determine its location on the partitioning scheme. I would choose to leave them separate as there is no gain by adding the partition column to the primary key.
Post #997773
Posted Monday, October 04, 2010 12:35 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, March 23, 2011 10:41 AM
Points: 3, Visits: 24
Logically, it does NOT make sense to add partition column as part of primary key and I know databases like Oracle has no such limitation. But a SQLServer DBA insists, SQLServer partition design require to have partition key as part of primary key.
Post #997779
Posted Monday, October 04, 2010 12:40 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, May 06, 2013 6:51 AM
Points: 989, Visits: 1,790
This is just wrong. I know of no requirement like that. Partitioning has nothing to do with referential integrity and it shouldn't even enter into the discussion.
Post #997783
Posted Monday, October 04, 2010 12:43 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, February 12, 2013 1:09 PM
Points: 335, Visits: 391
No it does not need to be part of the primary key. In fact most partitions are not on the primary key but on some date field. Which is what Jeff was trying to say above I think. So keep your primary key as-is.
Post #997786
Posted Thursday, October 21, 2010 6:33 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, February 21, 2013 10:48 PM
Points: 36, Visits: 349
Hey Guys
I'm currently trying to create a table with a partition key that is not part of the primary key, but afetr I create the partition function,scheme ansd table and insert values into the table, they are not getting partitioned. I have provided the scripts below

--- Create Partition Range Function
CREATE PARTITION FUNCTION TestDB_PartitionRange (datetime)
AS RANGE LEFT FOR
VALUES ('2010-10-21');
GO

--- Attach Partition Scheme to FileGroups
CREATE PARTITION SCHEME TestDB_PartitionScheme
AS PARTITION TestDB_PartitionRange
TO ([PRIMARY], Secondary);
GO

--Create table on partition scheme
CREATE TABLE TestTable
(ID INT NOT NULL,
Date DATETIME
,CONSTRAINT [pc_work_PK_new] PRIMARY KEY CLUSTERED ([ID] ASC ) on [primary]
)ON TestDB_PartitionScheme (Date);
GO


--- Insert Data in Partitioned Table
INSERT INTO TestTable (ID, Date) -- Inserted in Partition 1
VALUES (1,'2010-10-20');
INSERT INTO TestTable (ID, Date) -- Inserted in Partition 2
VALUES (11,'2010-10-21');
INSERT INTO TestTable (ID, Date) -- Inserted in Partition 2
VALUES (12,'2010-10-22');
GO


--- Verify Rows Inserted in Partitions
SELECT *
FROM sys.partitions
WHERE OBJECT_NAME(OBJECT_ID)='TestTable';
GO

When I verify that rows are in the different partitons, they are not.

Any help appreciated
Post #1008904
Posted Tuesday, December 14, 2010 5:24 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, December 27, 2012 10:40 AM
Points: 245, Visits: 78
Hi,

try this:

--- Create Partition Range Function
CREATE PARTITION FUNCTION TestDB_PartitionRange (datetime)
AS RANGE LEFT FOR
VALUES ('2010-10-21');
GO

--- Attach Partition Scheme to FileGroups
CREATE PARTITION SCHEME TestDB_PartitionScheme
AS PARTITION TestDB_PartitionRange
TO ([PRIMARY], [Secondary]);
GO

--Create table on partition scheme
CREATE TABLE TestTable
(ID INT NOT NULL,
Date DATETIME
)ON TestDB_PartitionScheme (Date);
GO

CREATE CLUSTERED INDEX [IX_TestTable] ON TestTable([ID] ASC, [Date] ASC) ON TestDB_PartitionScheme(Date)
GO

ALTER TABLE TestTable ADD CONSTRAINT [pc_work_PK_new] PRIMARY KEY ([ID] ASC ) ON [PRIMARY]
GO

--- Insert Data in Partitioned Table
INSERT INTO TestTable (ID, Date) -- Inserted in Partition 1
VALUES (1,'2010-10-20');
INSERT INTO TestTable (ID, Date) -- Inserted in Partition 2
VALUES (11,'2010-10-21');
INSERT INTO TestTable (ID, Date) -- Inserted in Partition 2
VALUES (12,'2010-10-22');
GO


--- Verify Rows Inserted in Partitions
SELECT *
FROM sys.partitions
WHERE OBJECT_NAME(OBJECT_ID)='TestTable';
GO


I don't know if it's the best solution, but it works.
Post #1034306
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse