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

11-04-10, 03:46
|
|
Registered User
|
|
Join Date: Nov 2010
Posts: 24
|
|
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!
|
|

11-04-10, 03:55
|
|
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).
|
|

11-04-10, 10:44
|
|
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.
|
|

11-04-10, 11:16
|
|
Registered User
|
|
Join Date: Nov 2003
Posts: 2,407
|
|
Quote:
Originally Posted by tedd
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.
|
|

11-04-10, 12:07
|
|
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"
|
|

11-04-10, 12:15
|
|
Registered User
|
|
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
|
|
And I think I know where the lightning will hit
Dave
|
|

11-06-10, 04:42
|
|
Registered User
|
|
Join Date: Nov 2010
Posts: 24
|
|
Quote:
Originally Posted by shammat
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?
|
|

11-06-10, 05:01
|
|
Registered User
|
|
Join Date: Nov 2003
Posts: 2,407
|
|
Quote:
Originally Posted by tedd
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.
|
|

11-06-10, 05:19
|
|
Registered User
|
|
Join Date: Nov 2010
Posts: 24
|
|
Quote:
Originally Posted by shammat
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'...
|
|

11-06-10, 06:19
|
|
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)
|
|

11-06-10, 06:32
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
Quote:
Originally Posted by tedd
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
|
|

11-06-10, 07:17
|
|
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.
|

11-06-10, 07:43
|
|
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?
|
|

11-06-10, 08:54
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
Quote:
Originally Posted by shammat
Try to retrieve something like the following:
|
here come da lightning fo sho 
|
|

11-06-10, 09:52
|
|
Registered User
|
|
Join Date: Nov 2010
Posts: 24
|
|
Quote:
Originally Posted by r937
here come da lightning fo sho 
|
Gonna do it. Have no options anyway 
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|