Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Is it good way to use more than 200 columns in single Database table? while in one record only 20 columns would have the value and rest 180 will having null value?
Example- Suppose I have 20 Objects each having 15 attributes(Properties) in which 5 attributes are common for all 10 Objects and other 10 attributes are different for all 20 Objects.
Here I defined 5 Columns for Common attributes and what about rest of the 180 attributes? Any one have good idea to store this data with optimum way.
Posted

An alternative to answer one (which is a good method) is a two-table answer relying upon tagging.

Table 1 has your primary key data.

Table 2 uses a ref to the primary key, a tag, and a value.

The tag is equivalent to a column name. You add as many tagged values as you need for each reference. No need to create a tag type until it's actually used.

The advantage of this type of tagging (instead of columns or normalization) is that it's extensible without adding any additional values to your ref. Values become part of the vocabulary simply because you use the string as a tag.



Clarifications/Examples

Table 1
SQL
recid  Your_Main_Data
1      Kittens
2      Roaches

Table 2 (replaces all of the columns)
ref  tag     value
1    pet     domestic
1    taste
2    bug     nasty
2    taste   bitter
2    pet     vermon

So 
column 'ref'   ties in to your main table
column 'tag'   would be like a column header
column 'value' would be the content under your column

Note that only those you need are used.
Since there's not external list for tags, you can use anything at any time.
 
Share this answer
 
v2
Comments
Member 10815407 17-Oct-14 8:15am    
I am not able to understand your solution. Can you pls give me more details with an example
W Balboos, GHB 17-Oct-14 9:20am    
I updated the answer with more detail/
Member 10815407 17-Oct-14 9:36am    
I like your answer What would be the datatype of column 'Value' in Table2?
because attribute value can be integer,varchar or datetime value
and how can me use type casting and DateTime formating
First, this is too vague. A sample (not all 200 ofcourse !) would have helped rather than verbose.

Anyway, In my opinion, this is where normalization would come into picture

Table1: Store Object with ObjectID as PK, and any other Object details

Table2: 5 columns for common Attributes and one for ObjectID as FK

Table3: 180 rows for each Object in Table 1 with ObjectID as FK

If, you want, you can combine Table1 and Table2 if table1 is not going to have more columns.

This is best I can think with information you have provided and there could be better ways.

Thanks
Milind
 
Share this answer
 
Comments
Member 10815407 17-Oct-14 8:19am    
Suppose in future 30 more Objects are added then we need to add 30*10=300 new columns in Table3 and Now in Table3 total no of columns will be 500. Then how we will manage the table.
MT_ 17-Oct-14 8:52am    
Man, I said ROWS not columns :-)
I use that scenario in an application and made the data column as a varchar.

Depending upon you programming language, storage is either very easy or you'll need to cast to char types. If it's datatime type, you'll need to do the casting in your query when you recover that type.

Similarly, if you want to read the column data you saved (vertical) into a single row, then you'll need a series of inner-joins.

There's a basic rule in operation here: a simplified extremely generic storage method as I have described may require extra work to retreive the data. There is no free ride. The simplest query to retreive data would be your 200 column horror, each column of the correct data type. You'd have no way to extend the stored data without adding columns. I prefer the other extreme: I'll write the block of inner-joins to get the data I require and can cast the data types at when needed.

.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900