简体   繁体   English

SQL Server中的数据库模型-引用完整性

[英]Database Model In SQL Server - Referential Integrity

We have a table 'XYZ' in which we are inserting data for different tables - 我们有一个表“ XYZ”,其中要插入不同表的数据-

ID  TableName   RowID
1   Person       1
2   Company      1
3   Employee     5

If we want to maintain the referential integrity for column RowID, I think of only two options: 如果我们想维护列RowID的引用完整性,我认为只有两个选择:

  1. Base Table: In which we create the unique ID for every table row through column BaseID in all tables,and use Base Table Column BaseID in XYZ rather than RowID through that we maintain the referential integrity. 基本表:在该表中,我们通过所有表中的BaseID列为每个表行创建唯一ID,并在XYZ中使用Base Table Column BaseID而不是RowID来保持引用完整性。

  2. Allowing the same column to store FKs to multiple tables, This also not a very efficient way. 允许同一列将FK存储到多个表中,这也不是一种非常有效的方法。

So please advise if you know the efficient way to maintain integrity. 因此,请告知您是否知道保持诚信的有效方法。

Thanks for Help. 感谢帮助。

You have following method for this purpose: 为此,您有以下方法:

1- Have one reference table for each table, and use view for have XYZ: 1-每个表都有一个参考表,并使用视图获取XYZ:

PersonRows Table
ID RowId

CompanyRows Table
ID RowId

EmployeeRows Table
ID RowId


Create View XYZ AS    
  Select 'Person' AS TableName, RowID From PersonRows
  UNION ALL
  Select 'Company' AS TableName, RowID From CompmayRows
  UNION ALL
  Select 'Employee' AS TableName, RowID From EmployeeRows

2- Use after trigger (Insert, Update, Delete) for each of Person, Company and Employee table in order to check integrity. 2-对人员,公司和员工表中的每个表使用触发器(插入,更新,删除)之后,以检查完整性。

For Example: 例如:

Create Trigger tr_Person_Del ON Person
After Delete
AS Begin
  Delete From XYZ Where TableName = 'Person' and RowID in (Select Person.Id From Deleted)
End

3- Use following table (In this method I suggest to use sparse column for PersonRowID, CompanyRowID and EmployeeRowId) 3-使用下表(在这种方法中,我建议对PersonRowID,CompanyRowID和EmployeeRowId使用稀疏列)

XYZ(ID, TableName, PersonRowId, CompanyRowId, EmployeeRowId)

This is very much a judgement call. 这很大程度上是一个判断电话。 As you say you have three options: 如您所说,您有三种选择:

Use a single RowID column, however you can't enforce RI. 使用单个RowID列,但是您不能强制执行RI。

You can create a set of nullable columns PersonRowID, CompanyRowID, EmployeeRowID and so on. 您可以创建一组可为空的列PersonRowID,CompanyRowID,EmployeeRowID等。 Each of these has an FK onto the appropriate table and is null if the entity is not a Person/Company/Employee. 它们中的每一个都在适当的表上具有FK,如果实体不是个人/公司/雇员,则为空。

Your other option is to create a base table and another table for each type. 您的另一个选择是为每个类型创建一个基础表和另一个表。 This in my opinion is overkill, for three child tables you end up with a Base table plus one for each entity!. 我认为这太过分了,对于三个子表,您最终得到一个基本表,再给每个实体一个!

My suggestion would be to have: 我的建议是:

ID  TableName   PersonRowID   CompanyRowID   EmployeeRowID
1   Person       1            null           null
2   Company      null         1              null
3   Employee     null         null           5

You would then have FKs from PersonRowID to the Person table, CompanyRowID to the Company table and EmployeeRowID to the Employee table. 然后,您将拥有FK,从PersonRowID到Person表,从CompanyRowID到Company表,从EmployeeRowID到Employee表。

I have to admit though - I question the reason for this table in the first place... perhaps you should think about whether this table is correct before progressing much further? 我必须承认-我首先质疑此表的原因...也许您应该在进一步发展之前考虑一下该表是否正确?

You don't need to mantain a table with table names, SQL Server already do that. 您不需要维护带有表名的表,SQL Server已经做到了。

SELECT * FROM sys.tables

http://msdn.microsoft.com/es-es/library/ms187406.aspx http://msdn.microsoft.com/es-es/library/ms187406.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM