You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: auto-increment.md
+20-20
Original file line number
Diff line number
Diff line change
@@ -19,19 +19,19 @@ The following is a basic example of `AUTO_INCREMENT`:
19
19
{{< copyable "sql" >}}
20
20
21
21
```sql
22
-
createtablet(id intprimary key AUTO_INCREMENT, c int);
22
+
CREATETABLEt(id intPRIMARY KEY AUTO_INCREMENT, c int);
23
23
```
24
24
25
25
{{< copyable "sql" >}}
26
26
27
27
```sql
28
-
insert into t(c) values (1);
29
-
insert into t(c) values (2);
30
-
insert into t(c) values (3), (4), (5);
28
+
INSERT INTO t(c) VALUES (1);
29
+
INSERT INTO t(c) VALUES (2);
30
+
INSERT INTO t(c) VALUES (3), (4), (5);
31
31
```
32
32
33
33
```sql
34
-
mysql>select*from t;
34
+
mysql>SELECT*FROM t;
35
35
+----+---+
36
36
| id | c |
37
37
+----+---+
@@ -49,11 +49,11 @@ In addition, `AUTO_INCREMENT` also supports the `INSERT` statements that explici
49
49
{{< copyable "sql" >}}
50
50
51
51
```sql
52
-
insert into t(id, c) values (6, 6);
52
+
INSERT INTO t(id, c) VALUES (6, 6);
53
53
```
54
54
55
55
```sql
56
-
mysql>select*from t;
56
+
mysql>SELECT*FROM t;
57
57
+----+---+
58
58
| id | c |
59
59
+----+---+
@@ -76,13 +76,13 @@ TiDB implements the `AUTO_INCREMENT` implicit assignment in the following way:
76
76
For each auto-increment column, a globally visible key-value pair is used to record the maximum ID that has been assigned. In a distributed environment, communication between nodes has some overhead. Therefore, to avoid the issue of write amplification, each TiDB node applies for a batch of consecutive IDs as caches when assigning IDs, and then applies for the next batch of IDs after the first batch is assigned. Therefore, TiDB nodes do not apply to the storage node for IDs when assigning IDs each time. For example:
77
77
78
78
```sql
79
-
createtablet(id intunique key AUTO_INCREMENT, c int);
79
+
CREATETABLEt(id intUNIQUE KEY AUTO_INCREMENT, c int);
80
80
```
81
81
82
82
Assume two TiDB instances, `A` and `B`, in the cluster. If you execute an `INSERT` statement on the `t` table on `A` and `B` respectively:
83
83
84
84
```sql
85
-
insert into t (c) values (1)
85
+
INSERT INTO t (c) VALUES (1)
86
86
```
87
87
88
88
Instance `A` might cache the auto-increment IDs of `[1,30000]`, and instance `B` might cache the auto-increment IDs of `[30001,60000]`. In `INSERT` statements to be executed, these cached IDs of each instance will be assigned to the `AUTO_INCREMENT` column as the default values.
@@ -97,9 +97,9 @@ Instance `A` might cache the auto-increment IDs of `[1,30000]`, and instance `B`
97
97
98
98
In the example above, perform the following operations in order:
99
99
100
-
1. The client inserts a statement `insert into t values (2, 1)` to instance `B`, which sets `id` to `2`. The statement is successfully executed.
100
+
1. The client inserts a statement `INSERT INTO t VALUES (2, 1)` to instance `B`, which sets `id` to `2`. The statement is successfully executed.
101
101
102
-
2. The client sends a statement `insert into t (c) (1)` to instance `A`. This statement does not specify the value of `id`, so the ID is assigned by `A`. At present, because `A` caches the IDs of `[1, 30000]`, it might assign `2` as the value of the auto-increment ID, and increases the local counter by `1`. At this time, the data whose ID is `2` already exists in the database, so the `Duplicated Error` error is returned.
102
+
2. The client sends a statement `INSERT INTO t (c) (1)` to instance `A`. This statement does not specify the value of `id`, so the ID is assigned by `A`. At present, because `A` caches the IDs of `[1, 30000]`, it might assign `2` as the value of the auto-increment ID, and increases the local counter by `1`. At this time, the data whose ID is `2` already exists in the database, so the `Duplicated Error` error is returned.
103
103
104
104
### Monotonicity
105
105
@@ -108,7 +108,7 @@ TiDB guarantees that `AUTO_INCREMENT` values are monotonic (always increasing) o
108
108
{{< copyable "sql" >}}
109
109
110
110
```sql
111
-
CREATETABLEt (a intprimary key AUTO_INCREMENT, b timestampNOT NULL DEFAULT NOW());
111
+
CREATETABLEt (a intPRIMARY KEY AUTO_INCREMENT, b timestampNOT NULL DEFAULT NOW());
112
112
INSERT INTO t (a) VALUES (NULL), (NULL), (NULL);
113
113
SELECT*FROM t;
114
114
```
@@ -237,14 +237,14 @@ After the value `2030000` is inserted, the next value is `2060001`. This jump in
237
237
In earlier versions of TiDB, the cache size of the auto-increment ID was transparent to users. Starting from v3.0.14, v3.1.2, and v4.0.rc-2, TiDB has introduced the `AUTO_ID_CACHE` table option to allow users to set the cache size for allocating the auto-increment ID.
At this time, if you invalidate the auto-increment cache of this column and redo the implicit insertion, the result is as follows:
257
257
258
258
```sql
259
-
mysql>deletefrom t;
259
+
mysql>DELETEFROM t;
260
260
Query OK, 1 row affected (0.01 sec)
261
261
262
-
mysql>rename table t to t1;
262
+
mysql>RENAME TABLE t to t1;
263
263
Query OK, 0 rows affected (0.01 sec)
264
264
265
-
mysql>insert into t1 values()
265
+
mysql>INSERT INTO t1 values()
266
266
Query OK, 1 row affected (0.00 sec)
267
267
268
-
mysql>select*from t;
268
+
mysql>SELECT*FROM t;
269
269
+-----+
270
270
| a |
271
271
+-----+
@@ -276,7 +276,7 @@ mysql> select * from t;
276
276
277
277
The re-assigned value is `101`. This shows that the size of cache for allocating the auto-increment ID is `100`.
278
278
279
-
In addition, when the length of consecutive IDs in a batch `insert` statement exceeds the length of `AUTO_ID_CACHE`, TiDB increases the cache size accordingly to ensure that the statement can be inserted properly.
279
+
In addition, when the length of consecutive IDs in a batch `INSERT` statement exceeds the length of `AUTO_ID_CACHE`, TiDB increases the cache size accordingly to ensure that the statement can be inserted properly.
0 commit comments