Embedding comments in MySQL statements

佐手、 提交于 2019-12-06 12:29:18

问题


Does anyone know of a way to embed comments in MySQL statements? When I search for mysql and comments I get only ways to put comments in tables, etc

The idea, if I implement this the way my boss wants it, is to prepend the user id to the statement apparently so that when MySQL is analyzed later (via the binary log) we know who did what.

Example:
SELECT id
FROM customer
WHERE handle='JBH'

Would now show up as:
-- user:jwilkie
SELECT id
FROM customer
WHERE handle='JBH'

(or similar)

EDIT FOR CLARITY: The reason for this is that we have perl modules that are interfacing with MySQL and we are retrieving the user id by reading $ENV{USER} (which in this case is "jwilkie"). It is a situation where we have one MySQL user defined but multiple people running the perl mod.

Does anyone have experience with this? Many many thanks! Jane


回答1:


Normally, comments are stripped before the SQL statement is recorded in the binary log. However, a nasty workaround is to pretend that ypur comment contains syntax for some future version of MySQL - eg. 9.99.99:

/*!99999 user:jwilkie */ insert into tbl values (yyy);

These comments will then be passed through into the binary log.




回答2:


If you have control over the SQL queries being generated, then you should be able to embed comments in them programatically in your query builder.

Select queries don't go in the binary log, but the comments may make it into the slow query log, general query log, etc.

Here's a blog post from Percona that touches on the subject a bit, specifically in the context of mk-query-digest. It might help:

http://www.mysqlperformanceblog.com/2010/07/05/mk-query-digest-query-comments-and-the-query-cache/



来源:https://stackoverflow.com/questions/4100350/embedding-comments-in-mysql-statements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!