tags:

views:

977

answers:

13

What is the point (if any) in having a table in a database with only one row?

Note: I'm not talking about the possibility of having only one row in a table, but when a developer deliberately makes a table that is intended to always have exactly one row.

Edit:

The sales tax example is a good one.

I've just observed in some code I'm reviewing three different tables that contain three different kinds of certificates (a la SSL), each having exactly one row. I don't understand why this isn't made into one large table; I assume I'm missing something.

+2  A: 

It can be useful sometime to emulate some features the Database system doesn't provide. I'm thinking of sequences in MySQL for instance.

jeje
+2  A: 

For some things you only need one row - typically system configuration data. For example, "current sales tax rate". This might change in the future and so shouldn't be hardcoded, but you'll typically only ever need one at any given time. This kind of data needs to be in the database so that queries can use it in computations.

Vinay Sajip
Then do you have each state as a column?ie: Select California From CurrentSalesTaxRate ?
NerdFury
I'm not a huge fan of this, for instance here in Canada, our national sales tax has changed twice in the last 5 years, and provincial ones are being changed as well. So a table of sales tax with a tax/rate/effective/expiry date works best.
Nathan Koop
Depends on the application. For example, if you need to handle multiple sales tax rates for different states, it wouldn't be in a single-row table; but if you're doing a single-country application in Europe, say, you might put the Value Added Tax rate into such a table.
Vinay Sajip
Using a tax rate was just an example. There are lots of other configuration items which change much less often.
Vinay Sajip
For simple system configuration data, I would consider a key-value pair table with a row for each configuration item.
Jason Musgrove
And if there was only one configuration item, there would only be one row in the table :-)
Vinay Sajip
+1  A: 

It's not necessarily a bad idea.

What if you had some global state (say, a boolean) that you wanted to store somewhere? And you wanted your stored procedures to easily access this state?

You could create a table with a primary key whose value range was limited to exactly one value.

Aaron F.
+4  A: 

I've seen something like this when a developer was asked to create a configuration table to store name-value pairs of data that needs to persist without being changed often. He ended up creating a one-row table with a column for each configuration variable. I wouldn't say it's a good idea, but I can certainly see why the developer did it given his instructions. Needless to say it didn't pass review.

I've just observed in some code I'm reviewing three different tables that contain three different kinds of certificates (a la SSL), each having exactly one row. I don't understand why this isn't made into one row; I assume I'm missing something.

This doesn't sound look a good design, unless there are some important details you don't know about. If there are three pieces of information that have the same constraints, the same use and the same structure, they should be stored in the same table, 99% of the time. That's a big part of what tables are for fundamentally.

Welbog
Yeah, this was my impression as well, but I thought that maybe there were some special speedups for 'singleton' tables (singleton might not be the right word here, but you get the idea)
Jeremy Powell
No boost that the right clustered index wouldn't provide.
Welbog
A: 

Unless there are insert constraints on the table a timestamp for versioning then this sounds like a bad idea.

CptSkippy
A: 

What is the point (if any) in having a table in a database with only one row?

A relational database stores things as relations: a tuples of data satisfying some relation.

Like, this one: "a VAT of this many percent is in effect in my country now".

If only one tuple satisifies this relation, then yes, it will be the only one in the table.

SQL cannot store variables: it can store a set consisting of 1 element, this is a one-row table.

Also, SQL is a set based language, and for some operations you need a fake set of only one row, like, to select a constant expression.

You cannot just SELECT out of nothing in Oracle, you need a FROM clause.

Oracle has a pseudotable, dual, which contains only one row and only one column.

Once, long time ago, it used to have two rows (hence the name dual), but lost its second row somewhere on its way to version 7.

MySQL has this pseudotable too, but MySQL is able to do selects without FROM clause. Still, it's useful when you need an empty rowset: SELECT 1 FROM dual WHERE NULL

I've just observed in some code I'm reviewing three different tables that contain three different kinds of certificates (a la SSL), each having exactly one row. I don't understand why this isn't made into one large table; I assume I'm missing something.

It may be a kind of "have it all or lose" scenario, when all three certificates are needed at once:

SELECT  *
FROM    ssl1
CROSS JOIN
        ssl2
CROSS JOIN
        ssl3

If any if the certificates is missing, the whole query returns nothing.

Quassnoi
I definitely don't see the coss join pattern here, but that's a good point.
Jeremy Powell
@Jeremy: if you don't need any other condition except for mere presense or absense of a row, `CROSS JOIN` is what you need.
Quassnoi
+1  A: 

A table with a single row can be used to store application level settings that are shared across all database users. 'Maximum Allowed Users' for example.

PaulG
A: 

Funny... I asked myself the same question. If you just want to store some simple value and your ONLY method of storage is an SQL server, that's pretty much what you have to do. If I have to do this, I usually end up creating a table with several columns and one row. I've seen a couple commercial products do this as well.

ProZach
A: 

We have used a single-row table in the past (not often). In our case, this table was used to store system-wide configuration values that were updatable via a web interface. We could have gone the route of a simple name/value table, but the end client preferred a single row. I personally would have preferred the latter, but it really is up to preference, especially if this table will never have any sort of relationship with another table.

Keith
The single table was likely a better idea than a name value table. Ugh I cringe just at the thought of someone using one of those if there is any alternative as they can be horrible for performance.
HLGEM
A: 

If your database is your application, then it probably makes sense for storing configuration data that might be required by stored procedures implementing business logic.

If you have an application that could use the file system to store information, then I don't think there is an advantage to using the database over an XML or flat file, except maybe that most developers are now far more well versed in using SQL to store and retrieve data than accessing the file system.

NerdFury
A: 

There was a table set up like this in a project I inherited. It was for configuration data, and the reason that was given was that it made for very simple queries:

SELECT WidgetSize FROM ConfigTable
SELECT FooLength  FROM ConfigTable

Okay fine. We converted to a generalized configuration table:

ID  Name  IntValue StringValue TextValue

This has served our purposes well.

Jack Straw
A: 

I really cannot figure out why this would be the best solution. It seams more efficient to just have some kind of config file that will contain the data that would be in the tables one row. The cost of connecting to the database and querying the one row would be more costly. However if this is going to be some kind of config for the database logic. Then this would make a little bit more sense depending on the type of database you are using.

MANCHUCK
A: 

I use the totally awesome rails-settings plugin for this http://github.com/Squeegy/rails-settings/tree/master

It's really easy to set up and provides a nice syntax:

  Settings.admin_password = 'supersecret'
  Settings.date_format    = '%m %d, %Y'
  Settings.cocktails      = ['Martini', 'Screwdriver', 'White Russian']
  Settings.foo            = 123

Want a list of all the settings?

  Settings.all            # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}

Set defaults for certain settings of your app. This will cause the defined settings to return with the Specified value even if they are not in the database. Make a new file in config/initializers/settings.rb with the following:

Settings.defaults[:some_setting] = 'footastic'