English 中文(简体)
相关数据库中的“OR”关系
原标题:Relation "OR" in relational databases

The question is pretty trivial I guess. But nevertheless, E.g., I have Entities: user (id, name), group (id, name), user_group (user_id, group_id) and gallery (id, name, owner_id). Owner of the gallery could be user OR group.

如何在关系数据库中找到最佳解决办法?

感谢!

PS 任何人都知道如何优化使用 al和chem。 它如何看待?

我想到的是所有权人(id,user_id, group_id),但我对如何显示关系方面的“OR”关系没有任何想法。

问题回答

The simplest solution would be a relation Owner(id, user_id, group_id) where either user_id or group_id can be set -- guard that with an appropriate constraint.

缩略语

  • A group could have not only users but other groups (recursion and/or infinit cylces ahead).
  • If User and Group are used in some more places more adaptions might be necessary.
  • Data consistence cannot be enforced by the database any more.

Combine Owner and group into one table. 所有人和群体只能因本表的属性而有所不同,或者在结合本新表格(“成员”)时有/没有行踪。

用户与群体之间不存在技术差异(只有概念一)。

将这两条列入同一表格(user),并在第二个领域标明一个浏览类型(组群或用户)。

使用应用逻辑,确保只有group的“有子女”才能列入user_group表。

引进一个新实体,使其成为小组的所有人。 然后,将电离层和组群的类别(即“继承”的类别)放出。

Your ER model would look like this (only PK fields shown):

enter image description here

从理论上讲,在物理数据库中采用一种类别有三种主要方法。 所有这一切都有利弊,但就你的模式而言,solution 1可能是最适当的:

  1. Use separate tables for OWNER, USER and GROUP and connect them via FOREIGN KEYs.
    • In this scenario, you may or may not use the discriminator (i.e. type identifier) in OWNER.
  2. Put USER and GROUP in the separate tables, with OWNER s fields present in both.
  3. Put OWNER, USER and GROUP in the same table.




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签