通过您的表结构描述,很明显它没有可以自动生成值的主键字段。MySQLinformation_schema.tables
不持有auto_increment
价值,但null
对于那些未定义的字段auto_increment
。
触发问题:
触发器主体中使用的代码块似乎取决于 id 字段的显式计算和输入。它没有使用auto_increment
字段的默认行为。
根据MySQL 关于 LAST_INSERT_ID 的文档:
LAST_INSERT_ID()返回一个 BIGINT UNSIGNED(64 位)值
,表示
作为最近执行的 INSERT 语句的结果为AUTO_INCREMENT列
成功插入的第一个自动生成的值。
很明显,它仅适用于auto_increment字段。
没有任何字段id_1
和id_2
属性auto_increment
。
由于这个原因,尽管您null
在插入时将这些字段作为输入传递,但不会自动生成任何值并分配给它们。
更改您的表以设置auto_increment
为这些id_x
字段之一,然后开始插入值。一个警告是,在插入期间将值显式传递给auto_increment
字段将导致last_insert_id
返回一个zero
或最近的自动生成值,而不是NEW.id
. 在插入期间传递null
或不选择auto_increment
字段将触发为该字段生成新值,并且last_insert_id
可以选择并返回它。
以下示例演示了上述行为:
mysql> drop table if exists so_q27476005;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create table so_q27476005( i int primary key );
Query OK, 0 rows affected (0.33 sec)
以下语句显示auto_increment
了字段的下一个适用值。
mysql> select auto_increment
-> from information_schema.tables
-> where table_name='so_q27476005';
+----------------+
| auto_increment |
+----------------+
| NULL |
+----------------+
1 row in set (0.00 sec)
让我们尝试null
在字段中插入一个值。
mysql> insert into so_q27476005 values( null );
ERROR 1048 (23000): Column 'i' cannot be null
上面的语句失败,因为输入到了一个not null primary key
字段中,但没有归因于auto_increment
。仅对于auto_increment
字段,您可以传递null
输入。
现在让我们看看 的行为last_insert_id
:
mysql> insert into so_q27476005 values( 1 );
Query OK, 1 row affected (0.04 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
由于输入是明确的,并且该字段未归因于auto_increment
,因此
调用last_insert_id
结果为 a 0
。请注意,如果在同一个数据库连接会话中对另一个表insert
的任何其他字段进行了另一个调用,这也可以是其他值。auto_increment
让我们看看表中的记录。
mysql> select * from so_q27476005;
+---+
| i |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
现在,让我们申请auto_increment
该领域i
。
mysql> alter table so_q27476005 change column i i int auto_increment;
Query OK, 1 row affected (0.66 sec)
Records: 1 Duplicates: 0 Warnings: 0
以下语句显示auto_increment
了该字段的下一个适用值i
。
mysql> select auto_increment
-> from information_schema.tables
-> where table_name='so_q27476005';
+----------------+
| auto_increment |
+----------------+
| 2 |
+----------------+
1 row in set (0.00 sec)
您可以交叉检查last_insert_id
蒸馏器是否相同。
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
让我们null
在字段中插入一个值i
。
mysql> insert into so_q27476005 values( null );
Query OK, 1 row affected (0.03 sec)
null
尽管将 a 传递给了一个字段,但它成功了primary key
,因为该字段属于auto_increment
.
让我们看看生成并插入了哪个值。
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)
该字段的下一个适用auto_increment
值i
是:
mysql> select auto_increment
-> from information_schema.tables
-> where table_name='so_q27476005';
+----------------+
| auto_increment |
+----------------+
| 3 |
+----------------+
1 row in set (0.00 sec)
mysql> select * from so_q27476005;
+---+
| i |
+---+
| 1 |
| 2 |
+---+
2 rows in set (0.00 sec)
现在,让我们观察last_insert_id
为字段提供显式输入时的结果。
mysql> insert into so_q27476005 values( 3 );
Query OK, 1 row affected (0.07 sec)
mysql> select * from so_q27476005;
+---+
| i |
+---+
| 1 |
| 2 |
| 3 |
+---+
3 rows in set (0.00 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)
您可以看到last_insert_id
由于显式输入而没有捕获该值。
但是,信息模式确实注册了下一个适用值。
mysql> select auto_increment
-> from information_schema.tables
-> where table_name='so_q27476005';
+----------------+
| auto_increment |
+----------------+
| 4 |
+----------------+
1 row in set (0.08 sec)
现在,让我们观察last_insert_id
当字段输入是自动/隐式时的结果。
mysql> insert into so_q27476005 values( null );
Query OK, 1 row affected (0.10 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 4 |
+------------------+
1 row in set (0.00 sec)
希望,这些细节对你有所帮助。