一个或两个外键 [英] One or Both Foregin Keys

查看:89
本文介绍了一个或两个外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个可以提供物品位置的设计:


完全合格的地址,包括建筑物名称,房间号码和

摊位数字。

** - 或 - **

GPS位置

** - 或 - **

两个


除触发器之外,有没有办法使用表格和

主键强制执行此操作?


我想象一个地址表,然后


要么是某种中间表,要么是一对多的
关系,GPS到地址,
或地址表中的正确列。


无论哪种方式,我都没有看到一种正常的方式来执行EITHER或BOTH而没有

触发器。


提前致谢。


----------------- ----------(广播结束)---------------------------

提示3:如果通过Usenet发布/阅读,请发送适当的

subscribe-nomail命令给 ma ******* @ postgresql.org ,以便您的

消息可以干净地通过邮件列表

解决方案

Dennis Gearon< ge ***** @ fireserve.net>写道:

我正在设计一个可以拥有某个位置的设计:
具有均匀建筑物名称,房间号和
展位号的完全合格的地址。
** - 或者 - **
GPS位置
** - 或者 - **
两个
除了触发器之外,有没有办法使用表格来强制执行此操作/>主键?




将外键约束放在地址和GPS loc上(这样它们就可以使
无效或为NULL)然后添加一个CHECK约束说

他们不能都是NULL。


问候,tom lane


---------------------------(广播结束)---------------- -----------

提示7:别忘了增加免费空间地图设置


Dennis Gearon< ge ***** @ fireserve.net>写道:

我正在设计一个可以拥有某个位置的设计:
具有均匀建筑物名称,房间号和
展位号的完全合格的地址。
** - 或者 - **
GPS位置
** - 或者 - **
两个
除了触发器之外,有没有办法使用表格来强制执行此操作/>主键?




将外键约束放在地址和GPS loc上(这样它们就可以使
无效或为NULL)然后添加一个CHECK约束说

他们不能都是NULL。


问候,tom lane


---------------------------(广播结束)---------------- -----------

提示7:别忘了增加免费空间地图设置





我的关键结构是这样的:


a cb

+ --- ------- + + --------- + + ---------- +

| PriKey1 | ---> | ForKey |< --- | PriKey2 |

| | |国旗| | |

+ ---------- + + --------- + + ---------- +


....其中c.ForKey是来自PriKey1或PriKey2的值,它们是不同的
值。所有字段都具有相同的数据类型; a.PriKey1和b.PriKey2是

序列。如何在这个结构中引用完整性,那么当Flag为True时c.ForKey引用a.PriKeya或者当Flag为False时引用b.PriKey2

?看起来非常努力通过列表和谷歌最后

晚上没有运气:(


-Ryan Riehle
http://www.buildways.com


关键字:一个外键,多个主键,多个表,

泛化,超类,子类,约束

-------------------- -------(播出结束)---------------------------

提示1:订阅和取消订阅命令转到 ma*******@postgresql.org

I am working on a design where a location for something can have:

Fully qualified address with even building names, room numbers, and
booth numbers.
**-OR-**
GPS location
**-OR-**
Both

Other than triggers, is there a way to enforce this using tables and
primary keys?

I imagine an address table, and then

either a some sort of intermediate table, in a one to many
relationship, GPS to Addresses,
or the correct columns in the address table.

Either way, I don''t see a normal way to enforce EITHER or BOTH without a
trigger.

Thanks in Advance.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

解决方案

Dennis Gearon <ge*****@fireserve.net> writes:

I am working on a design where a location for something can have:
Fully qualified address with even building names, room numbers, and
booth numbers.
**-OR-**
GPS location
**-OR-**
Both Other than triggers, is there a way to enforce this using tables and
primary keys?



Put foreign key constraints on the address and GPS loc (so that they
must be either valid or NULL) and then add a CHECK constraint saying
they can''t both be NULL.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 7: don''t forget to increase your free space map settings


Dennis Gearon <ge*****@fireserve.net> writes:

I am working on a design where a location for something can have:
Fully qualified address with even building names, room numbers, and
booth numbers.
**-OR-**
GPS location
**-OR-**
Both Other than triggers, is there a way to enforce this using tables and
primary keys?



Put foreign key constraints on the address and GPS loc (so that they
must be either valid or NULL) and then add a CHECK constraint saying
they can''t both be NULL.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 7: don''t forget to increase your free space map settings


Hi,

I have a key structure like so:

a c b
+----------+ +---------+ +----------+
| PriKey1 |--->| ForKey |<---| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

....where c.ForKey is a value from PriKey1 OR PriKey2, which are different
values. All fields have the same data type; a.PriKey1 and b.PriKey2 are
sequences. How does one enfore referential integrity in this structure so
that c.ForKey references a.PriKeya when Flag is True or references b.PriKey2
when Flag is False? Looked pretty hard through the lists and on Google last
night with no luck :(

-Ryan Riehle
http://www.buildways.com

KEYWORDS: one foreign key, multiple primary keys, multiple tables,
generalization, superclass, subclass, constraint
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org


这篇关于一个或两个外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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