What database field type should i use?


#1

in a database table if there is a field which has a certain set of
fixed values. for example
staus => {Single, Married, Divorced }
OR
state => {California, Albama, Olaska …}

so what should be preferred way out of the following for storing the
values

  1. Keep the field as “string(Rails)” VARCHAR(MySQL) itself …and
    while showing the field just show the field value.

  2. Keep the field internally as a code like {:california =>
    01, :albama => 02, washington => 03 …} but while showing the state
    show only the corresponding state.

By using option 2, a certain disadvantage is extra computation time
required to find out corresponding state name based on code when
showing the state field to user. But an advantage could be in terms of
smaller database. In my opinion, saving 01 as an integer could save
significant space than storing “california” if number of records
happen to be in tens of thousands .

please suggest ??

vipin


#2

In the case of method 2 where would you store the tens of thousands of
strings if not in the database?
Colin

2009/5/7 Vipin removed_email_address@domain.invalid


#3

Vipin wrote:

in a database table if there is a field which has a certain set of
fixed values. for example
staus => {Single, Married, Divorced }
OR
state => {California, Albama, Olaska …}

so what should be preferred way out of the following for storing the
values

I use ENUM() columns in mysql - rails treats them as strings so
everything works (apart from schema dumps)


#4

just realize…

in the case od 1st method:
everything is simple, but db grows… but… you made some typo mistake
(eg Albama instead of Alabama) and what then… repair entire table to
get the correct results? weird

2nd method:
I’m using it for a small enumeration lists (not hundreds/thousands items

  • then is better to use db enumeratin methods, like countries table and
    country_id column)

in app/model/some_model.rb

@@enum_list = %w( value1 value2 value3 )

to use in erb templates for form.select() helper

def self.enum_list_for_select
@@enum_list.enum_with_index.collect{|k,v| [k,v]}
end

def enum_list_to_text
self.enum_list.nil? ? ‘undefined’ : @@enum_list[self.enum_list]
end

tom

Vipin wrote:

while showing the field just show the field value.
happen to be in tens of thousands .

please suggest ??

vipin

Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz


#5

2009/5/7 Andrew P. removed_email_address@domain.invalid

I use ENUM() columns in mysql - rails treats them as strings so
everything works (apart from schema dumps)

Sorry, I misunderstood the question, I assumed you wanted to know how to
store the country names in a countries table. I did not realise that in
option 1 you meant storing the string in every record that references
country, though that is what you said, I did not read it carefully. Now
that I understand, I would suggest some variant of option 2, store an id
in
each record referencing a country and determine the string by an
enumeration
or table lookup later. Worrying too much about computing time during
development is a mugs game, an application always ends up with most of
its
computing time in an area you do not expect. Worry about optimisation
later
when (or more likely if) a particular area becomes a problem.

Colin


#6

Sometimes it’'s a matter of taste or design constraint, but why not
use a small join table and a foreign key? ENUM is also a great choice,
as these guys have pointed out, but sometimes there is a bit of
overhead – but you shouldn’t worry about that until it becomes an
issue. Personally, I find it easier to maintain a simple join table
and FK relationships than to mess with ENUM field types.

I don’t know if this is still applicable, but it seems like there is a
bit of data massaging in Rails for the ENUM type (Rails converts it
internally to VARCHAR):

http://lists.rubyonrails.org/pipermail/rails/2005-January/001536.html

I like to let the database do as much work as it can, :slight_smile:

HTH!

Billee D.


#7

It’s about performance.

if you’ll have marriage status (single/married/divorced/etc) in table
marriagestatuses, and using belongs_to/has_many via marriagestatus_id,
and not using :include => marriagestatuses in you ‘queries’, db will do
a lot of queries just for a few enumerables.

so each of us have to decide whether to use another ‘enumerate table’ or
nor

tom

Billee D. wrote:

fixed values. for example
2. Keep the field internally as a code like {:california =>
please suggest ??

vipin

Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz


#8

in database only …i am going to store both string /id in database
only


#9

On May 7, 5:47 pm, Tom Z Meinlschmidt removed_email_address@domain.invalid wrote:

country_id column)
def enum_list_to_text

staus => {Single, Married, Divorced }
01, :albama => 02, washington => 03 …} but while showing the state

vipin

Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz

Thanks ! i will try this.


#10

On May 7, 6:24 pm, Colin L. removed_email_address@domain.invalid wrote:

that I understand, I would suggest some variant of option 2, store an id in
each record referencing a country and determine the string by an enumeration
or table lookup later. Worrying too much about computing time during
development is a mugs game, an application always ends up with most of its
computing time in an area you do not expect. Worry about optimisation later
when (or more likely if) a particular area becomes a problem.

Colin

Colin,
yes i guess 2nd variant will be better.
thanks


#11

On May 7, 8:09 pm, “Billee D.” removed_email_address@domain.invalid wrote:

in a database table if there is a field which has a certain set of

please suggest ??

vipin

But Bilee,
if we use another table and do the mapping through foreign key won;t
it be even poorer as we will be making 2 SQL queries to access the
same record. which we are doing in one SQL query in above two
methods.

But i ll certainly check the link provided.

vipin


#12

The question isn’t really a performance issue, but rather one of
customization. If users are likely to need to add values to an
enumeration, (example: category for a blog post), then a lookup table
is a good idea. If the values are substantially unlikely to change
(see your examples - marriage status and state) then there’s not a lot
to be gained by complicating the DB.

–Matt J.


#13

I would also use a small join table with your sets … so your users
can add properties on the fly.


#14

Though you might already have solved your issue at hand, I’ve recently
ran into a similar situation and written a small plugin for it,
columns are backed by integers, but in ruby you can treat them using
symbols or whatever:

class User < ActiveRecord::Base
as_enum :status, { :single => 0, :married => 1, :divorced => 2 }
end

Then create an integer column:

add_column :users, :status_cd, :integer

It’s then possible to easily access these values using @user.status:

@user = User.new
@user.status = :married # => implies @user.status_cd = 1

For my problem I also required some shorthands to check for values, so
I’ve added also ? methods:

@user.married? # => true
@user.single? # => false

The code lives at github http://github.com/lwe/simple_enum/tree/master
any feedback is appreciated

cheers, lukas