涉及来自另一个 table 的多行的字段

Field involving multiple rows from another table

我 created/am 创建了 3 个表:SlavesCitizensIncidents.

涉及多名公民奴隶事件我该如何处理? 现在我正在考虑在 Incidents 中创建两个字段,其中包含 CitizenID 和 SlaveID(SlaveID1、SlaveID2...、SlaveIDn)的列表,但这看起来很愚蠢。

其实你的想法听起来一点也不蠢。您可以这样设计 Incidents table:

+------------+-----------+---------+
| IncidentID | CitizenID | SlaveID |
+------------+-----------+---------+
|     1      |     A     |    A    |      <-- incident #1 involved 2 citizens and 1 slave
|     1      |     B     |    A    |
|     2      |     A     |    A    |      <-- incident #2 involved 2 citizens and 2 slaves
|     2      |     B     |    A    |
|     2      |     A     |    B    |
|     2      |     A     |    B    |
+------------+-----------+---------+

现在当你查询某个事件ID时,你可以获得事件中涉及的所有公民和奴隶的列表。这是您的数据库架构中的多对多关系。

您正在寻找多对多关系。

基本上你可以用table这样的方式逃脱:

CREATE TABLE [dbo].[incidents](
    citizen_id*
    ,slave_id*
)

*号表示列是主键的一部分。这确保了公民约翰和奴隶帕特里克之间只有一种关系。

你可以通过制作 2 个桥 table 和一个主 table

Master table
_______________
Incidents

Bridge tables
___________
incident_slave(pk of incidents table , slave information field(s) or pk of slave table)
incident_citizen(pk of incidents table , citizen information field(s) or pk of citizen table)