简体   繁体   English

检索MySQL表注释的最优雅方法是什么?

[英]What's the most elegant way to retrieve a MySQL table comment?

I'm trying to programmatically retrieve the comment for a MySQL table. 我正在尝试以编程方式检索MySQL表的注释。 The first method suggested to me was: 向我建议的第一种方法是:

$shown = $db->query('show create table ' . TABLE_NAME)->fetch_row();
preg_match("/COMMENT='(.*)'/", $shown[0], $m);
$comment = $m[1];

But that kind of workaround makes me cringe. 但这种解决方法让我感到畏缩。 I stumbled upon another way: 我偶然发现了另一种方式:

$result = $db->query("select table_comment from information_schema.tables where table_schema = '" .
    DATABASE_NAME . "' and table_name = '" TABLE_NAME '\'')->fetch_row();
$comment = $result[0];

It's a little better (no string parsing), but it still makes me uncomfortable because I'm digging into internal structures where I don't feel like I belong. 它有点好(没有字符串解析),但它仍然让我感到不舒服,因为我正在挖掘内部结构,我觉得我不属于自己。

Is there a nice, simple way to get at the table comment in code? 有没有一个很好的,简单的方法来获取代码中的表注释?

Information schema isn't really an internal structure where you don't belong. 信息模式实际上不是您不属于的内部结构。 It's part of the ANSI SQL standard and its purpose is to give you a legitimate way to query metadata. 它是ANSI SQL标准的一部分,其目的是为您提供查询元数据的合法方式。

I would feel no hesitation to use it. 我会毫不犹豫地使用它。

The one disadvantage is that MySQL's implementation of information schema tends to have pretty poor performance . 一个缺点是MySQL的信息架构实现往往具有相当差的性能 So be careful about running queries against IS in routines that should be quick. 所以要小心在应该快速的例程中对IS运行查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 定期将数据内容复制到MySQL表中,最优雅的方法是什么? - What would be most elegant way to replicate data contents periodically into a MySQL table? 什么是产品,选项和类别最优雅的MySQL架构? - What's the most elegant MySQL schema for products, options and categories? 从包含元素的键的值枚举多维PHP数组上的键的最优雅方法是什么? - What's the most elegant way to enumerate keys on a multidimensional PHP array from the value of a contained element's key? 在PHP中逐字段添加多维数组的最优雅方法是什么? - What's the most elegant way to add up multidimensional arrays field by field in PHP? 使函数返回非二进制状态的最优雅方法是什么? - What's the most elegant way to have functions return non-binary states? 进行递归多对多数据库查找的最优雅方法是什么? - What's the most elegant way to do this recursive many-to-many database lookup? 什么是在MySQL数据库中保存数据的最有效方法 - What's the most efficient way to save data in MySQL Database PHP中“foreach x除了y”最优雅的方法是什么? - What is the most elegant way to do “foreach x except y” in PHP? 在PHP中定义全局常量数组的最“优雅”方法是什么 - What is the most “elegant” way to define a global constant array in PHP 通过保留顺序更改数组键的最优雅方法是什么 - What is the most elegant way to change a key of an array by preserve the order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM