将单个非重复数据存储到数据库的最佳方法是什么?

时间:2022-12-08 12:01:37

What is best practice for storing data in a database which ever only requires a single entry. An example would be configuration data which relates to the entire application/website. Is it common to create a table for this which has only a single entry?

将数据存储在数据库中的最佳实践是什么,该数据库只需要一次输入。一个例子是与整个应用程序/网站有关的配置数据。为此创建一个只有一个条目的表是常见的吗?

I'm asking under the context of a MongoDB database though I think the question is also valid for SQL databases.

我在MongoDB数据库的上下文中问,虽然我认为这个问题对SQL数据库也有效。

6 个解决方案

#1


5  

An example of an auxiliary table commonly found in databases would be called Constants and may hold such values of pi, the idea begin that all applications using the database are required to use the same scale and precision. In standard SQL, to ensure they is at most one row e.g. (from Joe Celko):

通常在数据库中找到的辅助表的示例将被称为常量,并且可以保持这样的pi值,该想法开始使用数据库的所有应用程序都需要使用相同的比例和精度。在标准SQL中,要确保它们最多只有一行,例如(来自Joe Celko):

CREATE TABLE Constants
(
 lock CHAR(1) DEFAULT 'X' NOT NULL PRIMARY KEY, 
 CHECK (lock = 'X'),
 pi FLOAT DEFAULT 3.142592653 NOT NULL,
 e FLOAT DEFAULT 2.71828182 NOT NULL,
 phi FLOAT DEFAULT 1.6180339887 NOT NULL, 
 ...
);

Because mySQL doesn't support CHECK constraint then a trigger is required to achieve the same.

因为mySQL不支持CHECK约束,所以需要触发器才能实现相同的目的。

#2


3  

A table would be fine, no reason why not to use it just because it will have only one row.

一个表就可以了,没有理由不使用它只是因为它只有一行。

I just had the weirdest idea (I wouldn't implement it but for some reason I thought of that). You can create a hard-coded view like this:

我只是有一个最奇怪的想法(我不会实现它,但出于某种原因,我想到了这一点)。您可以创建这样的硬编码视图:

create view myConfigView 
as
select 'myConfigvalue1' as configValue1, 'myConfigvalue2' as configValue2

and do select * from myConfigView :)

并从myConfigView中选择* :)

but again, no reason why not to use a table just because it will have only one row

但同样,没有理由不使用表只是因为它只有一行

#3


1  

If you are using a SQL DB, you will probably have columns like key name, and value and each attribute will be stored as a row. In MongoDB, you can store all related configuration as a single JSON document

如果您使用的是SQL DB,则可能会有关键名称和值等列,并且每个属性都将存储为一行。在MongoDB中,您可以将所有相关配置存储为单个JSON文档

#4


0  

For MongoDB databases, I usually just make a new "table", but, for SQL databases, that entails a lot more (especially when others are also working on the same database; SQL isn't as malleable), so, you might want to be a bit more careful with it.

对于MongoDB数据库,我通常只创建一个新的“表”,但是,对于SQL数据库,这需要更多(特别是当其他人也在同一个数据库上工作时; SQL不具有可塑性),所以,你可能想要要小心一点。

#5


0  

I use a config table with a name (config_name) and a value (config_value). I even add a help field so that users can see what the name/value pair is intended for, or where it is used.

我使用带有名称(config_name)和值(config_value)的配置表。我甚至添加了一个帮助字段,以便用户可以查看名称/值对的用途或使用位置。

CREATE TABLE  config (
  config_id bigint unsigned NOT NULL auto_increment,
  config_name varchar(128) NOT NULL,
  config_value text NOT NULL,
  config_help text COMMENT 'help',
  PRIMARY KEY  (config_id),
  UNIQUE KEY ix_config_name (config_name),
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Then following php code recovers the value for a key, or returns an empty string. Assumes $db is an open database connection. All entries are forced to lower case.

然后下面的PHP代码恢复键的值,或返回一个空字符串。假设$ db是一个开放的数据库连接。所有条目都被强制为小写。

function getConfigValue($name) {
    $retval='';
    $db = $this->db;
    $sql = 'select config_value from config where LOWER(config_name)="'.strtolower($name).'"';
    $result = $db->Query($sql);
    if ($result) {
        $row = $db->FetchAssoc($result);
        $retval = $row['config_value'];
    }
    return $retval;
}

All mysql/php in this instance, but the general principle remains.

在这个例子中所有的mysql / php,但一般原则仍然存在。

#6


0  

I would just create table for configurations, as rainecc told, and use cache then to take that all table to memory :) and use it from there (cache). It will be the best.

rainecc告诉我,我只是为配置创建表,然后使用缓存将所有表都带到内存:)并从那里使用它(缓存)。这将是最好的。

#1


5  

An example of an auxiliary table commonly found in databases would be called Constants and may hold such values of pi, the idea begin that all applications using the database are required to use the same scale and precision. In standard SQL, to ensure they is at most one row e.g. (from Joe Celko):

通常在数据库中找到的辅助表的示例将被称为常量,并且可以保持这样的pi值,该想法开始使用数据库的所有应用程序都需要使用相同的比例和精度。在标准SQL中,要确保它们最多只有一行,例如(来自Joe Celko):

CREATE TABLE Constants
(
 lock CHAR(1) DEFAULT 'X' NOT NULL PRIMARY KEY, 
 CHECK (lock = 'X'),
 pi FLOAT DEFAULT 3.142592653 NOT NULL,
 e FLOAT DEFAULT 2.71828182 NOT NULL,
 phi FLOAT DEFAULT 1.6180339887 NOT NULL, 
 ...
);

Because mySQL doesn't support CHECK constraint then a trigger is required to achieve the same.

因为mySQL不支持CHECK约束,所以需要触发器才能实现相同的目的。

#2


3  

A table would be fine, no reason why not to use it just because it will have only one row.

一个表就可以了,没有理由不使用它只是因为它只有一行。

I just had the weirdest idea (I wouldn't implement it but for some reason I thought of that). You can create a hard-coded view like this:

我只是有一个最奇怪的想法(我不会实现它,但出于某种原因,我想到了这一点)。您可以创建这样的硬编码视图:

create view myConfigView 
as
select 'myConfigvalue1' as configValue1, 'myConfigvalue2' as configValue2

and do select * from myConfigView :)

并从myConfigView中选择* :)

but again, no reason why not to use a table just because it will have only one row

但同样,没有理由不使用表只是因为它只有一行

#3


1  

If you are using a SQL DB, you will probably have columns like key name, and value and each attribute will be stored as a row. In MongoDB, you can store all related configuration as a single JSON document

如果您使用的是SQL DB,则可能会有关键名称和值等列,并且每个属性都将存储为一行。在MongoDB中,您可以将所有相关配置存储为单个JSON文档

#4


0  

For MongoDB databases, I usually just make a new "table", but, for SQL databases, that entails a lot more (especially when others are also working on the same database; SQL isn't as malleable), so, you might want to be a bit more careful with it.

对于MongoDB数据库,我通常只创建一个新的“表”,但是,对于SQL数据库,这需要更多(特别是当其他人也在同一个数据库上工作时; SQL不具有可塑性),所以,你可能想要要小心一点。

#5


0  

I use a config table with a name (config_name) and a value (config_value). I even add a help field so that users can see what the name/value pair is intended for, or where it is used.

我使用带有名称(config_name)和值(config_value)的配置表。我甚至添加了一个帮助字段,以便用户可以查看名称/值对的用途或使用位置。

CREATE TABLE  config (
  config_id bigint unsigned NOT NULL auto_increment,
  config_name varchar(128) NOT NULL,
  config_value text NOT NULL,
  config_help text COMMENT 'help',
  PRIMARY KEY  (config_id),
  UNIQUE KEY ix_config_name (config_name),
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Then following php code recovers the value for a key, or returns an empty string. Assumes $db is an open database connection. All entries are forced to lower case.

然后下面的PHP代码恢复键的值,或返回一个空字符串。假设$ db是一个开放的数据库连接。所有条目都被强制为小写。

function getConfigValue($name) {
    $retval='';
    $db = $this->db;
    $sql = 'select config_value from config where LOWER(config_name)="'.strtolower($name).'"';
    $result = $db->Query($sql);
    if ($result) {
        $row = $db->FetchAssoc($result);
        $retval = $row['config_value'];
    }
    return $retval;
}

All mysql/php in this instance, but the general principle remains.

在这个例子中所有的mysql / php,但一般原则仍然存在。

#6


0  

I would just create table for configurations, as rainecc told, and use cache then to take that all table to memory :) and use it from there (cache). It will be the best.

rainecc告诉我,我只是为配置创建表,然后使用缓存将所有表都带到内存:)并从那里使用它(缓存)。这将是最好的。