AUTO_INCREMENT

Spiegazione

L'attributo AUTO_INCREMENT è utile per generare un'identità univoca per le righe.

Esempi

CREATE TABLE animali (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     nome CHAR(30) NOT NULL,
     PRIMARY KEY (id)
 );

INSERT INTO animals (nome) VALUES
    ('cane'),('gatto'),('pinguino'),
    ('volpe'),('balena'),('ostrica');
MariaDB [test]> SELECT * FROM animali;
+----+----------+
| id | nome     |
+----+----------+
|  1 | cane     |
|  2 | gatto    |
|  3 | pinguino |
|  4 | volpe    |
|  5 | balena   |
|  6 | ostrica  |
+----+----------+

Impostare il valore auto_increment:

MariaDB [test]> ALTER TABLE animals AUTO_INCREMENT=8;

MariaDB [test]> INSERT INTO animals (name) VALUES ('aardvark');

MariaDB [test]> SELECT * FROM animals;
+----+-----------+
| id | name |
+----+-----------+
| 1 | dog |
| 2 | cat |
| 3 | penguin |
| 4 | fox |
| 5 | whale |
| 6 | ostrich |
| 8 | aardvark |
+----+-----------+

Vedi anche

Commenti

Sto caricando i commenti......
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.