CodeIgniter Forums
'Select where string/integer = something': does it matter if it's a string or int? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: 'Select where string/integer = something': does it matter if it's a string or int? (/showthread.php?tid=8899)



'Select where string/integer = something': does it matter if it's a string or int? - El Forum - 06-04-2008

[eluser]codex[/eluser]
I tend to use integers to store stuff like options in the DB. For example:

$brand[0] = 'Chevrolet';
$brand[1] = 'Buick';
$brand[2] = 'Ford';

To store a carbrand I may store '2', which corresponds with 'Ford' etc. And to echo the brandname I do $brand[$result_from_db].

Pretty straight forward stuff and it works fine, but when looking at raw data in the database it doesn't make any sense, you only see integers and you don't know what they mean, unless you have the conversion table next to you. It would make far more sense if the data was stored as a string, but does it matter how you store it performance wise?


'Select where string/integer = something': does it matter if it's a string or int? - El Forum - 06-04-2008

[eluser]gtech[/eluser]
let me get this right..

are you currently only storing the integers in the database and then converting them into strings in the code?

if so then it is perfectly acceptable to store data as strings thats what databases are designed to do, say in the brands table you might want to store name, market share or any other kind of data.
Code:
table brands:
--------------
id  name        share  
==  =====       =====
0   Chevrolet   20%
1   Buick       20%
2   Ford        40%
there is no performance loss storing the strings in the database.

the point of having an id is so other tables can relate to the brands table
eg
Code:
table models:
--------------
id  name     brandid
==  =====    =======
0   Cortina  2
1   Ka       2
2   Escort   2
3   Camaro   0

the brandid tells me that the Camaro is a chevrolet, so if I want to select company information related to the car model I can easily.

It is more efficient to search using ids and that is why it is useful for every database table to contain a unique id for every row.

another advantage is that say chevrolet and ford built an escort your table could look like this
Code:
id  name     brandid
==  =====    =======
0   Cortina  2
1   Ka       2
2   Escort   2
3   Escort   0
note that each model still has a unique ID, but the brandid shows that the models belong to different companies.

does that make sense, or have I misunderstood your question?

you may want to research one to many, and many to many relationships.