如何使用表中不同数量的列设计数据库(How to design database using varying number of columns in table)

编程入门 行业动态 更新时间:2024-10-28 20:27:05
如何使用表中不同数量的列设计数据库(How to design database using varying number of columns in table)

让我们说,你正在创建一个系统来存储不同国家的特征。 会有相同的基本列如名称,人口,首府城市等。但让我们说,除此之外,你想存储一些国家的具体信息,如最高的山峰,最近的海洋,最着名的食物等。这些列将有所不同国家。

如何使用像MySQL这样的关系数据库来完成。 我知道使用像MongoDB这样的无模式NoSQL数据库更容易,每个国家都可以作为单独的文档存储。 但是可以使用关系数据库完成这样的事情吗?

Let us say that you are creating a system to store characteristics of different countries. There will be same basic columns like name, population, capital city etc. But let us say in addition to it you want to store some country specific information like highest mountain, nearest ocean, most famous food etc. These columns will be different for each country.

How can this be done using a relational database like MySQL. I know this is easier using a schema-less NoSQL database like MongoDB where each country can be stored as a separate document. But can something like this be done using relational DBs?

最满意答案

只有文本字段,您需要两个额外的表:

属性(包含属性的名称,例如“最高峰”) country_properties(包含国家/地区对的值:例如:国家“奥地利”的ID,房产“最高山”的ID,“山的名字”)

或者,如果只有几个属性,则只需为未知值存储NULL。

With only text fields you need two additional tables:

properties (contains the name of a property, eg "highest mountain") country_properties (contains values for country-property pairs: eg: id of country "austria", id of property "highest mountain", "name of the mountain")

Alternatively, if there are only a couple of properties, simply store NULL for unknown values.

更多推荐

本文发布于:2023-08-07 20:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465905.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   数量   数据库   design   database

发布评论

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

>www.elefans.com

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