You must Sign In to post a response.
  • Category: SQL Server

    Sql server Primary Key

    how to set Primary key in 2 Columns in same table.How to do.It is Posiple Or Not.Posible merans how to do..............very urgent
  • #746671
    Hi,

    Refer the below SQL Script to set 2 Primary key Columns in a table

    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    SET ANSI_PADDING ON
    GO

    CREATE TABLE [dbo].[TestTable](
    [ID] INT NOT NULL,
    [Name] [char](7) NOT NULL,
    [Place] [char](10) NULL,
    CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED
    (
    [ID] ASC,
    [Name] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF
    GO

    Let me know if you have any issues.

    Regards
    Siva

  • #746672
    first create the table with out primary key

    take for example you are creating a table like this


    CREATE TABLE dbo.Employee
    (
    EMPID INT
    , EmpFirstName VARCHAR(50)
    , EmpLastName VARCHAR(50)
    )


    Now you can create primary key using the columns EmpFirstName and EmpLastName like


    CREATE CLUSTERED INDEX XI_EmpName ON dbo.Employee
    (
    EmpFirstName, EmpLastName
    );



    This will create the primary index on EmpFirstname and EmpLastName. for more details about primary key in sql server, visit the below url

    http://dotnetbites.com/primary-key-sql-server

    Please mark this as Answer, if this helps

    Regards,
    Alwyn Duraisingh.M 
    << Database Administrator >>
    Jesus saves! The rest of us better make backups...

  • #746673
    Hi,

    You can able to set multiple columns in Primary Key relation.

    See below sample

    CREATE TABLE TABLENAME
    (
    col1 int not null,
    col2 int not null,
    col3 varchar(100),
    CONSTRAINT [PK_TABLENAME] PRIMARY KEY CLUSTERED
    (
    col1 ASC,
    col2 ASC
    )
    )


    Now, col1 and col2 both are in a primary key relationship.

    Hope this will help you to resolve your problem...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #746684
    You can create composite key for another column

  • #746704
    Yes you can assign more than one primary key to your table and it is called as Candidate key
    see below query snippet

    CREATE TABLE table(
    col1 VARCHAR,
    col2 VARCHAR,
    PRIMARY KEY (col1, col2)
    );

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #746712
    Hi,
    Suppose you already have a table and then you wanted to add primary key on multiple columns, use below query,


    ALTER TABLE TableName
    ADD CONSTRAINT PK_TestComposite
    PRIMARY KEY (col1, col2, col3)


    Let me know if you are looking for something different.


    Regards,
    Asheej T K

  • #747045
    primary does not contain two colunn
    it is uniqueness values of key
    so impossible

  • #747077
    you can have a composite key (2 or more key columns)


  • Sign In to post your comments