JK87
JK87

Reputation: 379

Store data as varchar, or as tinyint and use switch

Probably a very simple question, but I'm not sure how to test it myself..

Imagine I have a mysql table with let's say, 30000 rows. Each row has a column car_brand. In my phpform I have a select with by example, the following car brands: Mercedes, Audi, Chevrolet, Volkswagen, Opel.

What is the fastest (less memory using) way to show the car brand to the user?

Use tinyint and say 0 = Mercedes, 1 = Audi, 2 = Chevrolet... And use a CASE in the SELECT statement or use a php switch() before showing it in a HTML table OR is it better to save the data as a varchar?

Hope someone have a clear answer ;)

Upvotes: 0

Views: 67

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269693

The correct way is to use a separate reference table for the brands:

create table CarBrands (
    CarBrandId int not null primary key auto_increment,
    BrandName varchar(255) not null
);

Then refer to the car brand in other tables using CarBrandId and use a join to get the brand name. This is the right way to handle a reference table.

If you are really opposed to a reference table, then you can use enum.

Upvotes: 1

Related Questions