向 MySQL 表添加元数据属性
我想向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您
CREATE
或ALTER
表格时,您可以添加COMMENTS
- 一个用于整个表格,一个用于每一列。要检索这些评论,您可以查询数据库INFORMATION_SCHEMA
,特别是INFORMATION_SCHEMA.COLUMNS.COLUMNS_COMMENT
和INFORMATION_SCHEMA.TABLE.TABLE_COMMENTS
列。INFORMATION_SCHEMA
提供了大量有关数据库的元数据,包括DESCRIBE
语句提供的数据。任何对某个表或列具有读取权限的用户都可以从INFORMATION_SCHEMA
读取相应的元数据,但无法读取他们没有读取权限的表的元数据。将自定义元数据存储在
INFORMATION_SCHEMA
中看起来很自然,但它并不像您可能需要的那样灵活,因为每列只能存储一个COMMENT
。如果这对您的目的造成限制或者您需要定期更新数据,您应该关注@Dark Falcon 并创建一个附加表。When you
CREATE
orALTER
a table, you can addCOMMENTS
- one for the table as a whole and one for each column. To retrieve these comments, you can query the databaseINFORMATION_SCHEMA
, specifically the columnsINFORMATION_SCHEMA.COLUMNS.COLUMNS_COMMENT
andINFORMATION_SCHEMA.TABLE.TABLE_COMMENTS
.INFORMATION_SCHEMA
provides a lot of metadata about your databases, including the data provided byDESCRIBE
statements. Any user who has read access to a certain table or column can read the respective metadata fromINFORMATION_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 oneCOMMENT
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.如何在 PHP 中设置表的 COMMENT 值:
继 titanoboa 的回答之后,以下是 如何读回:
Following on from titanoboa's answer, here's how to set a table's COMMENT value in PHP:
And here's how to read it back:
什么样的属性?既然你提到了反射,我假设你正在尝试找出有关表结构的信息?您是否意识到告诉您表结构的命令是 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?