Solved

mysql trigger refer to the "table name"

Posted on 2014-01-08
2
1,091 Views
Last Modified: 2014-01-08
Hello Experts,

Inside the code of the trigger, is it possible to get the name of the table that the trigger belongs to?

Something like "thistrigger.tablename" lol...

Thx in advanced
0
Comment
Question by:justaphase
2 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 39765975
Hi.

Triggers have a NEW and OLD to access columns of the inserted and deleted rows, respectively; however, it does not have a keyword for its own metadata as you showed.  To get information about trigger, you can query INFORMATION_SCHEMA.TRIGGERS.

For example:
SELECT EVENT_OBJECT_TABLE 
INTO @table_name
FROM INFORMATION_SCHEMA.TRIGGERS
WHERE TRIGGER_NAME = 'trigger name here'
;

Open in new window


Triggers are table-specific, so it is just as easy to hard-code the table name in the trigger body.  However, if the concern is that you may change the table name later, you can use the above query to dynamically find the table name.  Although, it also is vulnerable to trigger name changes.  Therefore, you will have to maintain the trigger body in either case.

I hope that helps.
0
 
LVL 1

Author Closing Comment

by:justaphase
ID: 39766774
Yes it helps!
I would prefer the table name, but since it's impossible, that's a good way to start.
It's more probable to change the table name, that is to change the trigger name.

Thx!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now