Use Auto_increment column as part of the key : AUTO_INCREMENT : Table MySQL TUTORIALS


MySQL TUTORIALS » Table » AUTO_INCREMENT »

 

Use Auto_increment column as part of the key


mysql>
mysql> CREATE TABLE employee (
    ->     grp ENUM('A','B','C'NOT NULL,
    ->     id MEDIUMINT NOT NULL AUTO_INCREMENT,
    ->     name CHAR(30NOT NULL,
    ->     PRIMARY KEY (grp,id)
    -> );
Query OK, rows affected (0.02 sec)

mysql>
mysql> INSERT INTO employee (grp,nameVALUES ('A','A1'),
    ->                                        ('B','B1'),
    ->                                        ('C','C1'),
    ->                                        ('A','A2'),
    ->                                        ('B','B2'),
    ->                                        ('C','C2');
Query OK, rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT FROM employee ORDER BY grp,id;
+-----+----+------+
| grp | id | name |
+-----+----+------+
| A   |  | A1   |
| A   |  | A2   |
| B   |  | B1   |
| B   |  | B2   |
| C   |  | C1   |
| C   |  | C2   |
+-----+----+------+
rows in set (0.00 sec)

mysql>
mysql>
mysql> drop table employee;
Query OK, rows affected (0.00 sec)

mysql>
mysql>



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .

Follow Navioo On Twitter

MySQL TUTORIALS

 Navioo Table
» AUTO_INCREMENT