如何设计一个包含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个字段,但一次只能使用一个
发布评论