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

  • SEARCH
  • Home
  • 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 5987005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:45:10+00:00 2026-05-22T22:45:10+00:00

It is possible to create a primary key or unique index within a SQL

  • 0

It is possible to create a primary key or unique index within a SQL Server CREATE TABLE statement. Is it possible to create a non-unique index within a CREATE TABLE statement?

CREATE TABLE MyTable(
    a int NOT NULL
    ,b smallint NOT NULL
    ,c smallint NOT NULL
    ,d smallint NOT NULL
    ,e smallint NOT NULL

    -- This creates a primary key
    ,CONSTRAINT PK_MyTable PRIMARY KEY CLUSTERED (a)

    -- This creates a unique nonclustered index on columns b and c
    ,CONSTRAINT IX_MyTable1 UNIQUE (b, c)

    -- Is it possible to create a non-unique index on columns d and e here?
    -- Note: these variations would not work if attempted:
    -- ,CONSTRAINT IX_MyTable2 INDEX (d, e)
    -- ,CONSTRAINT IX_MyTable3 NONCLUSTERED INDEX (d, e)
);
GO

-- The proposed non-unique index should behave identically to
-- an index created after the CREATE TABLE statement. Example:
CREATE NONCLUSTERED INDEX IX_MyTable4 ON MY_TABLE (d, e);
GO

Again, the goal is to create the non-unique index within the CREATE TABLE statement, not after it.

For what it’s worth, I did not find the [SQL Server Books Online entry for CREATE TABLE] to be helpful.

Also, [This Question] is nearly identical, but the accepted answer does not apply.

  • 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-05-22T22:45:10+00:00Added an answer on May 22, 2026 at 10:45 pm

    As of SQL 2014, this can be accomplished via inline index creation:

    CREATE TABLE MyTable(
        a int NOT NULL
        ,b smallint NOT NULL
        ,c smallint NOT NULL
        ,d smallint NOT NULL
        ,e smallint NOT NULL
    
        -- This creates a primary key
        ,CONSTRAINT PK_MyTable PRIMARY KEY CLUSTERED (a)
    
        -- This creates a unique nonclustered index on columns b and c
        ,CONSTRAINT IX_MyTable1 UNIQUE (b, c)
    
        -- This creates a standard non-clustered index on (d, e)
        ,INDEX IX_MyTable4 NONCLUSTERED (d, e)
    );
    GO
    

    Prior to SQL 2014, CREATE/ALTER TABLE only accepted CONSTRAINTs to be added, not indexes. The fact that primary key and unique constraints are implemented in terms of an index is a side effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to create a primary key or unique index on an NVARCHAR(4000)
It's possible to create digrams in SQL Server 2000 that can be useful to
I have an events table with two columns eventkey (unique, primary-key) and createtime ,
Possible Duplicate: is this possible to made two primary key in one table Is
Is it possible to create federated Subversion servers? As in one server at location
Is it possible to create a trigger that will not be in a transaction?
Is it possible to create LIGHTWEIGHT transactions using TransactionScope() with SQL2000? Or if not,
I have a table with a primary key, but I want two other columns
Possible Duplicate: Relational database design question - Surrogate-key or Natural-key? When I create relational
I am seeing too frequently not null primary key in scripts creating tables in

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.