I am very confuse about the two structure. What are the advantage and disadvantage of this two table? Which one is better and why?
TABLE1
id, name, age, birthdate, address
somedata1 somedata1 somedata1 somedata1 somedata1
somedata2 somedata2 somedata2 somedata2 somedata2
somedata3 somedata3 somedata3 somedata3 somedata3
TABLE2
id, col_name, col_value
somedata name somedata
somedata age somedata
somedata birthdate somedata
somedata address somedata
somedata2 name somedata2
somedata2 age somedata2
somedata2 birthdate somedata2
somedata2 address somedata2
somedata3 name somedata3
somedata3 age somedata3
somedata3 birthdate somedata3
somedata3 address somedata3
In common case, second table is anti-pattern in context of database design. And, even more, it has specific name: Entity-Attribute-Value (EAV). There are some cases, when using this design is justified, but that are rare cases - and even there it can be avoided.
Data integrity support
Despite the fact, that such structure seems to be more "flexible" or "advanced", this design has weakness.
"customer_name"
as attribute name in first case - and another developer will forget that and use "name_of_customer"
. And.. it's ok, DB will pass that and you'll end with hours spent on debugging this case.Row reconstruction
In addition, row reconstruction will be awful in common case. If you have, for example, 5 attributes - that will be 5 self-table JOIN
-s. Too bad for such simple - at first glance - case. So I don't want even imagine how you'll maintain 20 attributes.
My point is - no. In RDBMS there will always be a way to avoid this. It's horrible. And if EAV is intended to be used, then best choice may be non-relational databases.
in second case (table2) this is complex and take much time to find data when we make query for it. this case is used when you don't know about number of columns or they are varies, if you have fixed length of columns then used first case(table1) because in this case data find fast way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With