将MySQL主键的自动增量设置为无限(或非常大)?

您可以使用BIGINT,但这不是无限的,但是您可以使用它使用大量的主键自动递增。语法如下-

yourColumnName BIGINT NOT NULL AUTO_INCREMENT;

为了理解上述语法,让我们创建一个表。创建表的查询如下-

mysql> create table LargeAutoIncrement
   -> (
   -> Id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY
   -> );

现在,在此表中,您可以存储大量数字,如922337203685477575807,即用于主键自动递增。

让我们使用insert命令在表中插入9223372036854775805中的记录。查询如下。我们将只插入第一个值,其余为空白,因为自动递增将自动插入值-

mysql> insert into LargeAutoIncrement values(9223372036854775805);
mysql> insert into LargeAutoIncrement values();
mysql> insert into LargeAutoIncrement values();

现在,您可以使用select语句显示表中的所有记录。查询如下-

mysql> select *from LargeAutoIncrement;

以下是具有自动增量的输出-

+---------------------+
| Id                  |
+---------------------+
| 9223372036854775805 |
| 9223372036854775806 |
| 9223372036854775807 |
+---------------------+
3 rows in set (0.00 sec)