views:

596

answers:

2

I have been looking for a database solution to allow user defined fields and values (allowing an unlimited number). At first glance, EAV seemed like the right fit, but after some reading I am not sure anymore.

What are the pros and cons of EAV?

Is there an alternative database method to allow user defined attributes/fields and values?

+2  A: 

This is not to be considered an exhaustive answer, but just a few points on the topic.

Since the question is also tagged with the [sql] tag, let me say that, in general, relational databases aren't particularly suitable for storing data using the EAV model. You can still design an EAV model in SQL, but you will have to sacrifice many advantages that a relational database would give. Not only you won't be able to enforce referential integrity, use SQL data types for values and enforce mandatory attributes, but even the very basic queries can become difficult to write. In fact, to overcome this limitation, several EAV solutions rely on data duplication, instead of joining with related tables, which as you can imagine, has plenty of drawbacks.

If you really require a schemaless design, "allowing an unlimited number of attributes", your best bet is probably to use a NoSQL solution. Even though the weaknesses of EAV relative to relational databases also apply to NoSQL alternatives, you will be offered additional features that are difficult to achieve with conventional SQL databases. For example, usually NoSQL datastores can be scaled much easier than relational databases, simply because they were designed to solve some sort of scalability problem, and they intentionally dropped features that make scaling difficult.

Many cloud computing platforms (such as those offered by Amazon, Google and Microsoft) are featuring datastores based on the EAV model, where an arbitrary number of attributes can be associated with a given entity. If you are considering deploying your application to the cloud, you may consider this both as a business advantage, as well as a technical one, because the strong competition between the big vendors is pushing the value-to-cost ratios to very high levels, by continually pushing up on the features and pushing down the financial and implementation costs.

Daniel Vassallo
i dont see how EAV is a technical advantage : its more complicated to build, to query and to insert, and readability is very low. What would be a simple and fast "select * from" becomes a slow monster sql query. And i m not even talking about trying to do some reporting based on a EAV model.
guigui42
@guigui42: Thanks for your comment. You have a point, but my answer was not intended to be exhaustive. Nevertheless, since it was marked as accepted by the OP, I have updated the answer with some further considerations.
Daniel Vassallo
A: 

Is there an alternative database method to allow user defined attributes/fields and values?

One alternative is to change the database schema based on user input: for example when the user wants a new field, then add a corresponding column to the database.

ChrisW
The downside to this is that you will have tons of empty fields
Nic Hubbard
@ChrisW: The scenarios where that is feasible are very limited in my opinion.
Daniel Vassallo