Solved

mysql trigger refer to the "table name"

Posted on 2014-01-08
2
1,266 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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