数据库设计 - NULL 外键

发布于 2024-08-20 13:41:54 字数 350 浏览 5 评论 0原文

感谢您的阅读。

我正在为一家小狗店制作数据库。我有一张给小狗的桌子和一张给主人的桌子。一只小狗可以有一个主人,主人可以拥有多只小狗,但并不是所有的小狗都被拥有。处理这种情况有什么好方法吗?

  • 如果小狗没有所有者,我是否在小狗表中使用 NULL 的 FK?
  • 我是否创建一个关联表,该表是所有者与小狗的一对多映射,并且在小狗上有一个标志,如果小狗未被拥有,则该标志会被标记?
  • 我要创建两个表吗?一张表可以用于拥有的小狗,并且它对所有者表具有非空 FK,而另一张表则保存不拥有的小狗?

感谢您的帮助。

这个问题的真正目的是,如何将一行标记为全局的,并允许任何用户查看?

and thanks for reading.

I'm making a DB for a puppy shop. I have a table for puppies and a table for owners. A puppy can have one owner, owners can own more than one puppy, but not all puppies are owned. What's a good way to handle this situation?

  • Do I use a FK in the puppy table that is NULL if the puppy doesn't have an owner?
  • Do I create an association table that is a one-to-many mapping of owners to puppies and have a flag on the puppies that gets marked if the puppy is un-owned?
  • Do I create two tables? One table can be for puppies that are owned and it has a NON-NULL FK to the owner table and another table that holds the puppies that are not owned?

Thanks for the help.

This question is really aiming at, how do I mark a row as global, and allowed to be viewed by any user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

晒暮凉 2024-08-27 13:41:55

我将有下表:

Dog
Owner
DogOwner (contains non-nullable DogID and OwnerID FKs that together make up the PK)

然后,您将执行以下操作:

select *
from Dog d
left outer join DogOwner do on d.DogID = do.DogID
left outer join Owner o on do.OwnerID = o.OwnerID

该查询检索所有狗,甚至是那些没有主人的狗。

这对您的设计有一些改进:

  • 将表命名为 Dog,因为狗不会长时间保持小狗状态(嗅一嗅)
  • 使用交集表 DogOwner,因为狗可以拥有更多比一位业主。我知道我的也有!

I would have the following tables:

Dog
Owner
DogOwner (contains non-nullable DogID and OwnerID FKs that together make up the PK)

Then, you would do:

select *
from Dog d
left outer join DogOwner do on d.DogID = do.DogID
left outer join Owner o on do.OwnerID = o.OwnerID

This query retrieves all dogs, even those with no owner.

This has a few improvements over your design:

  • Names the table Dog because dogs don't stay puppies very long (sniff)
  • Uses the intersection table DogOwner, because Dogs can have more than one owner. I know mine does!
柠檬 2024-08-27 13:41:55

如果每只小狗确实只能由一个人拥有,那么当然,如果尚未拥有它,请将 fk 留空/NULL。

否则,我建议 3 个表

  • puppy info
  • 所有者信息
  • puppy-owner

puppy 所有者行将有两列:puppy-id、owner-id。尽管您说一只小狗只能有一个主人,但事实是它很可能由家庭中的所有成年人“拥有”。如果它是一只表演犬,它很可能由饲养员和一个或多个其他人共同拥有。

If each puppy really can be owned only by one and only one person, yes of course leave the fk blank/NULL if it's not yet owned.

Otherwise, I suggest 3 tables

  • puppy info
  • owner info
  • puppy-owner

puppy owner rows will have two columns: puppy-id, owner-id. Even though you're saying that a puppy can have only one owner, the fact is that it is likely to be "owned" by all the adults in the household. If it's a show dog, it's likely to be co-owned by the breeder and one or more others.

云淡风轻 2024-08-27 13:41:55

这是一个有趣的建模问题,因为可以说小狗商店拥有所有不属于任何人的小狗。毕竟,如果李尔·库乔(Li'l Cujo)发脾气并咬伤了几位顾客的脚踝,小狗店老板就要承担所有破伤风疫苗注射的费用。当帕蒂·佩奇为她的爱人购买那只小狗时,交易是所有权的变更,而不是创造它。

此参数的逻辑是 OwnerId 是 NOT NULL 列。

This is an interesting modelling problem, because it could be argued that the puppy store owns all the puppies which are not owned by anybody else. After all, if Li'l Cujo goes on the rampage and nips the ankles of a few customers the puppy store owner would be liable for the cost of all those tetanus jabs. When Patti Page bought that doggy for her sweetheart the transaction was a change of ownership, not the creation of it.

The logic of this argument is that OwnerId is a NOT NULL column.

韶华倾负 2024-08-27 13:41:55

您当前拥有的表(Puppy 和 Owner)应该没问题。在您的 Puppy 表中,您将有一个名为 OwnerID 之类的列,它是 Owner 表的 FK。允许它为 NULL 就可以了。当它为空时,没有人拥有这只小狗。

The tables you currently have (Puppy and Owner) should be fine. In your Puppy table, you will have a column called something like OwnerID which is a FK to the Owner table. It is fine to allow this to be NULL. When it's null, no one owns the puppy.

﹂绝世的画 2024-08-27 13:41:55
Create table owner (ownerid int PRIMARY KEY, ownername varchar(50) not null)

Create table dog(ownerid int, dogid int, dogname varchar(50), CONSTRAINT pk_col PRIMARY KEY (ownerid, dogid), constraint fk_col foreign key (ownerid) references owner(ownerid) );

这是您可以拥有的最佳解决方案。表设计传达的是,您在所有者表中拥有所有者列表,而表狗仅具有所有者存在于所有者表(即父表)中并且他有一只狗的条目。只有那些有主人的小狗才有资格进入狗表。

支持您的要求的查询。

SELECT owner.ownerid, dog.dogid, dog.dogname FROM owner, dog
WHERE owner.ownerid = dog.ownerid
Create table owner (ownerid int PRIMARY KEY, ownername varchar(50) not null)

Create table dog(ownerid int, dogid int, dogname varchar(50), CONSTRAINT pk_col PRIMARY KEY (ownerid, dogid), constraint fk_col foreign key (ownerid) references owner(ownerid) );

This is the best solution you can have. What the table design communicates is you have the list of owners in an owner table and table dog only has those entries where the owner exists in the owner table which is the parent table and he has a dog. That's only those puppies who have an owner have any entry into the dog table.

A query to support your requirements.

SELECT owner.ownerid, dog.dogid, dog.dogname FROM owner, dog
WHERE owner.ownerid = dog.ownerid
李不 2024-08-27 13:41:55

您可以创建一个特殊的所有者“Nobody”,并使所有无主的小狗都引用它,而不是拥有一个空所有者。如果您的数据库无法索引空 FK,并且您开始在寻找无主小狗时遇到性能问题,那么这可能是有意义的。

这让设计变得有点复杂;如果有疑问,请首先尝试空所有者方法。

You may create a special owner "Nobody" and make all unowned puppies refer to it, instead of having a null owner. This may make sense if your database cannot index null FKs and you start to have performance issues looking for unowned puppies.

This complicates design a bit; if in doubt, try the null owner approach first.

茶花眉 2024-08-27 13:41:54

解决方案 1) 是正确的。小狗可以没有所有者或只有一个所有者,因此该列要么填充现有所有者,要么填充 NULL。

Solution 1) is the correct one. A puppy can have either no owner or a single owner, so the column is either populated with an existing owner or NULL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文