如何在 postgres 表中表示以下信息?

发布于 2024-11-06 18:48:46 字数 598 浏览 0 评论 0原文

我需要存储的信息具有以下格式

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

探春 2024-11-13 18:48:46

类别、命令和描述对于某种“命令”表来说是非常简单的 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');

我不确定“类别”之间有什么区别和“命令”是数据库需要了解多少选项,但希望以上内容可以帮助您入门。

The category, command, and description are pretty straight forward varchar columns for some sort of "command" table:

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.
)

And the options should have their own table:

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
)

So, your set_fan options would look like this in 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);

And set_time sort of like this:

('set_time', 0, '0-99 Minutes', null)

And 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');

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.

怎樣才叫好 2024-11-13 18:48:46

当我设计数据库模式时,我会绘制出我认为需要的不同表,然后查看它们的关系。例如,您想查看表 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.

栀子花开つ 2024-11-13 18:48:46

我不确定我是否理解这个问题,因为我不认为您的“代码”与 SQL 远程相关,但无论如何。

对于 SetBackSetTimeSetHour,我将使用带有适当检查约束的整数列,以确保只能存储有效数字。

如果 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 and SetHour 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文