我需要一些可以在一列中保留多个值的设计

I need some design where I can keep more than one value in one column

我正在使用 spring MVC 设计在线银行业务。 客户将拥有一个唯一的 CustomerID,但 he/she 可以拥有多个帐户。与每个 CustomerID 一样,银行中可以有多个帐户。所以我正在设计一个 table 属性:

CustomerID、AccountNumber,其中 CustomerId 将作为主键。

请建议如何设计数据库。

您可以使用逗号 (,)、半色 (;) 等分隔符将多个值放在单列中。但是你可以用另一种方式来做。您可以创建一个 AccountDetails table,其中主键为 AccountNumber,外键为 CustomerId。

这是一个很好的示例,您可以在其中创建两个相互关联的表 - 一个用于客户,一个用于客户。一个客户可以有多个账户,所以是一对多的关系。考虑下表:

Customers:
CustomerId(primary key), (other columns...)

Accounts:
AccountId, CustomerId(foreign key), AccountNumber

通过加入这些表,您可以在一个视图中显示客户及其帐户:

SELECT CustomerId, AccountNumber FROM Customers, Accounts WHERE Customers.CustomerId=Accounts.CustomerId

将多个值塞入同一个字段违反了atomicity的原则,因此违反了第一范式。相反,您应该使用两个 tables:

客户:

  • CustomerID(主键)
  • 其他客户领域

账户:

  • 帐号(主键)
  • CustomerID(引用客户 table 的外键)
  • 其他帐户字段

所以,一个客户可能在 Customer table 中有一行,而在 Account table 中可能有不止一行。