Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to add primary key in existing table?

How to add primary key in existing table?

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    apple blue
    wrote on last edited by
    #1

    Hi all :laugh: Currently now I used MS SQL Server Management Studio 2008. I have an existing table with data already store on that table. Table Name : Application Column : Categories (nvarchar(1), null) Sequence (nvarchar(1), null) Detail (nvarchar(150), null) I would like to ask how to add primary key to column Categories and Sequence ? Do I need to use alter query? Thank you in advance !

    P D D 3 Replies Last reply
    0
    • A apple blue

      Hi all :laugh: Currently now I used MS SQL Server Management Studio 2008. I have an existing table with data already store on that table. Table Name : Application Column : Categories (nvarchar(1), null) Sequence (nvarchar(1), null) Detail (nvarchar(150), null) I would like to ask how to add primary key to column Categories and Sequence ? Do I need to use alter query? Thank you in advance !

      P Offline
      P Offline
      Peter Leow
      wrote on last edited by
      #2

      Yes.

      ALTER TABLE application
      ADD CONSTRAINT pk_application PRIMARY KEY (categories,sequence)

      Learn more: 1. https://technet.microsoft.com/en-us/library/ms190273(v=sql.105).aspx[^] 2. http://www.w3schools.com/sql/sql_primarykey.asp[^]

      A 1 Reply Last reply
      0
      • P Peter Leow

        Yes.

        ALTER TABLE application
        ADD CONSTRAINT pk_application PRIMARY KEY (categories,sequence)

        Learn more: 1. https://technet.microsoft.com/en-us/library/ms190273(v=sql.105).aspx[^] 2. http://www.w3schools.com/sql/sql_primarykey.asp[^]

        A Offline
        A Offline
        apple blue
        wrote on last edited by
        #3

        Alright ! i got it now. Thank you Peter :-D

        1 Reply Last reply
        0
        • A apple blue

          Hi all :laugh: Currently now I used MS SQL Server Management Studio 2008. I have an existing table with data already store on that table. Table Name : Application Column : Categories (nvarchar(1), null) Sequence (nvarchar(1), null) Detail (nvarchar(150), null) I would like to ask how to add primary key to column Categories and Sequence ? Do I need to use alter query? Thank you in advance !

          D Offline
          D Offline
          Dhamodharan A
          wrote on last edited by
          #4

          Hi, for creating primary key u must need to maintain that columns as not null, so first execute this query to make it as not null

          ALTER TABLE D_D_ext
          ALTER COLUMN Categories NVARCHAR(1) NOT NULL ;
          ALTER TABLE D_D_ext
          ALTER COLUMN Sequence NVARCHAR(1) NOT NULL ;

          Then execute this query for creating Primary key

          ALTER TABLE D_D_ext
          ADD CONSTRAINT pk_D_D_ext_Categories_Sequence PRIMARY KEY (Categories, Sequence)

          Thanks dhamu

          A 1 Reply Last reply
          0
          • D Dhamodharan A

            Hi, for creating primary key u must need to maintain that columns as not null, so first execute this query to make it as not null

            ALTER TABLE D_D_ext
            ALTER COLUMN Categories NVARCHAR(1) NOT NULL ;
            ALTER TABLE D_D_ext
            ALTER COLUMN Sequence NVARCHAR(1) NOT NULL ;

            Then execute this query for creating Primary key

            ALTER TABLE D_D_ext
            ADD CONSTRAINT pk_D_D_ext_Categories_Sequence PRIMARY KEY (Categories, Sequence)

            Thanks dhamu

            A Offline
            A Offline
            apple blue
            wrote on last edited by
            #5

            Hi dhamu :) alright ! i try to apply the suggestion that you give. thank you for your response :-D

            1 Reply Last reply
            0
            • A apple blue

              Hi all :laugh: Currently now I used MS SQL Server Management Studio 2008. I have an existing table with data already store on that table. Table Name : Application Column : Categories (nvarchar(1), null) Sequence (nvarchar(1), null) Detail (nvarchar(150), null) I would like to ask how to add primary key to column Categories and Sequence ? Do I need to use alter query? Thank you in advance !

              D Offline
              D Offline
              Daniel Miller
              wrote on last edited by
              #6

              You can't define a primary key on a nullable column, so first you will need to ensure there are no null values in the Categories column and in the Sequence column. Then you can alter the table so these columns are not null:

              ALTER TABLE [Application] ALTER COLUMN Categories NVARCHAR(1) NOT NULL
              ALTER TABLE [Application] ALTER COLUMN Sequence NVARCHAR(1) NOT NULL

              Then you can create your primary key:

              ALTER TABLE [Application] ADD CONSTRAINT PK_Application PRIMARY KEY (Categories, Sequence)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups