我需要存储的信息具有以下格式
category, command, options, description
选项取决于命令,可以有许多不同的值。 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'字段有多种类型的值。
在postgres中表示此信息的最佳方式是什么?另外,我应该使用一个表还是多个表?
答案 0 :(得分:2)
对于某种“命令”表,类别,命令和描述是非常简单的varchar
列:
create table commands (
command varchar not null primary key,
category varchar not null,
description varchar not null -- Or text if the description will be large.
)
选项应该有自己的表格:
create table command_options (
command varchar not null references commands(command),
idx int not null check (idx >= 0),
value varchar not null, -- Not sure if these two column
label varchar null -- names make sense in your context
)
因此,您的set_fan
选项在command_options
中会如下所示:
INSERT INTO command_options
(command, idx, value, label)
VALUES
('set_fan', 0, 'ON', null),
('set_fan', 1, 'OFF', null),
('set_fan', 2, 'AUTO', null);
set_time
有点像这样:
('set_time', 0, '0-99 Minutes', null)
set_fan_opt
:
('set_fan_opt', 0, 'Constant', 'constant value'),
('set_fan_opt', 1, 'Air Mixture', 'Sets Fan Air Mixture'),
('set_fan_opt', 2, 'OFF', 'Sets off fan');
我不确定“类别”和“命令”之间的区别是什么,或者数据库需要了解多少选项,但希望以上内容可以帮助您入门。
答案 1 :(得分:0)
当我设计数据库模式时,我会绘制出我认为需要的不同表格,然后查看它们之间的关系。例如,您想要查看表A是否与表B具有1:1或1:多的映射。只是为了开始。尝试直观地映射出来。这看起来像一个相当简单的数据库,所以它不会花很长时间。
然后映射您计划使用的列。确保您可以唯一标识记录。
答案 2 :(得分:0)
我不确定我是否理解这个问题,因为我不认为你的“代码”与SQL有任何关系,但无论如何。
对于SetBack
,SetTime
和SetHour
,我会使用带有适当检查约束的整数列,以确保只能存储有效数字。
如果SetFanOptions
是您列出的值中的单个值,我也会使用整数列(再次使用适当的检查约束)
如果每个粉丝可以有多个粉丝选项(?),则需要与另一个表的一对多关系。