向 MySQL 表添加元数据属性

发布于 2024-08-25 17:49:04 字数 165 浏览 3 评论 0原文

我想向 MySQL 表添加自定义属性,我可以通过 php 读取该表。

这些属性不会干扰表本身 - 它们主要在代码生成期间由 php 代码访问,并且这些属性必须驻留在数据库本身中。

概念上与 .NET 反射类似。

MySQL 支持这样的东西吗?

谢谢。

I would like to add custom attributes to a MySQL table which I can read via php.

These attributes are not to interfere with the table itself - they are primarily accessed by php code during code generation time and these attributes HAVE to reside in the DB itself.

Something similar in concept to .NET reflection.

Does MySQL support anything like this?

Thanks.

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

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

发布评论

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

评论(3

百合的盛世恋 2024-09-01 17:49:04

当您CREATEALTER表格时,您可以添加COMMENTS - 一个用于整个表格,一个用于每一列。要检索这些评论,您可以查询数据库 INFORMATION_SCHEMA,特别是 INFORMATION_SCHEMA.COLUMNS.COLUMNS_COMMENTINFORMATION_SCHEMA.TABLE.TABLE_COMMENTS 列。 INFORMATION_SCHEMA 提供了大量有关数据库的元数据,包括 DESCRIBE 语句提供的数据。任何对某个表或列具有读取权限的用户都可以从 INFORMATION_SCHEMA 读取相应的元数据,但无法读取他们没有读取权限的表的元数据。

将自定义元数据存储在 INFORMATION_SCHEMA 中看起来很自然,但它并不像您可能需要的那样灵活,因为每列只能存储一个 COMMENT。如果这对您的目的造成限制或者您需要定期更新数据,您应该关注@Dark Falcon 并创建一个附加表。

When you CREATE or ALTER a table, you can add COMMENTS - one for the table as a whole and one for each column. To retrieve these comments, you can query the database INFORMATION_SCHEMA, specifically the columns INFORMATION_SCHEMA.COLUMNS.COLUMNS_COMMENT and INFORMATION_SCHEMA.TABLE.TABLE_COMMENTS. INFORMATION_SCHEMA provides a lot of metadata about your databases, including the data provided by DESCRIBE statements. Any user who has read access to a certain table or column can read the respective metadata from INFORMATION_SCHEMA, but cannot read metadata about tables they do not have read access for.

It looks natural to store your custom metadata in INFORMATION_SCHEMA, but it is not as flexible as you might need it because you can store only one COMMENT per column. If this is to restrictive for your purpose or you need to update the data regularly, you should follow @Dark Falcon and create an additional table.

墨小墨 2024-09-01 17:49:04

如何在 PHP 中设置表的 COMMENT 值:

$my_comment = "Test comment.";
$mysqli->query("ALTER TABLE `my_table_name` COMMENT = '$my_comment'");

titanoboa 的回答之后,以下是 如何读回:

$res = $mysqli->query("SELECT `TABLE_COMMENT` FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = 'my_db_name' AND `TABLE_NAME` = 'my_table_name' LIMIT 1");
$row = $res->fetch_assoc();
$my_comment = $row['TABLE_COMMENT']; // "Test comment."

Following on from titanoboa's answer, here's how to set a table's COMMENT value in PHP:

$my_comment = "Test comment.";
$mysqli->query("ALTER TABLE `my_table_name` COMMENT = '$my_comment'");

And here's how to read it back:

$res = $mysqli->query("SELECT `TABLE_COMMENT` FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = 'my_db_name' AND `TABLE_NAME` = 'my_table_name' LIMIT 1");
$row = $res->fetch_assoc();
$my_comment = $row['TABLE_COMMENT']; // "Test comment."
全部不再 2024-09-01 17:49:04

什么样的属性?既然你提到了反射,我假设你正在尝试找出有关表结构的信息?您是否意识到告诉您表结构的命令是 SQL,并像其他查询一样返回其结果?这使您能够以编程方式处理 DESCRIBE TABLE 等结果。这是您要找的吗?

What kind of attributes? Since you mention reflection, I assume you're trying to find something out about the table structure? Did you realize that the commands that tell you about the table structure are SQL and return their results as any other query? This lets you progammatically process the results of, for example, DESCRIBE TABLE. Is this what you're looking for?

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