I need to create a Mysql Trigger to create a table with the name containing the id of the last row inserted in another table

Asked

Viewed 78 times

0

I would like every time a row was inserted into the chats table,

a new table was created. The Table must have the name of the value inserted in the chat_id column of the chats table.

Ex: If the last id inserted is 34, the table should be called chat_34

I tried to create Trigger for after there is an Insert in the chat_id table

CREATE TRIGGER `create_chats_table` AFTER INSERT ON `chats` FOR EACH ROW 

SELECT chat_id INTO @chat_id_ FROM chats ORDER BY chat_id DESC LIMIT 1;
SELECT CONCAT('chat_', @chat_id_) INTO @table_name_;

CREATE TABLE IF NOT EXISTS @table_name_  (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `message` TEXT,
    `author` varchar(20) NOT NULL,
    `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT  @table_name_ FOREIGN KEY (id) REFERENCES chats (chat_id) ON DELETE CASCADE,
    PRIMARY KEY (`id`)
  ) ENGINE  = InnoDB;

The problem is when I try to create, the following message appears in the terminal:

O nome da entidade era esperado. (near "@table_name_" at position 27)

It seems that Mysql is not recognizing the variable I am using on this line

CREATE TABLE IF NOT EXISTS @table_name_ (

How can I make my Rigger work? What I’m doing wrong?

  • Need to create a table for each id, could not be a single table with an identifier , I know little Mysql and in a quick search I saw nothing about DDL commands in Trigger , in Oracle is possible but a little complicated.

  • According to the documentation specification yes. Each new chat will be held in a new table, and this table will have to be created every time there is an Insert in the chats table. Ai I’m trying to do this on Rigger, for every time there is an Insert in the chats table, the triggers automatically already create the table.

  • To add, I arrived at it from here link ;link That’s why I thought it works. Because what they did in this stack overflow response is very similar to what I tried to do (at least the variable declaration part)

  • the key and make a ddl via Trigger or via Procedure , not being possible via Trigger , create a table that will have the table to be created the Procedure , reads the table and creates the Trigger and erases the record , the Procedure runs from time to time via Event , if Rigger cannot do the DDL, not questioning the (evil)creation of the patched tables ...

  • Only as an Oracle note would it be possible more would need a permission for Trigger and the command would need to run via a "execute immediate", ie "on the outside".

  • The point is, I can’t change bank technology at this point in the championship, so I’m trying to do it that way, and I’m trying to figure out why what I tried was wrong. It’s been 3 days that I’m testing, and I’ve arrived in this form, which is looking promising, since there are examples right here in the stackoverflow of similar things working.

  • saw this : https://stackoverflow.com/questions/5981396/can-mysql-triggers-be-created-with-dynamic-sql-from-within-a-stored-procedure does not Trigger create the table but a Procedure called by EVENT the Trigger only writes to an auxiliary table

  • I mean, if I want to do this, won’t I get on the path I’m trying to? That’s what you mean?

  • can you write better please? don’t get it, are missing a few letters :)

  • For the little I saw Trigger does not allow dynamic sql to generate a DDL (create table for example) command, so : Trigger writes to an auxiliary table the table name to be created , a precedent does : reads this table and by means of dynamic sql makes the DDL command of table creation and erases the record , a EVENT programmed psra , say 5 in 5 minutes calls this precedent , This if it’s not possible to be by Rigger and if it really needs to be create a table by id, I pity the guy who takes care of the comic. []s

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.