Share on Google+Share on Google+

MySQL Auto_Increment

Advertisement
This example illustrates how to define AUTO_INCREMENT of a field.

MySQL Auto_Increment

This example illustrates how to define AUTO_INCREMENT of a field.

The AUTO_INCREMENT attribute can be used to generate a unique identity. Here we make PRIMARY KEY to the id.

 

 

Query

 

CREATE TABLE animals (
      id MEDIUMINT NOT NULL AUTO_INCREMENT,
      name CHAR(30) NOT NULL,
      PRIMARY KEY (id)
  );

 

Query

 

INSERT INTO animals (name) VALUES
     ('dog'),('cat'),('penguin'),
     ('lax'),('whale'),('ostrich');

 

Query

 

SELECT * FROM animals;

 

Output

 

+----+--------------------------------+
| id | name                           |
+----+--------------------------------+
| 1  | dog                            |
| 2  | cat                            |
| 3  | penguin                        |
| 4  | lax                            |
| 5  | whale                          |
| 6  | ostrich                        |
+----+--------------------------------+

 

Advertisements

Posted on: December 9, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.