I am creating a partitioned table called TestArticles, specifying several filegroups according to the year of their publication (publishDate). This code (excluding the commented parts) executes correctly. My task is to add a unique index to the 'hash' field. When I try to do this in the table creation code, I receive the following error:
'Column 'publishDate' is a partitioning column of the index 'UQ_Articles_hash. Partitioning columns of a unique index must be a subset of the index key.'
I can create a composite primary key from (id, publishDate, hash) - but that is not what is required of me.
Is there any way to specify hash as a unique index for each created filegroup or to designate it as such when initializing the entire table?
USE Articles;
GO
ALTER DATABASE Articles
ADD FILEGROUP Articles2024;
ALTER DATABASE Articles
ADD FILEGROUP Articles2025;
ALTER DATABASE Articles
ADD FILEGROUP Articles2026;
ALTER DATABASE Articles
ADD FILEGROUP Articles2027;
CREATE PARTITION FUNCTION PF_Articles_PublishDate (DATETIME)
AS RANGE RIGHT FOR VALUES
(
'2024-01-01',
'2025-01-01',
'2026-01-01'
);
CREATE PARTITION SCHEME PS_Articles_PublishDate
AS PARTITION PF_Articles_PublishDate
TO
(
Articles2024,
Articles2025,
Articles2026,
Articles2027
);
CREATE TABLE TestArticles (
id INT NOT NULL,
path VARCHAR(200) NULL,
description VARCHAR(100) NOT NULL,
publishDate DATETIME NOT NULL,
hash BIGINT NOT NULL,
authorId INT NOT NULL,
CONSTRAINT PK_Articles PRIMARY KEY CLUSTERED (id, publishDate),
CONSTRAINT FK_Articles_Authors FOREIGN KEY (authorId) REFERENCES dbo.Authors(id)
--,CONSTRAINT UQ_Articles_hash UNIQUE NONCLUSTERED (hash)
) ON PS_Articles_PublishDate (publishDate);
Database: MS SQL Server 2022 Version: 16.0.1000.6 Error details: Msg 1908; Level 16; State 1.