Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to save trivial user states (e.g. dismissed welcome msg) in database?

Should I use (create) a column for every new state? Or one field with a bunch of comma separated states (alternatively a json obj)? Any suggestions welcome.

UPDATE

First let me day thanks for the answers. I just want to clear up, what options I see:

  1. Put a column for every state in the user row (initial plan) / Can get messy with lots of states (in the future)
  2. Put one column with json/xml data in the user row / Easy to maintain (no db change required), but doesn't feel right
  3. Have a dedicated states table (thx lhiles)/ Sounds cool, how would this table look like?

I'm looking for pros/cons of the different implementations. Again: Thanks!

like image 773
Aron Woost Avatar asked Nov 24 '25 19:11

Aron Woost


1 Answers

Create a column for each state. This is proper data normalization.

With a column for each state you can retrieve as few or as many states as needed for the current operation.

All of the states returned will be contained in a single row with each column named. This makes referencing each state value very easy.

It allows you to easily add constraints to each state as needed. (State X can only contain '1' or '2'.)

It allows you to easily query states across users. (How many users have set a state value to 'X'?)

like image 99
Scott Bruns Avatar answered Nov 26 '25 14:11

Scott Bruns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!