To answer the original question, there probably
is a maximum number of columns allowed in a table for any given RDBMS - but it's not a limit you're ever likely to run into. The limit for an Oracle 11g table is 1000 columns (see
). If you're running close to that kind of number, it should be ringing alarm bells about your design!
The short rule is that if you have repeating groups in your table - columns called item1, item2, item3 - they should probably be split out into a separate item table. The longer corollary is that sometimes it's better to leave them be, depending on the nature of the data and how you intend to use it.
Let's look at something that's already been alluded to - mailing addresses. Let's assume that any address in the world can be written in (at most) 5 lines of text; what's the best way to store the address of a customer? (Let's assume that for some reason you
can't just store it in a single column with line breaks embedded where you need them)
Here's a few options (there are others):
[ol][li]We could put 5 columns into the customer table - address1, address2, address3, etc.[/li]
[li]We could add a new table [tt]customer_address_line[/tt], with columns [tt]customer_id[/tt], [tt]line_no[/tt] and [tt]address_line[/tt].[/li]
[li]We could add an [tt]address[/tt] table (with the address lines stored either within the table or in a further denormalised table, as above), and just add an [tt]address_id[/tt] to the customer table.[/li][/ol]
Option 1 is the simplest, but violates the "short rule" given above. However, if you think about how you might
use this information, it looks pretty good. You're likely to capture the whole address at once, through some form of data entry; and use it all at once - say by printing a mailing list. The fact that all the address is in the same database row is very handy.
Option 2, which might appear to be the more "proper" solution, could be a fiddle to implement. Imagine writing a query to populate a mailing list, where you have to attach between 1 and 5 address_line records to each customer row. It's a lot more complicated for no great benefit.
Option 3 is useful if you want to do more with addresses than use them to send stuff through the mail. If you want to know whether 2 of your customers share an address, or you want to know where a particular address
is geospatially, this could be the approach to take. Note also that you may be able to make use of ready-made address tables - the PAF or the NLPG in the UK, for example - which can improve the accuracy of your customer data.
-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd