views:

1522

answers:

15

It it ok to have a table with just one column? I know it isn't technically illegal, but is it considered poor design?

EDIT:

Here are a few examples:

  • You have a table with the 50 valid US state codes, but you have no need to store the verbose state names.
  • An email blacklist.

Someone mentioned adding a key field. The way I see it, this single column WOULD be the primary key.

A: 

I would say in general, yes. Not sure why you need just one column. There are some exceptions to this that I have seen used effectively. It depends on what you're trying to achieve.

They are not really good design when you're thinking of the schema of the database, but really should only be used as utility tables.

I've seen numbers tables used effectively in the past.

Brendan Enrick
A: 

The purpose of a database is to relate pieces of information to each other. How can you do that when there is no data to relate to?

Maybe this is some kind of compilation table (i.e. FirstName + LastName + Birthdate), though I'm still not sure why you would want to do that.

EDIT: I could see using this kind of table for a simple list of some kind. Is that what you are using it for?

Matthew Jones
No, the primary purpose of a database is to store information.
Spencer Ruport
But a spreadsheet can store information. And how is the information useful if it is just a bunch of disjointed numbers and letters with no correlation between them?
Matthew Jones
The information can be useful because the application using the database *needs* it and it isn't a fixed set. That is, the DB is simply the most *convenient* place to put information to be used by a database app - relational or otherwise. I'm pretty sure that you'd agree that we're not building apps to prove our purity with respect to the relational ideal. Instead, we build apps to be useful.
Mark Brittingham
@Mark - That is what I meant by a simple list. Guess I did not explain myself very well.
Matthew Jones
@SR - No, the primary purpose of a database is to retrieve information. As the rows of interest more often than not are dependant on other pieces of data I think MJ's original comment stands.
CurtainDog
+17  A: 

I've used them in the past. One client of mine wanted to auto block anyone trying to sign up with a phone number in this big list he had so it was just one big blacklist.

Spencer Ruport
+8  A: 

If there is a valid need for it, then I don't see a problem. Maybe you just want a list of possibilities to display for some reason and you want to be able to dynamically change it, but have no need to link it to another table.

Kevin
+1 - Bottom line, this is the right answer in my book
Mark Brittingham
+1 for concise, clear answer.
Matthew Jones
Why can't a one column table be linked to another table?
Aheho
Um, well, it could be. I just don't think that most of the time it would be a good idea.
Kevin
Why not? Take the state code table as an example. Why wouldn't you use that as a foriegn key constraint against another table with address fields?
Aheho
That's a great example of a time that it would be a good idea.
Kevin
A: 

Yes that is perfectly fine. but an ID field couldn't hurt it right?

Eric
Actually, it can. When you have a definitive list of valid values, the last thing you want is an ID field because it implies that the values are not unique. If 'LightBlue' is a key, then you don't want someone thinking that 'LightBlue' with id 1 might be different from 'LightBlue' with id 4.
jbourque
Who says the Id has to be identity? Or part of the key?
Eric
It could be a synthetic key and the original field could have a unique constraint on it.
Mark Canlas
+1  A: 

The only use case I can conceive of is a table of words perhaps for a word game. You access the table just to verify that a string is a word: select word from words where word = ?. But there are far better data structures for holding a list of words than a relational database.

Otherwise, data in a database is usually placed in a database to take advantage of the relationships between various attributes of the data. If your data has no attributes beyond its value how will these relationship be developed?

So, while not illegal, in general you probably should not have a table with just one column.

jmucchiello
Similar to this is the table of numbers that comes in very handy to stop looping.
u07ch
+56  A: 

In terms of relational algebra this would be a unary relation, meaning "this thing exists"

Yes, it's fine to have a table defining such a relation: for instance, to define a domain.

The values of such a table should be natural primary keys of course.

A lookup table of prime numbers is what comes to my mind first.

Quassnoi
+1 Good answer, Quassnoi.
Mark Brittingham
+1: The table is a set of values that happen to be primitive types of your RDBMS.
S.Lott
+1 for providing answer based on relational theory.
lubos hasko
+5  A: 

There would be rare cases where a single-column table makes sense. I did one database where the list of valid language codes was a single-column table used as a foreign key. There was no point in having a different key, since the code itself was the key. And there was no fixed description since the language code descriptions would vary by language for some contexts.

In general, any case where you need an authoritative list of values that do not have any additional attributes is a good candidate for a one-column table.

jbourque
+4  A: 
Doliveras
Why having countries_id? To insert a country, you need to know its name in at least one language, and this will result in a country_id appearing in countries_description. A country_id without countries_description entry is meaningless. "Mr. Barack Obama, President of COALESCE(14243, country_name) RETURNED NULL VALUE, today met with Dmitry Medvedev, President of Россия"
Quassnoi
@Doliveras: OK, I see now, +1. I'd rather use ISO 3166-1 for coutry_code, however. Unlike numeric id, a 3-letter country code gives an idea of what country is mentioned to almost everyone in the world who can read Latin alphabet.
Quassnoi
Here's another way I've implemented to accommodate natural language translations in the same table. Granted, many fields are nullable as a result, but you can offload data integrity to custom triggers.CREATE TABLE "DBA"."myLocalisedTable" ( "entry_id" INTEGER NOT NULL DEFAULT AUTOINCREMENT, "master_entry_id" INTEGER NULL, "master_entry_label" VARCHAR(200) NULL, "language_id" INTEGER NULL, "localised_entry_label" VARCHAR(300) NULL,
Vincent Buck
+1  A: 

Yes as long as the field is the primary key as you said it would be. The reason is because if you insert duplicate data those rows will be readonly. If you try to delete one of the rows that are duplicated. it will not work because the server will not know which row to delete.

Eric
That's ridiculous. A delete operation without a primary key or constraint is going to delete all matching rows, not ignore them.
Mystere Man
By 'not work', he could mean "not work as expected", which covers the "hey I deleted one row but both are gone" condition.
GWLlosa
+16  A: 

Yes, it's certainly good design to design a table in such a way as to make it most efficient. "Bad RDBMS Design" is usually centered around inefficiency.

However, I have found that most cases of single column design could benefit from an additional column. For example, State Codes can typically have the Full State name spelled out in a second column. Or a blacklist can have notes associated. But, if your design really does not need that information, then it's perfectly ok to have the single table.

Mystere Man
+1  A: 

I use single-column tables all the time -- depending, of course, on whether the app design already uses a database. Once I've endured the design overhead of establishing a database connection, I put all mutable data into tables where possible.

I can think of two uses of single-column tables OTMH:

1) Data item exists. Often used in dropdown lists. Also used for simple legitimacy tests.

Eg. two-letter U.S. state abbreviations; Zip codes that we ship to; words legal in Scrabble; etc.

2) Sparse binary attribute, ie., in a large table, a binary attribute that will be true for only a very few records. Instead of adding a new boolean column, I might create a separate table containing the keys of the records for which the attribute is true.

Eg. employees that have a terminal disease; banks with a 360-day year (most use 365); etc.

-Al.

A. I. Breveleri
+3  A: 

No problem as long as it contains unique values.

Vulcan Eager
A: 

Mostly I've seen this in lookup type tables such as the state table you described. However, if you do this be sure to set the column as the primary key to force uniqueness. If you can't set this value as unique, then you shouldn't be using one column.

HLGEM
A: 

All my tables have at least four tech fields, serial primary key, creation and modification timestamps, and soft delete boolean. In any blacklist, you will also want to know who did add the entry. So for me, answer is no, a table with only one column would not make sense except when prototyping something.