简体   繁体   English

SQL当事人关系模型-子类和超类之间的一致性

[英]SQL Party-Relationship Model - Consistency Between Sub and Super Class

In trying to solve a problem I was having with having different types of the same table, I decided to go with the Party Model design for relational databases - using multiple tables to represent the sub-objects. 为了解决同一个表具有不同类型的问题,我决定采用关系数据库的Party Model设计-使用多个表来表示子对象。

Using this post as well as this one as guides, I decided to construct a Party model using multiple table inheritance. 使用这个职位以及这个作为指导,我决定建立基于多表继承缔约方模型。

Here is a basic representation of what I'm doing (in pseudo sql): 这是我在做什么的基本表示(在伪sql中):

Parties
- ID, AUTO_INCREMENT
- Type
- Shared_Data
- Primary Key (ID, Type)

User_Sub
- ID
- Type, Default 'U'
- User_Data
- Primary Key (ID, Type)
- Foreign Key 'User-Party' (ID, Type) references (Parties.ID, Parties.Type)

Organization_Sub
- ID
- Type, Default 'O'
- Organization_Data
- Primary Key (ID, Type)
- Foreign Key 'Organization-Party' (ID, Type) references (Parties.ID, Parties.Type)

Because data is spread across multiple tables, I'd like to be able to read from and update to the parent and child tables at once. 由于数据分散在多个表中,因此我希望能够一次读取和更新父表和子表。 Reading is fine, and can be done with a: 阅读很好,可以使用以下方法完成:

CREATE VIEW Users as
SELECT P.*, U.User_Data FROM Parties P 
Inner Join Users_Sub U on P.id=U.id, P.type=U.type

But, inserting into both is not possible in MySQL (is it possible in other flavors?). 但是,在MySQL中无法同时插入两者(在其他版本中也可以吗?)。 Is it not possible to do multiple-table inheritance in such a manner? 这样不能进行多表继承吗? Putting all my User and Organization Columns into the Parties Table would probably solve this issue, but I'd be left with quite a few null fields. 将所有“用户”和“组织”列放入“缔约方表”中可能会解决此问题,但我会留下很多空字段。

Insight on this would be much appreciated. 对此有见识将不胜感激。

MySQL supports multi-table updates and deletes, but not inserts. MySQL支持多表更新和删除,但不支持插入。 Nor does MySQL support INSTEAD OF triggers like SQL Server does (which would provide one workaround for the lack of multi-table inserts). MySQL也不像SQL Server一样支持INSTEAD OF触发器(这将为缺乏多表插入提供一种解决方法)。

To deal with this, one option would be to create a stored procedure for your single-row inserts, which starts a transaction, inserts into Parties, grabs the new ID value (using LAST_INSERT_ID() ), then inserts into the child table. 为了解决这个问题,一种选择是为单行插入创建一个存储过程,该存储过程开始一个事务,插入到多方中,获取新的ID值(使用LAST_INSERT_ID() ),然后插入到子表中。

Your decision may ultimately depend on whether you need to perform multi-row inserts on a regular basis. 您的决定最终可能取决于您是否需要定期执行多行插入。 If so, if you had all the unique child table columns also present (and nullable) on the parent (Parties) table, you could use an AFTER INSERT trigger to insert the non-Parties-specific column values into a new row in the appropriate child table. 如果是这样,如果在父表(Parties)上还存在所有唯一的子表列(并且可以为空),则可以使用AFTER INSERT触发器将非特定于Parties的列值插入相应的新行中子表。

Another less elegant (and less efficient) scenario would be (in a stored procedure) to lock the Parties table, save the highest auto-increment ID to a variable, perform the multi-row Parties insert, save the last-inserted ID (now the highest ID), then insert rows into the appropriate child table using an incrementing variable for ID (to match the range of the IDs you just created in Parties). 另一个较不优雅(效率不高)的方案是(在存储过程中)锁定“方”表,将最高自动增量ID保存到变量,执行多行方插入,保存最后插入的ID(现在最高ID),然后使用ID的递增变量将行插入相应的子表中(以匹配您刚在Party中创建的ID的范围)。

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

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