如何在 postgres 表中表示以下信息?
我需要存储的信息具有以下格式
category, command, options, description
选项,具体取决于命令,并且可以有许多不同的值。 例如
'SetBack', 'set_fan', [ (0,ON), (1, OFF), (2, AUTO) ], 'Sets the fan value'
'SetTime', 'set_time', [0-99 Minutes], 'Sets the time value'
'SetHour', 'set_hour', [0-12 Hours], 'Sets the hour value'
'SetFanOptions', 'set_fan_opt', [ (0,'Constant','constant value'), (1, 'Air Mixture', 'Sets Fan Air Mixture'), (2, OFF, 'sets off fan') ... ], 'Sets the Fan Ait Mixture value'
“选项”字段有多种类型的值。
在 postgres 中表示这些信息的最佳方式是什么?另外,我应该使用一张表还是多个表?
The information I need to store has the following format
category, command, options, description
options depend on commands and can have many different values.
e.g
'SetBack', 'set_fan', [ (0,ON), (1, OFF), (2, AUTO) ], 'Sets the fan value'
'SetTime', 'set_time', [0-99 Minutes], 'Sets the time value'
'SetHour', 'set_hour', [0-12 Hours], 'Sets the hour value'
'SetFanOptions', 'set_fan_opt', [ (0,'Constant','constant value'), (1, 'Air Mixture', 'Sets Fan Air Mixture'), (2, OFF, 'sets off fan') ... ], 'Sets the Fan Ait Mixture value'
'options' field has multiple types of values.
What would be the best way to represent this information in postgres? Also, Should I use one table or multiple tables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
类别、命令和描述对于某种“命令”表来说是非常简单的
varchar
列:并且选项应该有自己的表:
因此,您的
set_fan
选项在command_options
中看起来像这样:set_time
有点像这样:set_fan_opt
:我不确定“类别”之间有什么区别和“命令”是数据库需要了解多少选项,但希望以上内容可以帮助您入门。
The category, command, and description are pretty straight forward
varchar
columns for some sort of "command" table:And the options should have their own table:
So, your
set_fan
options would look like this incommand_options
:And
set_time
sort of like this:And
set_fan_opt
:I'm not sure what the difference between "category" and "command" is or how much the database needs to know about the options but hopefully the above will get you started.
当我设计数据库模式时,我会绘制出我认为需要的不同表,然后查看它们的关系。例如,您想查看表 A 与表 B 是否具有 1:1 或 1:many 映射。就开始吧。尝试直观地绘制出来。这看起来是一个相当简单的数据库,所以不会花很长时间。
然后绘制出您计划使用的列。确保您可以唯一标识一条记录。
When I am designing a database schema, I map out the different tables that I think I will need then look at their relation. For example, you want to look at if Table A will have a 1:1 or 1:many mapping with table B. Just to get started. Try to visually map it out. This looks like a fairly simple db so it wont take long.
Then map out the columns that you plan on using. Make sure you can uniquely identify a record.
我不确定我是否理解这个问题,因为我不认为您的“代码”与 SQL 远程相关,但无论如何。
对于
SetBack
、SetTime
和SetHour
,我将使用带有适当检查约束的整数列,以确保只能存储有效数字。如果 SetFanOptions 是您列出的单个值,我也会使用整数列(再次使用适当的检查约束)
如果每个风扇可以有多个 fanOptions(?),您将需要与另一个表的一对多关系。
I am not sure I understand the question because I don't recognize your "code" as anything remotely to do with SQL, but anyhow.
For
SetBack
,SetTime
andSetHour
I would use an integer column with the approriate check constraints to make sure only valid numbers can be stored.If
SetFanOptions
is a single value out of the ones you listed, I'd use an integer column as well (again with approriate check constraints)If you can have multiple fanOptions per fan(?) you will need a one-to-many relation to another table.