auto_increment 的主鍵,讓該列數字編號不重複,且每筆紀錄新增時自動加 1。
MariaDB root@(none):dyw> create table animals(id int unsigned not null auto_incre
-> ment,primary key(id),name varchar(20) not null, foot tin
-> yint unsigned not null);
Query OK, 0 rows affected
Time: 0.048s
MariaDB root@(none):dyw> insert into animals(id,name,foot) values (null,'dog',4),
-> (null,'cat',4),(null,'bird',2);
Query OK, 3 rows affected
Time: 0.007s
MariaDB root@(none):dyw> desc animals; +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | <null> | auto_increment | | name | varchar(20) | NO | | <null> | | | foot | tinyint(3) unsigned | NO | | <null> | | +-------+---------------------+------+-----+---------+----------------+ 3 rows in set Time: 0.027s
MariaDB root@(none):dyw> select * from animals; +----+------+------+ | id | name | foot | +----+------+------+ | 1 | dog | 4 | | 2 | cat | 4 | | 3 | bird | 2 | +----+------+------+ 3 rows in set Time: 0.021s
MariaDB root@(none):dyw> delete from animals where id=3; You're about to run a destructive command. Do you want to proceed? (y/n): y Your call! Query OK, 1 row affected Time: 0.010s
MariaDB root@(none):dyw> insert into animals(name,foot) values ('chicken',2);
Query OK, 1 row affected
Time: 0.004s
MariaDB root@(none):dyw> select * from animals; +----+---------+------+ | id | name | foot | +----+---------+------+ | 1 | dog | 4 | | 2 | cat | 4 | | 4 | chicken | 2 | +----+---------+------+ 3 rows in set Time: 0.023s
MariaDB root@(none):dyw> alter table animals drop id; You're about to run a destructive command. Do you want to proceed? (y/n): y Your call! Query OK, 3 rows affected Time: 0.083s
MariaDB root@(none):dyw> alter table animals add id int unsigned not null auto_in
-> crement first, add primary key(id);
You're about to run a destructive command.
Do you want to proceed? (y/n): y
Your call!
Query OK, 0 rows affected
Time: 0.080s
MariaDB root@(none):dyw> select * from animals; +----+---------+------+ | id | name | foot | +----+---------+------+ | 1 | dog | 4 | | 2 | cat | 4 | | 3 | chicken | 2 | +----+---------+------+ 3 rows in set Time: 0.022s
MariaDB root@(none):dyw> alter table animals auto_increment=15; You're about to run a destructive command. Do you want to proceed? (y/n): y Your call! Query OK, 0 rows affected Time: 0.014s
MariaDB root@(none):dyw> insert into animals(name,foot) values ('bird',2);
Query OK, 1 row affected
Time: 0.005s
MariaDB root@(none):dyw> select * from animals; +----+---------+------+ | id | name | foot | +----+---------+------+ | 1 | dog | 4 | | 2 | cat | 4 | | 3 | chicken | 2 | | 15 | bird | 2 | +----+---------+------+ 4 rows in set Time: 0.020s