When designing a relational database schema, when should fields be mutable, versus when should they be in a separate table with immutable values?
-
For example, I'm modelling a Quora user and their credits. I already have a separate table for transactions - earning credits, and spending credits. I just need to store the Quora user and their credits. The simple solution is to make the table mutable. The table has a field for the user ID and a field for the credits. The field for the user never changes, but the field for the credits changes through time. However, this looks like an object-oriented programming model with mutable state. In a functional-style program, the object would be immutable. To increase the credits, instead of modifying the user object, a new user object is created with the same user ID but with a change in the credits. Does it make sense to do this in the database as well? This will now be two tables, with the second table containing the different values of the credits as they vary in time.
-
Answer:
In database terms everything is mutable. You have data sets, and you can change the values, and your pointer to the data is the primary key of the table. You never change the pointer when the data changes, but instead change the data. That's because the relations between tables are using those pointers (keys) and changing them would mean expensive updates of all indexes and relations associated. If your problem is tracking the data changes over time, this can be done by using either an orthogonal normalized table or by adding metadata to the object.
Lucian Nutiu at Quora Visit the source
Other answers
You're perhaps mixing up the abstraction in the data layer or say, in something like Hibernate with the underlying data model itself. In a relational data model, there is no true manifestation of the concept of "mutable" vs. "non-mutable". What you should really be looking at is normalization. That will help you design the schema such that user profile information is one table (your "non-mutable" - mostly - data) while the transactional information like user credits, last login, number of followers, etc., (your "mutable" data) is in another or other table/s.
Suhas Mallya
Related Q & A:
- Is it mandatory to have broadcast receiver as a separate file?Best solution by stackoverflow.com
- What is Hash Table, how to create a hash table?Best solution by Stack Overflow
- How to change column data's as a separate column wise format in a SQL Server?Best solution by stackoverflow.com
- If I hooked up 2 amps would I need to buy a separate battery?Best solution by Yahoo! Answers
- What are some best practices to follow when designing for users completely unfamiliar with computers?Best solution by User Experience
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.