表有2个字段,但一次只能使用一个 [英] Table with 2 fields but only one can be used at a time

查看:215
本文介绍了表有2个字段,但一次只能使用一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设计一个包含2个字段的表的数据库,其中只有一个字段可以一次设置,没有太多的冗余?例如文件系统:

How would you design a database that has a table with 2 fields and just one of them can be set at a time, without too much redundancy? For example a file system:

假设我们有一个带有驱动器的表,一个带有文件夹,一个带有文件。

Let's say that we have a table with drives, one with folders and one with files.

驱动器和文件非常简单。但是,文件夹有一个父文件夹,可以是一个文件夹(在这种情况下,引用到同一个表)或一个磁盘(在这种情况下,引用到一个磁盘行)。

Drives and files are quite trivial. However, folders have a parent which can be either a folder (in which case the reference is to the same table) or a disk (in which case the reference is to a disk row).

您会添加一些额外的表吗?

Would you add some extra tables?

推荐答案

一个表,id,name,parentid,想要)类型。文件夹和文件都使用parentid指向其父级。磁盘没有父项。只有NULL的字段是类型为Disk的(相对较少)记录的parentid字段。

One table with id, name, parentid, and (if you want) type. Folders and files all point to their parent with parentid. Disks do not have a parentid. The only field that is NULL is the parentid field of the (relatively few) records that are of type Disk.

这篇关于表有2个字段,但一次只能使用一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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