简体   繁体   English

如何在MySQL中的表触发器中更改auto_increment字段?

[英]How to change the auto_increment field in a table trigger in MySQL?

There is a necessity when inserting into a table of values to change the auto-increment field to another that no two of the same id in these tables. 插入值表中以将自动递增字段更改为另一个值时,这些表中没有两个相同的ID是必要的。 It is necessary for the data output from the third table based on the recording and going to not add to the table a column indicating. 对于基于记录的从第三表输出的数据,有必要不向表中添加一列指示。 Here's my trigger, but it does not work 这是我的触发器,但是不起作用

CREATE TRIGGER `update_id` AFTER INSERT ON `table1`
FOR EACH ROW BEGIN
ALTER TABLE `table2` AUTO_INCREMENT = NEW.id;
END;

It's not entirely clear what problem you are trying to solve. 尚不清楚您要解决什么问题。

But it sounds as if you have two tables with an id column, and you want to ensure that the same value of id is not used in both tables. 但这听起来好像您有两个带有id列的表,并且您想确保两个表中都没有使用相同id值。 That is, if id value 42 exists in table1 , you want to ensure that 42 is not used as an id value in table2 . 也就是说,如果表table1存在id42table1确保在table2不将42用作id值。

Unforunately, MySQL does not provide any declarative constraint for this. 不幸的是,MySQL没有为此提供任何声明性约束。

It sounds as if you want an Oracle-style SEQUENCE object. 听起来好像您想要一个Oracle风格的SEQUENCE对象。 And unfortunately, MySQL doesn't provide an equivalent. 不幸的是,MySQL没有提供等效功能。

But what we can do is emulate that. 但是我们可以做的就是模仿。 Create an extra "sequence" table that contains an AUTO_INCREMENT column. 创建一个额外的“序列”表,其中包含一个AUTO_INCREMENT列。 The purpose of this table is to be used to generate id values, and to keep track of the highest generated id value: 该表的目的是用于生成id值,并跟踪生成的最高id值:

CREATE TABLE mysequence (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY);

Then, we'd remove the AUTO_INCREMENT attribute from the id columns of the two tables we want to generate distinct id values for. 然后,从要为其生成不同id值的两个表的id列中删除 AUTO_INCREMENT属性。

For those tables, we'd create BEFORE INSERT triggers that will obtain distinct id values and assign it to the id column. 对于这些表,我们将创建BEFORE INSERT触发器,该触发器将获取不同的id值并将其分配给id列。 To generate a unique value, we can insert a row to the new mysequence table, and then retrieve the auto_increment value using the LAST_INSERT_ID function. 要生成唯一值,我们可以在新的mysequence表中插入一行,然后使用LAST_INSERT_ID函数检索auto_increment值。

Something like this: 像这样:

CREATE TRIGGER table1_bi
BEFORE INSERT ON table1
FOR EACH ROW
BEGIN
   DECLARE generated_id INT UNSIGNED;
   -- do we need to generate a value for id column?
   IF NEW.id IS NULL THEN
      -- generate unique id value with insert into sequence table
      INSERT INTO mysequence (id) VALUES (NULL);
      -- retrieve inserted id value 
      SELECT LAST_INSERT_ID() INTO generated_id;
      -- assign the retrieved value to the id columns of the row being inserted
      SET NEW.id = generated_id;
   END IF
END$$

(That's just a rough outline, likely there's at least one syntax error in there somewhere.) (这只是一个粗略的概述,可能某处至少存在一个语法错误。)

You'd need to create a BEFORE INSERT trigger for each of the tables. 您需要为每个表创建一个BEFORE INSERT触发器。

This is one approach to generating distinct values for the id columns. 这是一种为id列生成不同值的方法。

Note that it wouldn't be necessary to keep ALL of the rows in the mysequence table, it's only necessary to keep the row with the largest id value. 请注意,不必将所有行保留在mysequence表中,只需保留具有最大id值的行。

Also note that this doesn't enforce any constraint on either tables; 还要注意,这对两个表都没有施加任何约束。 some session could supply a value for id that is already in the other table. 某些会话可以提供另一个表中已有的id值。 To prevent that, the trigger could raise an error if a non-NULL id value is supplied. 为避免这种情况,如果提供了非NULL id值,则触发器可能会引发错误。 It might also be possible to allow non-NULL values, and to perform a query to check if the supplied id value already exists in the other table, and raise an error if it does. 也可能允许非NULL值,并执行查询以检查提供的id值是否已存在于另一个表中,如果存在则引发错误。 But that query would be subject to a race condition... two concurrent sessions doing inserts to the tables, and you'd need to implement some concurrency killing locking mechanisms to prevent concurrent inserts. 但是该查询将受到竞争条件的影响...两个并发会话在表中进行插入,并且您需要实现一些并发终止锁定机制以防止并发插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM