Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to store single non-repeating data to a database?

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.

like image 616
Will Avatar asked Mar 21 '12 08:03

Will


People also ask

Which database is best for real time data?

SQLite. SQLite is an open-source database engine designed to help organizations store, retrieve, and modify data across web browsers, iOS and Android mobile devices, and other applications. The built-in C-language library lets users implement compact, self-reliant, and secure transactional database engines.

Do you need a database to store data?

Anything that you need to store permanently should go in a database. Relational databases are typically the most mature databases: they have withstood the test of time and continue to be an industry standard tool for the reliable storage of important data.

What do you understand about table in SQL What are the different components of a table?

SQL Tables A table is a collection of rows having one or more columns. A row is an instance of a row type. Every row of the same table has the same row type. The value of the n-th field of every row in a table is the value of the n-th column of that row in the table.


2 Answers

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

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.

like image 152
onedaywhen Avatar answered Sep 24 '22 14:09

onedaywhen


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

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

like image 37
Diego Avatar answered Sep 24 '22 14:09

Diego