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. Setting PRIMARY KEY using Alter Table

Setting PRIMARY KEY using Alter Table

Scheduled Pinned Locked Moved Database
helpquestion
4 Posts 3 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.
  • W Offline
    W Offline
    Widgets
    wrote on last edited by
    #1

    Guys im having a problem with this statement.. ALTER TABLE tblSample ADD SampleField nvarchar (30) NOT NULL CONSTRAINT SampleField PRIMARY KEY im just trying to set my newly added column and at the same time setting it as a primary key.. but it wont work.. Is there any other way? or can it set an existing non primary column to a primary one.. if it can then how.. thanks..

    G M 2 Replies Last reply
    0
    • W Widgets

      Guys im having a problem with this statement.. ALTER TABLE tblSample ADD SampleField nvarchar (30) NOT NULL CONSTRAINT SampleField PRIMARY KEY im just trying to set my newly added column and at the same time setting it as a primary key.. but it wont work.. Is there any other way? or can it set an existing non primary column to a primary one.. if it can then how.. thanks..

      G Offline
      G Offline
      GuyThiebaut
      wrote on last edited by
      #2

      Widgets wrote:

      but it wont work..

      Please supply error messages etc as "won't work" is an interpretation and not an observation of what actually happened. Have you checked that SampleField contains unique values? Primary keys can only be assigned to columns with unique values.

      You always pass failure on the way to success.
      1 Reply Last reply
      0
      • W Widgets

        Guys im having a problem with this statement.. ALTER TABLE tblSample ADD SampleField nvarchar (30) NOT NULL CONSTRAINT SampleField PRIMARY KEY im just trying to set my newly added column and at the same time setting it as a primary key.. but it wont work.. Is there any other way? or can it set an existing non primary column to a primary one.. if it can then how.. thanks..

        M Offline
        M Offline
        Mike Dimmick
        wrote on last edited by
        #3

        Several problems: - You haven't specified a default value for SampleField and have disallowed NULLs so the database doesn't know what value to give that field for existing rows in the table (it would normally use NULL but you've disallowed it); - Even when you do add a default value or allow NULLs primary keys must be built on unique values, and every row in the table will then have the same value in that column; - The SQL syntax for ALTER TABLE doesn't allow you to do both at the same time anyway; - The SQL syntax for adding a primary key is:

        ALTER TABLE tblSample
        ADD CONSTRAINT PK_tblSample PRIMARY KEY
        ( SampleField )

        PK_tblSample is the name I've given to the constraint, which you need if you later want to drop it. If using CREATE TABLE, you can set the primary key to a single column simply by stating PRIMARY KEY after the column definition, but the name is generated by SQL Server making it harder to use later. You can define a primary key on a table that doesn't already have one by using the syntax above. You can create compound keys - keys consisting of multiple column values - by including all the key columns between the parentheses.


        DoEvents: Generating unexpected recursion since 1991

        W 1 Reply Last reply
        0
        • M Mike Dimmick

          Several problems: - You haven't specified a default value for SampleField and have disallowed NULLs so the database doesn't know what value to give that field for existing rows in the table (it would normally use NULL but you've disallowed it); - Even when you do add a default value or allow NULLs primary keys must be built on unique values, and every row in the table will then have the same value in that column; - The SQL syntax for ALTER TABLE doesn't allow you to do both at the same time anyway; - The SQL syntax for adding a primary key is:

          ALTER TABLE tblSample
          ADD CONSTRAINT PK_tblSample PRIMARY KEY
          ( SampleField )

          PK_tblSample is the name I've given to the constraint, which you need if you later want to drop it. If using CREATE TABLE, you can set the primary key to a single column simply by stating PRIMARY KEY after the column definition, but the name is generated by SQL Server making it harder to use later. You can define a primary key on a table that doesn't already have one by using the syntax above. You can create compound keys - keys consisting of multiple column values - by including all the key columns between the parentheses.


          DoEvents: Generating unexpected recursion since 1991

          W Offline
          W Offline
          Widgets
          wrote on last edited by
          #4

          thanks guy i really appreaciate your help..

          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