表有2个字段,但一次只能使用一个

编程入门 行业动态 更新时间:2024-10-26 16:22:13
本文介绍了表有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个字段,但一次只能使用一个

本文发布于:2023-11-03 13:07:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555208.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!