Solved

mysql trigger refer to the "table name"

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

16 Experts available now in Live!

Get 1:1 Help Now