Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

mysql trigger refer to the "table name"

Posted on 2014-01-08
2
Medium Priority
?
1,482 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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