Adding a primary key to a table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • williamkimber
    New Member
    • Dec 2010
    • 10

    Adding a primary key to a table

    Hi i'm having trouble with adding a primary key to a pre created table.

    The syntax below is what i'm using to add a primary key to my university table

    Code:
    ALTER TABLE UNIVERSITY ADD CONSTRAINT UNIVERSITY PK PRIMARY KEY(U_PK);
    The error i get is INVALID TABLE COMMAND

    Can anybody help?

    Many thanks!!
  • stepterr
    New Member
    • Nov 2007
    • 157

    #2
    Here's an example of the syntax.

    Code:
    ALTER TABLE Customer ADD PRIMARY KEY (SID);

    Comment

    • Jerry Winston
      Recognized Expert New Member
      • Jun 2008
      • 145

      #3
      Notice the blank space characters in you primary key name "University PK". SQL is expecting a single name. You could add an under score like :
      Code:
      ALTER TABLE UNIVERSITY ADD CONSTRAINT UNIVERSITY_PK PRIMARY KEY(U_PK);
      or enclose the key name in brackets:
      Code:
      ALTER TABLE UNIVERSITY ADD CONSTRAINT [UNIVERSITY PK] PRIMARY KEY(U_PK);
      Also, take a look at stepterr's syntax. I think you don't intend CONSTRAINT to be part of the primary key name. SQL is trying to use it as a keyword to build the key.

      Comment

      Working...