If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > How to store a data with unknown amount of attributes?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-04-10, 03:46
tedd tedd is offline
Registered User
 
Join Date: Nov 2010
Posts: 24
Question How to store a data with unknown amount of attributes?

Hi everyone!
Got a question. I need to design a DB schema for an auto-related project. This DB will store plenty of data about cars. And there is a tricky task: we do not know, and will never know, the actual amount of characteristics we need to store about each car. Therefore, I can`t hard-code data`s attributes into DB schema. So, I think, there will be a table called “Cars attributes” where I will enumerate all cars attributes, and a table called “Car to car attribute” where actual data will be stored. But there is another problem - attributes could be of different types: integer, strings and dictionaries. (Example of dictionary: “Types of engine” with possible values: “V6”, “V8” and so on. Attribute can possess only one of these values). So, I suppose I need to create at least three tables: “Car attributes integer”, “Car attributes string”, “Car attributes dictionaries”.

And the question is - am I right? Is there an another way to store the data with unknown amount of attributes? Can you suggest some reading about this topic?

Thanks in advance!
Reply With Quote
  #2 (permalink)  
Old 11-04-10, 03:55
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
You can use something called the "Entity Attribute Value" pattern (Search this forum, this has been discussed several times, and your favorite search engine will also return several hits)


There are also DBMS specific solutions to this problem.

In PostgreSQL you could use a hstore column to store key/value pairs. This is also quite efficient when searching as the hstore columns can be indexed

In Oracle, DB2 (and probably SQL Server) you could use a XML Column that contains these mappings. Searching will need to be done using XPath expressions and XQuery. Those can be indexed for Oracle and DB2 (and I think SQL Server as well).
Reply With Quote
  #3 (permalink)  
Old 11-04-10, 10:44
tedd tedd is offline
Registered User
 
Join Date: Nov 2010
Posts: 24
shammat, thanks a lot! EAV is really what I need. I'm just going to enhance it a bit, in order to store different datatypes.
Reply With Quote
  #4 (permalink)  
Old 11-04-10, 11:16
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by tedd View Post
shammat, thanks a lot! EAV is really what I need.
Just keep in mind that EAV will be a nightmare if you plan to do reports on those attributes or let the user search on them. The EAV pattern usually doesn't scale very well here.
Reply With Quote
  #5 (permalink)  
Old 11-04-10, 12:07
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Enhancing EAV. I hear the sound of approaching thunder...
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #6 (permalink)  
Old 11-04-10, 12:15
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
And I think I know where the lightning will hit
Dave
Reply With Quote
  #7 (permalink)  
Old 11-06-10, 04:42
tedd tedd is offline
Registered User
 
Join Date: Nov 2010
Posts: 24
Quote:
Originally Posted by shammat View Post
Just keep in mind that EAV will be a nightmare if you plan to do reports on those attributes or let the user search on them. The EAV pattern usually doesn't scale very well here.
Actually I do need those attributes searchable. And comparable. And so on. Thats why I want to create different tables for different datatypes. attr_int, attr_bool, attr_char etc.

Quote:
Enhancing EAV. I hear the sound of approaching thunder...
Quote:
And I think I know where the lightning will hit
Looks like I don`t know something Am I doing something wrong? Are there any other methods of storing sparse matrix?
Reply With Quote
  #8 (permalink)  
Old 11-06-10, 05:01
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by tedd View Post
Thats why I want to create different tables for different datatypes. attr_int, attr_bool, attr_char etc.
One table for each datatype?
Now that sounds very wrong.
Reply With Quote
  #9 (permalink)  
Old 11-06-10, 05:19
tedd tedd is offline
Registered User
 
Join Date: Nov 2010
Posts: 24
Quote:
Originally Posted by shammat View Post
One table for each datatype?
Now that sounds very wrong.
Hmm. Whats wrong with that? As for me, it is more convenient to create different tables, than to use only one table for all values. I could index those tables, I could perform comparisons on values in 'integer table', I could store large text chunks in 'text table'...
Reply With Quote
  #10 (permalink)  
Old 11-06-10, 06:19
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
If I had to use an EAV approach I would use a single table with a single "key" column and multiple columns for different datatypes (and possibly one column indicating the datatype to be used)
Reply With Quote
  #11 (permalink)  
Old 11-06-10, 06:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by tedd View Post
Looks like I don`t know something Am I doing something wrong?
with EAV, it's not the storing of the data that's hard, it's the extraction

have you ever written any SQL to pull information out of an EAV database?

you should seriously try it first, it will open your eyes, tedd
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 11-06-10, 07:17
tedd tedd is offline
Registered User
 
Join Date: Nov 2010
Posts: 24
shammat, sounds really interesting! Your approach is definitely more convenient than mine. Now I`m going to play around with data, as r937 suggested.

Last edited by tedd; 11-06-10 at 07:27.
Reply With Quote
  #13 (permalink)  
Old 11-06-10, 07:43
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Try to retrieve something like the following:

- Show count of cars that have a V6 engine for each manufacturer
- What's the average horsepower for a given engine type?
- How many cars have 4 cylinders but less than 100hp and are built by a specific manufacturer?
Reply With Quote
  #14 (permalink)  
Old 11-06-10, 08:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by shammat View Post
Try to retrieve something like the following:
here come da lightning fo sho
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #15 (permalink)  
Old 11-06-10, 09:52
tedd tedd is offline
Registered User
 
Join Date: Nov 2010
Posts: 24
Quote:
Originally Posted by r937 View Post
here come da lightning fo sho
Gonna do it. Have no options anyway
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On