472,126 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

How to create a IDENTITY column in MySQL?

Please help me to create an IDENTITY column in MySQL
May 24 '07 #1
3 116378
pradeep kaltari
102 Expert 100+
Please help me to create an IDENTITY column in MySQL
Hi,
You can create identity column as:
1: While creating a table-->
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE table_name
  2. (
  3.    id INTEGER AUTO_INCREMENT PRIMARY KEY
  4. )
  5.  
The identity column needs to be specified as a key (PRIMARY or UNIQUE)

2: If the table already exists-->
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE table_name ADD COLUMN id INTEGER AUTO_INCREMENT UNIQUE KEY;
  2.  
I hope this helps.

Regards,
Pradeep.
May 24 '07 #2
pbmods
5,821 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE table_name
  2. (
  3.    id INTEGER AUTO_INCREMENT PRIMARY KEY
  4. )
  5.  
The identity column needs to be specified as a key (PRIMARY or UNIQUE)
You can also do this:

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE table_name
  2. (
  3.     id serial
  4. )
  5.  
Which is equivalent to:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE table_name
  2. (
  3.     id BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY auto_increment
  4. )
  5.  
May 25 '07 #3
Expand|Select|Wrap|Line Numbers
  1. create table table_name(id int(5), name varchar(30), primary key(id))
Jan 24 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by WhiteEagl | last post: by
2 posts views Thread by Omavlana Omav | last post: by
8 posts views Thread by shenanwei | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.