Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8935671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:02:39+00:00 2026-06-15T10:02:39+00:00

The "Create Table" grammar rather clearly does not allow me to specify a clustered

  • 0

The "Create Table" grammar rather clearly does not allow me to specify a clustered foreign key constraint. In other words, this is illegal:

--keyword CLUSTERED must be removed before this will execute...
CREATE TABLE [Content](
    [ID] [int] NOT NULL CONSTRAINT PK_Content_ID PRIMARY KEY,
    ContentDefID int NOT NULL CONSTRAINT FK_Plugin_ContentDef FOREIGN KEY CLUSTERED REFERENCES ContentDef(ID)
    )
GO

But I don’t understand why it is illegal. ISTM that clustering a foreign-key would facilitate performance of paged-lookups. In other words, "give me child items 80 through 140 of parent ID 20".

Is there a rationale for this?

Update

Based on Oded and Tvanfosson feedback, I’ve found that the following works:

CREATE TABLE [Content](
    [ID] [int] NOT NULL CONSTRAINT PK_Content_ID PRIMARY KEY,
    ContentDefID int NOT NULL UNIQUE CLUSTERED CONSTRAINT FK_ContentDefContent FOREIGN KEY REFERENCES ContentDef(ID)
    )
GO

But the above causes more problems than it solves. First, a "UNIQUE" foreign key forces my relationship to be one-to-one which I don’t want. Second, this only works because it represents the creation of two separate constraints, rather than a single CLUSTERED FOREIGN KEY.

But this investigation is getting me closer to my answer. Evidently clustered indexes MUST be unique, as stated here on SO. Quoting:

If the clustered index is not a unique index, SQL Server makes any duplicate keys unique by adding an internally generated value called a uniqueifier

In particular, I think this answer covers it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:02:40+00:00Added an answer on June 15, 2026 at 10:02 am

    As others have explained, the clustered index does not have to be the primary key but it either has to be unique or SQL-Server adds a (not shown) UNIQUIFIER column to it.

    To avoid this, you can make the clustered index unique by explicitly adding the primary key column to the clustered index, like below. The index will then be avaialbel to be used by the foreign key constraints (and for queries, like joining the two tables).

    Notice, that as @Martin Smith has explained, the concepts of CONSTRAINT and INDEX are different. And the various DBMSs implement these in different ways. SQL-Server automatically creates an index for some constraints, while it doesn’t for foreign key constraints. It’s advised though to have an index that the constraint can use (when deleting or updating in the referenced table):

    CREATE TABLE Content(
        ID int NOT NULL,
        ContentDefID int NOT NULL,
        CONSTRAINT PK_Content_ID 
          PRIMARY KEY NONCLUSTERED (ID),
        CONSTRAINT CI_Content
          UNIQUE CLUSTERED (ContentDefID, ID),
        CONSTRAINT FK_Plugin_ContentDef 
          FOREIGN KEY (ContentDefID) REFERENCES ContentDef(ID)
    ) ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With this schema: create table object ( obj_id serial primary key, name varchar(80) not
Using JavaScript, how do I create an HTML table that can "accept" numeric matrix
I have this table: CREATE TABLE `search_engine_rankings` ( `id` int(11) NOT NULL AUTO_INCREMENT, `keyword_id`
if I have create table t1( attr text primary key, val text ); insert
I have a table with this layout: CREATE TABLE Favorites ( FavoriteId uuid NOT
I created an SQLite table in Java: create table participants (ROWID INTEGER PRIMARY KEY
I am trying to create a table with an auto-incrementing primary key in Sqlite3
I've created a table with a primary key and enabled AUTO_INCREMENT : CREATE TABLE
<?php try { $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $sql = $DBH->exec(" CREATE TABLE
I'd like to create a customized checkbox that behaves exactly like StackOverflow's "accept answer"

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.