This question already has an answer here:
Mysql Create table using Trigger
(1 answer)
Closed 3 days ago.
Is there a way to make a trigger, that creates a new table when a row is inserted in the trigger table, where the name of the new table is what was inserted in a specific column in the table with the trigger?
Something like this?
CREATE TRIGGER playlists.insert_trigger ON playlists AFTER INSERT AS name = SELECT name FROM playlists WHERE id = max(id) CREATE TABLE name (id int, song varchar(255));
Table creation is an operation which causes implicit commit.
Statements That Cause an Implicit Commit
So this operation is not possible in the trigger.
Related
I have a innodb table on mysql like this:
create table person (
id int not null auto_increment primary key,
name varchar(512),
birthdate date,
...
id_most_relevant int,
fulltext(name)
);
I'm want to create a trigger that, whenever a person is updated, the trigger will search other person with most relevant name (by using a full-text search) and put his id on the id_most_relevant field of the updated person, but only if this relevance is more than 95%. So, to get the percentage relevance, I devide the relevance of each person with the relevance of the new name from the person updated. Something like:
SELECT id FROM PERSON
INTO _id_most_relevant
WHERE
MATCH (name) AGAINST (_new_name) /
MATCH (new.name) AGAINST (_new_name) > 0.95
The variables _id_most_relevant and _new_name are previously declare on the trigger and the variable _id_most_relevant would be used to update the person table on id=NEW.id .
Anyone has an idea on how to do that trigger?
I'm using Mysql 5.6 and I can't update it, but I can create an auxiliary table if necessary.
CREATE TRIGGER tr_bu_person
BEFORE UPDATE
ON person
FOR EACH ROW
SET NEW.id_most_relevant = ( SELECT id
FROM person
WHERE id <> NEW.id
ORDER BY MATCH (name) AGAINST (new.name) DESC
LIMIT 1 );
But I'd avoid of FTS usage in the trigger - I'd prefer to set this column to NULL (or remove it at all) and update it to the most relevant row id value in row retrieving query (or, maybe, by event procedure).
I find out that is not possible to solve this problem using a trigger because whenever I update a person's name I will need the fulltext index already updated to get the second MATCH (on the denominator), but the fulltext index it only be updated after the tigger executes (even if it was a after update trigger).
So I have to solve this using other way, without triggers.
That said, I will close this post.
I use mysql 5.7 and the structure of database is as the picture below.
I want the colum "Course_left1" on table Course to drop by 1 whenever a course log is inserted into the table Course_history,but it's not working.
CREATE TRIGGER Course_reduce
AFTER INSERT ON Course_history
FOR EACH ROW
UPDATE Course SET Course.Course_left1 = (Course.Course_left1 - 1) where NEW.Sid = Course.Sid
Sid stands for the id of Student while Course_left1 and Course_left2 stand for the course remain for the specific student.
Now, if I insert one tuple into table Course_history I want the Course_left1 in the table Course to drop by 1 which makes it 31.
i am trying to create a trigger for inserting data.and I want to get the current id for that author or user who is inserting or change in table.but I am getting this error:
unknown column id in new
here is my query
INSERT INTO logs VALUES(null,NEW.id,'inserted',NOW());
I have two table in my database comments table and logs table...
can anybody help me how can I get rid of this error...
It sounds like your comments table has columns like this:
CREATE TABLE comments (
comment_id ...
author ...
message ...
);
But your INSERT statement in the trigger references NEW.id. There is no id column in your comments table. The NEW.* syntax is a way to get access to the row that made the trigger execute. You can only reference columns that exist in that table.
So since I guess your primary key column is comment_id, you should drop the trigger and recreate it to run the INSERT like this:
INSERT INTO logs VALUES(null,NEW.comment_id,'inserted',NOW());
The point is that the NEW.comment_id must reference a column in the comments table, for which you defined the trigger.
I have a table 'project' that has attributes:name, UID(PK), section(distinguisher)... and 3 tables A,B,C based on section with specific properties of each section.i want to be able to enter data into project and the section specific data into A/B/C TABLE based on entry in section attribute of project table of that row. all tables have Foreign key with UID as UID_A, UID_B, UID_C...any ideas on how i can do it? all help appreciated as i am quite a novice...thanks! I am working with Mysql workbench.
Well, create a trigger on the table project:
CREATE TRIGGER `fill_abc` AFTER INSERT ON `project`
FOR EACH ROW BEGIN
INSERT INTO A <some specific data...>;
INSERT INTO B <some specific data...>;
INSERT INTO C <some specific data...>;
END;
Probably, you want to handle not only inserts into this table, but updates and deletes as well - it is nearly the same idea. See trigger definition here and examples here
I have one table (TABLE A) in my mysql database which has the listed fields:
row1_id - PK
row2_id - PK
row3_data
What I would like to do is, every time a record is inserted or updated in table A, I would like to call a trigger to write me the new information of table A in another table, TABLE A_LOG, which has the following fields:
row0-modification-id - PK
row1_id (from table A) - PK
row2_id (from table A) - PK
row3_data (from table A)
the Table A_LOG should then have 3 PKeys, two from the new inserted/updated record of table A and other that indicates me the number of the modification (1, 2, 3..)
Thank you very much for your help.
Create TableA_Log to autoincrement row0-modification-id.
I bet you don't exactly need this table to have row0-modification-id AND row1_id and row2_id all as the PK.
CREATE TRIGGER dbo.trg_TableA_ins_upd
ON dbo.DatabaseName
AFTER INSERT, UPDATE
AS
BEGIN
INSERT INTO TableA_Log (row1_id, row2_id, row3_data)
SELECT row1_id, row2_id, row3_data FROM inserted
END