我刚刚偶然发现了一个非常奇怪的行为:
想象一下我们有一张客户表:
MariaDB [connections]> describe customers;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| customerId | int(11) | NO | PRI | NULL | auto_increment |
| customerName | varchar(50) | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
插入几个值:
insert into customers(customerName) values('Foo');
insert into customers(customerName) values('Bar');
然后删除所有内容并重置自动增量:
DELETE FROM customers;
ALTER TABLE customers AUTO_INCREMENT = 0;
现在,插入一个 customerId=0 的新值:
INSERT INTO customers(customerId,customerName) VALUES(0,'Site owner');
并查看结果:
MariaDB [connections]> select * from customers;
+------------+--------------+
| customerId | customerName |
+------------+--------------+
| 1 | Site owner |
+------------+--------------+
1 row in set (0.00 sec)
客户 ID 设置为 1!!!!
重复相同的过程,但重置为 5 并插入 5,一切正常:
MariaDB [connections]> delete from customers;
Query OK, 1 row affected (0.00 sec)
MariaDB [connections]> ALTER TABLE customers AUTO_INCREMENT = 5;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [connections]> INSERT INTO customers(customerId,customerName) VALUES(5,'Site owner');
Query OK, 1 row affected (0.00 sec)
MariaDB [connections]> select * from customers;
+------------+--------------+
| customerId | customerName |
+------------+--------------+
| 5 | Site owner |
+------------+--------------+
1 row in set (0.00 sec)
这里发生了什么?如何使用插入值插入值“0”?(是的,我可以在之后进行编辑,但由于各种原因,这对我的情况不实用)。
谢谢!