Using AUTO_INCREMENT : AUTO_INCREMENT « Data Type « SQL / MySQL






Using AUTO_INCREMENT

    
/*
mysql> SELECT * FROM Employee;
+----+-------+
| id | name  |
+----+-------+
|  1 | Joe   |
|  2 | Yin   |
|  3 | peng  |
|  4 | Lava  |
|  5 | White |
|  6 | Ola   |
+----+-------+
6 rows in set (0.02 sec)

*/  
/* AUTO_INCREMENT : used to generate a unique identity for new rows:
  */
Drop table Employee;

CREATE TABLE Employee (
             id MEDIUMINT NOT NULL AUTO_INCREMENT,
             name CHAR(30) NOT NULL,
             PRIMARY KEY (id)
             );
             
INSERT INTO Employee (name) VALUES ('Joe'),
                                  ('Yin'),
                                  ('peng'),
                                  ('Lava'),
                                  ('White'),
                                  ('Ola');
SELECT * FROM Employee;


           
         
    
    
    
  








Related examples in the same category

1.AUTO_INCREMENT Integers
2.Preserve column attributes such as AUTO_INCREMENT and column's default value during table copying.
3.An AUTO_INCREMENT column
4.AUTO_INCREMENT sequences start at one
5.For MyISAM tables, you can begin the sequence at a specific initial value n by including an AUTO_INCREMENT = n
6.Using an AUTO_INCREMENT Column to Create Multiple Sequences
7.AUTO_INCREMENT column:
8.Not null tinyint and AUTO_INCREMENT
9.MEDIUMINT NOT NULL AUTO_INCREMENT
10.INTEGER UNSIGNED AUTO_INCREMENT
11.Insert value to AUTO_INCREMENT column
12.delete and reinsert value to AUTO_INCREMENT column
13.SET @@AUTO_INCREMENT_OFFSET = 10,@@AUTO_INCREMENT_INCREMENT = 10
14.Set AUTO_INCREMENT value
15.Parameter: INT NOT NULL AUTO_INCREMENT
16.Create a similar table, preserve the auto-incrementing key
17.Defining Auto-Increment Columns
18.If this value is larger than the current sequence counter, subsequent automatically generated values begin with the value plus one