自定义/任意数据库元数据
我认为某些反射托管环境(例如.NET)以属性的形式向代码实体添加自定义元数据的能力非常强大。是否有任何机制可以对数据库执行类似的操作?
数据库显然已经拥有相当数量的可用元数据;例如,您可以获得所有表、列和外键引用的列表,这足以组合一个架构图。然而,我可以想象一些更通用的东西的很多用途,比如 C# 和 DDL 的想象融合:
[Obsolete("Being replaced by the ClientInteraction table")]
create table CustomerOrder (
[SurrogateKey]
MyTableId int identity(1,1) primary key
[NaturalKey]
[ForeignKey("Customer", "CustomerId")] /* Actual constraint left out for speed */
,CustomerId int not null
[NaturalKey]
[ConsiderAsNull(0)]
[ConsiderAsNull(-1)]
,OrderId int not null
[Conditional("DEBUG")]
,InsertDateTime datetime
)
这个例子有点做作,但希望能让我的问题更清楚。我认为反映这种元数据的能力可以使许多任务更容易自动化。那里有这样的东西吗?我正在使用 SQL Server,但如果有其他 DBMS 的东西,那么我仍然有兴趣了解它。
I think that the ability of some reflective managed environments (e.g. .NET) to add custom metadata to code entities in the form of attributes is very powerful. Is there any mechanism for doing something similar for databases?
Databases obviously already have a fair amount of metadata available; for example, you can get a list of all tables, columns, and foreign key references, which is enough to put together a schema diagram. However, I could imagine plenty of uses for something more generic, such as something along the lines of this imaginary fusion of C# and DDL:
[Obsolete("Being replaced by the ClientInteraction table")]
create table CustomerOrder (
[SurrogateKey]
MyTableId int identity(1,1) primary key
[NaturalKey]
[ForeignKey("Customer", "CustomerId")] /* Actual constraint left out for speed */
,CustomerId int not null
[NaturalKey]
[ConsiderAsNull(0)]
[ConsiderAsNull(-1)]
,OrderId int not null
[Conditional("DEBUG")]
,InsertDateTime datetime
)
The example is a little contrived but hopefully makes my question clearer. I think that the ability to reflect over this kind of metadata could make many tasks much more easily automated. Is there anything like this out there? I'm working with SQL Server but if there's something for another DBMS then I'd still be interested in hearing about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 SQL Server 2005 及更高版本中,您可以使用
sp_addextendedproperty
和fn_listextendedproperty
(以及 SSMS gui)来设置和查看各种数据库项目的描述。以下是如何设置和查看表列描述的示例:in SQL Server 2005 and up you can use
sp_addextendedproperty
andfn_listextendedproperty
(as well as the SSMS gui) to set and view descriptions on various database items. here is an example of how to set and view a description on a table column:如果您知道在哪里查找,您几乎可以从 SQL Server 中提取与对象相关的任何内容。如果你需要提供更多的“属性”,那么你就用更多的“概念”来扩展问题域,这违背了 KISS 原则——而且关系数据库显然能够完美地表示与你的数据和你的示例有关的任何关系。已经表明。我想不出您想要将用户提供的元数据添加到表中的原因。
但是,如果您有需要,请坚持在 ORM 中添加其他属性。或者创建一个单独的表来描述您的特定问题的元数据。您还可以链接到可以用 C# 自由编写的表定义函数 - 因此您可以用 C# 表示您的属性 - 再次:我认为这有点矫枉过正。
You can pull out almost anything to do with an object out of SQL Server, if you know where to look. If you need to supply more "attributes" you are extending the problem domain with more "concepts" and this goes against the KISS principle -- also a relational database is obviously perfect capable of representing any relations to do with your data and the example you have shown. I cannot think of a reason why you would want to add user supplied meta-data to a table.
If however, you have the need, stick to adding the additional attributes in an ORM. Or create a separate table that describes the meta-data for your specific problem. You also have the ability to link in Table Defined Functions which you are free to write in C# -- you can therefore represent your attributes in C# -- again: I think this to be overkill.