Reputation: 1950
I am creating a small database of names that has three columns: ID, Name, Status
The 'status' can be one of three things: waiting, approved, other
My question is, what is the best/most correct way to store the 'status' in the DB? Should it be stored as a varchar string or as a 1, 2, or 3 integer and later translate that to waiting/approved/other when reading from the DB? I hope that makes sense, thanks for any help.
Upvotes: 3
Views: 462
Reputation: 34054
You should have a status table (referred to as a dictionary or lookup table) using a tinyint
data type to reference that status. You would use a foreign key constraint. This way you maintain relational and domain integrity. It will allow you to add/change/remove status values without changing table structure.
status ============= id value 1 Waiting 2 Approved 3 Other
users ==================== id name status_id 1 Bobby 3
Upvotes: 6
Reputation: 600
Create another table called
statuses with 2 fields id INT Primary Auto Increment name varchar(30).
Add your statuses in the user table by integer and reference them with a JOIN.
You can also change status in the users table to status_id it would make more sense.
IMO this is the most appropriate way in
Upvotes: 1
Reputation: 21856
I think they should be stored as what they are: a enum!
Example:
status ENUM('waiting', 'approved', 'other') DEFAULT 'other'
Upvotes: 0
Reputation: 28687
An integer field uses less memory space than a varchar field, so storing your 'status' option as a number will reduce memory usage marginally.
I recommend storing 'status' in an integer and translating it to the corresponding value externally when necessary.
Upvotes: 0
Reputation: 6062
Generally speaking, store those items as text, unless:
Upvotes: 0
Reputation: 16362
Integers are smaller. Integers can be translated by your app into multiple languages, etc.
Integers good.
Check out ENUM. If not, make sure you're using the right sized integer.
Upvotes: 0