文档中心 > Sequence简单使用
Sequence简单使用

最近更新时间:2022-03-23

mysql> use single_1;

Database changed
mysql> create table customer_seq(id bigint, next_id bigint, cache bigint, primary key(id)) comment
    -> 'vitess_sequence';
Query OK, 0 rows affected (0.02 sec)



mysql> show tables;
+--------------------+
| Tables_in_single_1 |
+--------------------+
| customer_seq       |
+--------------------+
1 row in set (0.01 sec)

mysql> show columns from customer_seq;
+---------+--------+------+-----+---------+-------+
| Field   | Type   | Null | Key | Default | Extra |
+---------+--------+------+-----+---------+-------+
| id      | bigint | NO   | PRI | NULL    |       |
| next_id | bigint | YES  |     | NULL    |       |
| cache   | bigint | YES  |     | NULL    |       |
+---------+--------+------+-----+---------+-------+
3 rows in set (0.00 sec)


mysql> select * from single_1.customer_seq;
+----+---------+-------+
| id | next_id | cache |
+----+---------+-------+
|  0 |       1 |    10 |
+----+---------+-------+
1 row in set (0.00 sec)


mysql> alter vschema add sequence single_1.customer_seq;
Query OK, 0 rows affected (0.01 sec)

mysql> show vschema tables;
+--------------+
| Tables       |
+--------------+
| customer_seq |
| dual         |
+--------------+
2 rows in set (0.00 sec)

INSERT INTO customer_seq (id, next_id, cache) VALUES (0, 1000, 100);
mysql> select next 3 values from single_1.customer_seq;
+---------+
| nextval |
+---------+
|       1 |
+---------+
1 row in set (0.01 sec)

mysql> select next 1 values from single_1.customer_seq;
+---------+
| nextval |
+---------+
|      13 |
+---------+
1 row in set (0.00 sec)

vtctlclient -server 192.168.1.27:15999 GetVschema multi;
{
  "sharded": true,
  "vindexes": {
    "hash": {
      "type": "hash"
    }
  },
  "tables": {
    "customer": {
      "columnVindexes": [
        {
          "name": "hash",
          "columns": [
            "customer_id"
          ]
        }
      ],
      "autoIncrement": {
        "column": "customer_id",
        "sequence": "single_1.customer_seq"
      }
    }
  }
}

mysql> insert into customer(uname) values('alice'),('bob'),('charlie '),('dan'),('eve');
Query OK, 5 rows affected (0.01 sec)

mysql> select * from multi.customer;
+-------------+----------+
| customer_id | uname    |
+-------------+----------+
|           1 | zhaowu   |
|           2 | lili     |
|           3 | zhangsan |
|          16 | alice    |
|          17 | bob      |
|           4 | lisi     |
|          18 | charlie  |
|          19 | dan      |
|          20 | eve      |
+-------------+----------+
9 rows in set (0.01 sec)

mysql> use multi:-80;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> use multi:80-
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from customer;
+-------------+----------+
| customer_id | uname    |
+-------------+----------+
|           4 | lisi     |
|          18 | charlie  |
|          19 | dan      |
|          20 | eve      |
+-------------+----------+
4 rows in set (0.00 sec)