数据库关系1:1 vs 1:0..1 [英] Database relationship 1:1 vs 1:0..1

查看:431
本文介绍了数据库关系1:1 vs 1:0..1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习这些关系。如下所示,是否正确:

I'm trying to learn these relationships. Is it correct as I show below that:

1)我将有一个 1:0..1 通过简单地使父母的身份密钥也成为孩子的身份密钥来建立关系?

1) I will have a 1:0..1 relation by simply making the identity key of the parent also the identity key of the child?

2)为使其 1:1 ,我在父表中添加了一个FK,将两个Identity列?

2) To make it 1:1, I add an FK in parent table, connecting the two Identity columns?

这就是全部吗?

1:0..1

客户(Id {PK,身份},姓名)

Customer(Id {PK, Identity}, Name)

CustomerAddress (CustomerId {PK,FK,Identity},StreetName)

CustomerAddress(CustomerId {PK, FK, Identity}, StreetName)

FK_CustomerAddress_Customer (CustomerId ,id)

FK_CustomerAddress_Customer(CustomerId, Id)

1:1

客户(Id {PK,FK,身份},名称)

Customer(Id {PK, FK, Identity}, Name)

FK_Customer_CustomerAddress (Id,CustomerId)

FK_Customer_CustomerAddress(Id, CustomerId)

CustomerAddress (CustomerId {PK,FK,Identity},Streetname)

CustomerAddress(CustomerId {PK, FK, Identity}, Streetname)

FK_CustomerAddress_Costumer (CustomerId,Id)

FK_CustomerAddress_Costumer(CustomerId, Id)

EDIT1 :

我认为以上两个都是错误的,解决方案是这样的:(?)

I believe both above was wrong, and solution is like this: (?)

1:0..1

客户(Id {PK,身份} ,名称)

CustomerAddress (CustomerId {PK,FK,UNIQUE} ,StreetName)

CustomerAddress(CustomerId {PK, FK, UNIQUE}, StreetName)

FK_CustomerAddress_Customer (CustomerId,Id)

FK_CustomerAddress_Customer(CustomerId, Id)

推荐答案

两个表之间的1:1关系意味着每个表中的元组每个表都映射到另一个中的一个元组。这在形式上更称为Bijection。 SQL的局限性在于,在大多数实际情况下,实际上不可能在SQL数据库中实现双射。这是因为SQL要求每个表都要单独更新,因此除非临时禁用其中一个约束,否则无法更新两个这样的表。

A 1:1 relationship between two tables means that each tuple in each table maps to exactly one tuple in the other. This is more formally known as a Bijection. It's a limitation of SQL that for most practical purposes bijection is effectively impossible to achieve in a SQL database. That's because SQL requires each table to be updated individually and therefore there is no way to update two such tables unless you temporarily disable one of the constraints.

1:0/1关系也称为Surjection(您的第一个例子)。假设是不允许使用null的SQL样式 FOREIGN KEY约束的标准行为。约束在关系的一侧始终是可选的。

A 1:0/1 relationship is also known as an Surjection (your first example). Surjection is the standard behaviour of a SQL-style "FOREIGN KEY" constraint assuming nulls are not permitted. The constraint is always optional on one side of the relationship.

注意:IDENTITY属性与您的示例无关。定义关系的是键和外键约束。

NOTE: The IDENTITY property isn't relevant to your example. It's the key and foreign key constraints that define the relationship.

这篇关于数据库关系1:1 vs 1:0..1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆