灵活的外键 [英] A flexible foreign key

查看:135
本文介绍了灵活的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个sql server数据库。我正在开发一个vb.net应用程序。



现在我想要有一个事件表,应该保持与我的数据库相关的所有事件。但是,这个表格的一个字段应该是与该事件必须做的记录相关的字段 ObjectID 。但是这个记录可能在不同的表格上。例如:




  • 事件1 ----表客户端记录25

  • 事件2 - ---记录30表发票

  • 事件3 ----记录40表文章
    ...



问题是这个字段 ObjectID 应该是一个灵活的外键,因为可能与不同的表相关。



有什么办法可以解决这个问题吗?



谢谢!

解决方案

您可以创建一个INDEXED VIEW - 定义它,但是您希望将objectIDs +任何其他相关字段(如您所述)从记录25客户表,记录记录30的发票表,等等。



您可以使用存储过程执行相同的操作。



根据您打算如何使用数据 - 如果您只是将其拉入vb.net数据表中,则这两个选项都可以正常运行。



这样的方法就是您不必在数据库中创建或维护任何新表。


I have a sql server database. I'm developing a vb.net application.

Now I'm thinking to have an "Event" table that should keep all the events related with my database. But one of the fields of this table should be a field ObjectID that is related with the record that this event has to do. But this record may be on different tables. For example :

  • Event 1 ---- Record 25 on table Clients
  • Event 2 ---- Record 30 Table Invoices
  • Event 3 ---- Record 40 Table Articles ...

The problem is that this field ObjectID should be a Foreign key in a flexible way , because may be related with different tables.

Is there any way I can resolve this case ?

Thank you !

解决方案

You could create an 'INDEXED VIEW' -- define it however you like to pull the objectIDs + any other relevant fields, as you've described, from record 25 of the clients table, record record 30 of the invoices table, and so on.

You could do the same thing with a stored procedure.

Depending on how you intend to consume the data -- if you're just pulling it into a vb.net datatable, either of these options should work fine.

A benefit to an approach like this is that you don't have to create or maintain any new tables in your database.

这篇关于灵活的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆