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

发布于 2024-09-16 00:28:38 字数 560 浏览 0 评论 0原文

我正在尝试以编程方式检索 MySQL 表的注释。向我建议的第一个方法是:

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

但这种解决方法让我感到畏缩。我偶然发现了另一种方式:

$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];

它好一点(没有字符串解析),但它仍然让我感到不舒服,因为我正在挖掘我不属于自己的内部结构。

有没有一种很好、简单的方法来获取代码中的表注释?

I'm trying to programmatically retrieve the comment for a MySQL table. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念三年u 2024-09-23 00:28:38

信息模式实际上并不是一个你不属于的内部结构。它是 ANSI SQL 标准的一部分,其目的是为您提供查询元数据的合法方法。

我会毫不犹豫地使用它。

一个缺点是 MySQL 的信息模式实现往往性能很差 。因此,在应该快速的例程中运行针对 IS 的查询时要小心。

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.

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. So be careful about running queries against IS in routines that should be quick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文