How to create a IDENTITY column in MySQL?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kcravimss
    New Member
    • May 2007
    • 1

    How to create a IDENTITY column in MySQL?

    Please help me to create an IDENTITY column in MySQL
  • pradeep kaltari
    Recognized Expert New Member
    • May 2007
    • 102

    #2
    Originally posted by kcravimss
    Please help me to create an IDENTITY column in MySQL
    Hi,
    You can create identity column as:
    1: While creating a table-->
    [code=sql]
    CREATE TABLE table_name
    (
    id INTEGER AUTO_INCREMENT PRIMARY KEY
    )
    [/code]
    The identity column needs to be specified as a key (PRIMARY or UNIQUE)

    2: If the table already exists-->
    [code=sql]
    ALTER TABLE table_name ADD COLUMN id INTEGER AUTO_INCREMENT UNIQUE KEY;
    [/code]
    I hope this helps.

    Regards,
    Pradeep.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Originally posted by pradeep kaltari
      [code=sql]
      CREATE TABLE table_name
      (
      id INTEGER AUTO_INCREMENT PRIMARY KEY
      )
      [/code]
      The identity column needs to be specified as a key (PRIMARY or UNIQUE)
      You can also do this:

      [code=mysql]
      CREATE TABLE table_name
      (
      id serial
      )
      [/code]

      Which is equivalent to:
      [code=mysql]
      CREATE TABLE table_name
      (
      id BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY auto_increment
      )
      [/code]

      Comment

      • parthi19
        New Member
        • Jan 2014
        • 3

        #4
        Code:
        create table table_name(id int(5), name varchar(30), primary key(id))

        Comment

        Working...