2

I need to copy the auto_increment ID-value into another column upon insert. I think I need an "after insert" trigger, because otherwise the new ID is not known..?

I tried this:

IF NEW.content IS NULL THEN
    SET NEW.content = NEW.id;
END IF

But it complains:

Updating of NEW row is not allowed in after trigger

5
  • what is the name of your table? Commented Sep 23, 2013 at 12:53
  • Table-name is 'notes'
    – kvdmolen
    Commented Sep 23, 2013 at 12:55
  • CREATE TRIGGER insert_example BEFORE INSERT ON example FOR EACH ROW SET NEW.object_id = NEW.id; Try This. Commented Sep 23, 2013 at 13:26
  • What's the point of setting that column to auto increment's value anyway? You can just modify the query that pulls data out to use auto increment's value if it sees that content column is null and your problem is gone.
    – N.B.
    Commented Sep 23, 2013 at 14:04
  • Main reason is to search for the ID when 'content' is empty, also this could be done in SQL indeed. Perhaps this is the better solution..
    – kvdmolen
    Commented Sep 24, 2013 at 14:15

5 Answers 5

3

On this code

  CREATE TRIGGER insert_example
  BEFORE INSERT ON notes
  FOR EACH ROW 
  SET NEW.content = (
        SELECT AUTO_INCREMENT 
        FROM information_schema.TABLES 
        WHERE TABLE_SCHEMA = DATABASE() 
        AND TABLE_NAME = 'notes'
  );

i did something like this

SET NEW.content = (SELECT CONCAT('ID',LPAD(AUTO_INCREMENT, number,'0')) 
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA = DATABASE() 
AND TABLE_NAME = 'notes');

in case you use UNSIGNED_ZEROFILL on the id field... and you may need a custom type of "public" id...

2

As the OP pointed out NEW.id won't work with auto-increment; one could use the following trigger (use at own risk). Try this.

CREATE TRIGGER insert_example 
      BEFORE INSERT ON notes
      FOR EACH ROW 
      SET NEW.content = (
            SELECT AUTO_INCREMENT 
            FROM information_schema.TABLES 
            WHERE TABLE_SCHEMA = DATABASE() 
            AND TABLE_NAME = 'notes'
      );
3
  • Similarly I thought of a BEFORE INSERT trigger with NEW.content = LAST_INSERT_ID() + 1 but I think that's risky too..
    – kvdmolen
    Commented Sep 24, 2013 at 14:18
  • It is not guaranteed that the next auto_increment value is LAST_INSERT_ID()+1. See autoinc_lock_mode for INNODB Commented Sep 24, 2013 at 15:07
  • so you get help from this link and know how to implement? Commented Sep 24, 2013 at 15:41
1

If you really need this behavior for some reason you can achieve it by using a separate table for sequencing and a BEFORE INSERT trigger

First you need a simple table for sequencing purposes that might look like

CREATE TABLE table1_seq
(
  id int not null auto_increment primary key
);

Then your factual table schema might look like

CREATE TABLE table1
(
  id int not null default 0 primary key,
  content int
);

Now the trigger

DELIMITER $$
CREATE TRIGGER tg_ai_table1
BEFORE INSERT ON table1
FOR EACH ROW
BEGIN
  INSERT INTO table1_seq() VALUES();
  SET NEW.id = LAST_INSERT_ID(), NEW.content = COALESCE(NEW.content, NEW.id);
END$$
DELIMITER ;

Now you if you insert into table1

INSERT INTO table1 () VALUES (NULL),(-1);

You'll have

| ID | CONTENT |
|----|---------|
|  1 |       1 |
|  2 |      -1 |

Here is SQLFiddle demo

2
  • @kvdmolen Did it help? Do you need more help with your question?
    – peterm
    Commented Sep 24, 2013 at 12:38
  • Answer is: not possible using triggers. I prefer the easier workarounds (see comment in initial question) or I'll have to build it in application layer. Thanks for your help!
    – kvdmolen
    Commented Sep 24, 2013 at 14:37
0

Yes, this is impossible with triggers. The best you could do is that you replace the INSERT statement with a CALL some_procedure(values...), and implement the above logic in that. (assuming you can change SQL on the application side).

Another (but uglier) solution to setup an EVENT, that periodically fixes your records, where content=NULL

0

Bit of a bodge, by can you just say

DELIMITER $$
CREATE TRIGGER sometrigger
AFTER INSERT ON sometable
BEGIN
  UPDATE sometable SET content = NEW.id WHERE id = NEW.id;
END $$

update the (same) table via a full query, rather than trying to use SET.

or even

DELIMITER $$
CREATE TRIGGER sometrigger
AFTER INSERT ON sometable
BEGIN
  UPDATE sometable SET content = id WHERE content is null;
END $$

(which is sort of similar to Allita's suggestion of an event)

3
  • It won't work in MySQL. You can't execute any DML statement within a trigger on a table on which you have that trigger defined.
    – peterm
    Commented Sep 24, 2013 at 2:05
  • Latter gives another error: "Can't update table 'notes' in stored function/trigger because it is already used by statement which invoked this stored function/trigger."
    – kvdmolen
    Commented Sep 24, 2013 at 14:41
  • Ah sorry. I admit didnt try it. I've used triggers to insert (another) new row, but hadnt tried update just assumed it would work. I can't downvote my own answer! Commented Sep 24, 2013 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.