Sequence 序列

  1. 序列是一組順序整數如 1,2,3,... 的集合,常使用於資料庫中。
  2. 新增資料表 animals,包含 id,name,foot 三個欄位,其中 id 定義為 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
    
  3. 於資料表 animals 新增三筆資料,其中 id 故意設定為空值。
    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
    
  4. 查詢資料表 animals 欄位。
    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
    
  5. 查詢資料表 animals,欄位 id 並沒有因用戶設定為空值而真的為空值,而是從 1 開始編號,每筆自動增加 1。
    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
    
  6. 刪除 id=3 的紀錄。
    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
    
  7. 再新增一筆 chicken 紀錄。
    MariaDB root@(none):dyw> insert into animals(name,foot) values ('chicken',2);
    Query OK, 1 row affected
    Time: 0.004s
    
  8. 查詢資料表 animals 中 chicken 紀錄的 id=4,並不是 3。
    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
    
  9. 因為刪除了 id=3 的紀錄,新增的紀錄 id=4,造成 id 不連續,如果要重新編號,必須先刪除 id 欄位,再重新產生,其編號就會重新編號。 不過要特別注意的是,若此資料表與其他資料表關聯,可能會造成資料對應錯誤,要特別小心。
  10. 刪除欄位 id。
    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
    
  11. 再增加 id 欄位於資料表 animals 最前頭 (first 就是指定在第一列) 。
    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
    
  12. 查詢資料表 animals,id 編號已從 1 重新編號。
    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
    
  13. 設定往後新增的紀錄,id 編號從 15 編起。
    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
    
  14. 新增一筆紀錄。
    MariaDB root@(none):dyw> insert into animals(name,foot) values ('bird',2);
    Query OK, 1 row affected
    Time: 0.005s
    
  15. 查詢資料表 animals,新增的紀錄 id 編號已從 15 開始。
    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