130

Is it OK to have a table with just one column?

I know it isn't illegal.

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. But the single column would be the primary key.

philipxy
  • 14,867
  • 6
  • 39
  • 83
Aheho
  • 12,622
  • 13
  • 54
  • 83
  • but atleast dont create an Index for it ! – Sathya Jun 04 '09 at 16:45
  • 4
    US state codes are classical example of defining a domain – Quassnoi Jun 04 '09 at 16:59
  • 4
    I've seen a database table used to hold a lock value for an application before – Russ Cam Jun 05 '09 at 11:49
  • My use for this pattern was to create sets of data. Each record in the main table has a SET field. Records with the same SET are connected. How do you insert multiple records with the same SET? 'INSERT INTO sets VALUES ()' and then use the last insert ID as your SET ID to attached to the records. Then again, maybe I could have used UUIDs, but something feels wrong about that. – William Entriken Jun 14 '12 at 16:18
  • One more use case is a boolean flag that is usually FALSE. I would create a table with one column that is both - primary and foreign key. – Paul Spiegel Oct 29 '16 at 14:42

15 Answers15

183

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
  • 413,100
  • 91
  • 616
  • 614
99

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

Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
30

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
  • 34,865
  • 12
  • 85
  • 147
20

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
  • 7,162
  • 11
  • 46
  • 70
  • 3
    Why can't a one column table be linked to another table? – Aheho Jun 04 '09 at 17:27
  • Um, well, it could be. I just don't think that most of the time it would be a good idea. – Kevin Jun 05 '09 at 14:19
  • 2
    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 Jun 05 '09 at 14:31
  • 1
    That's a great example of a time that it would be a good idea. – Kevin Jun 08 '09 at 12:37
13

One case that I found sometimes is something like this:

Table countries_id, contains only one column with numeric ID for each country.

Table countries_description, contains the column with country ID, a column With language ID and a column with the localized country name.

Table company_factories, contains information for each factory of the company, including the country in Wich is located.

So to maintain data coherence and language independent data in the tables the database uses this schema with tables with only one column to allow foreign keys without language dependencies.

In this case I think the existence of one column tables are justified.

Edited in response to the comment by: Quassnoi


(source: ggpht.com)

In this schema I can define a foreign key in the table company_factories that does not require me to include Language column on the table, but if I don't have the table countries_id, I must include Language column on the table to define the foreign key.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Doliveras
  • 1,794
  • 2
  • 14
  • 30
  • 2
    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 Jun 04 '09 at 17:13
  • 4
    @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 Jun 05 '09 at 11:49
  • 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 Jun 05 '09 at 12:01
7

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.

Jeremy Bourque
  • 3,533
  • 1
  • 21
  • 18
5

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
  • 325
  • 1
  • 3
5

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
  • 94,695
  • 15
  • 113
  • 186
  • While using a single column here is probably fine, keep in mind that you can set up uniqueness constraints on other columns as well. – Stealth Rabbi Sep 23 '19 at 14:56
4

No problem as long as it contains unique values.

Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
2

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.

Community
  • 1
  • 1
Brendan Enrick
  • 4,277
  • 2
  • 26
  • 40
1

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
  • 25,644
  • 17
  • 102
  • 155
  • 10
    No, the primary purpose of a database is to store information. – Spencer Ruport Jun 04 '09 at 16:45
  • 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 Jun 04 '09 at 16:46
  • 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 Jun 04 '09 at 16:53
  • @Mark - That is what I meant by a simple list. Guess I did not explain myself very well. – Matthew Jones Jun 04 '09 at 16:54
  • 1
    @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 Jun 05 '09 at 12:02
1

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
  • 7,930
  • 17
  • 96
  • 128
  • 2
    That's ridiculous. A delete operation without a primary key or constraint is going to delete all matching rows, not ignore them. – Erik Funkenbusch Jun 04 '09 at 17:33
  • 1
    By 'not work', he could mean "not work as expected", which covers the "hey I deleted one row but both are gone" condition. – GWLlosa Jun 04 '09 at 17:45
  • Or, if you try to delete a record in SSMS from the "Open Table" view, it will actually throw up a dialog that the record can't be uniquely identified and then do nothing... – Michael Fredrickson Jun 15 '11 at 17:39
-1

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
  • 18,754
  • 7
  • 41
  • 61
-1

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.

  • 1
    It sounds like you're mixing up a data table with a transaction table. In my opinion, these should be two separate things. – Josh Noe Dec 24 '14 at 17:05
-2

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

Eric
  • 7,930
  • 17
  • 96
  • 128
  • 7
    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. – Jeremy Bourque Jun 04 '09 at 16:58
  • Who says the Id has to be identity? Or part of the key? – Eric Jun 04 '09 at 17:06
  • 3
    It could be a synthetic key and the original field could have a unique constraint on it. – Mark Canlas Jun 04 '09 at 18:14