cooper
cooper

Reputation: 674

how to store constants in mysql

I need to store some constants in mysql in order to avoid doing subqueries or queries very complicated.

I wonder which would be the best way to do it.

-----------------------------------
ID      name                value
-----------------------------------
1      constant_name_1        100
2      constant_name_2          0  
3      constant_name_3         12
4      constant_name_4        257
-----------------------------------

I'm not sure if the example written above would be the best way because I've some doubts about it.

1) I don't need the ID field at all. I just need the constant_name, so I don't know if it's neccessary. 2) In most cases, I'll need to know the values of several constants at once, not just one. In this cases I'm not sure how should I do the queries.

"SELECT value FROM constants_table WHERE name IN ('constant_name1','constant_name2','constant_name3',...)" ??

Thanks so much

Upvotes: 1

Views: 1107

Answers (1)

Ed Gibbs
Ed Gibbs

Reputation: 26343

Your approach is reasonable. As long as the name will be unique I'd drop the ID column (there's no need for it) and make the name column your PK.

As for querying, my preference would be to pick the values one at a time, as needed, using a subquery. It's the "prettiest" way I can think of offhand, and it's optimizable:

SELECT myTable.ID, myTable.Name
FROM myTable
INNER JOIN (SELECT value FROM constants_table WHERE name = 'constant_name_1') C1
   ON myTable.ThisValue = C1.value
INNER JOIN (SELECT value FROM constants_table WHERE name = 'constant_name_4') C4
   ON myTable.ThatValue = C4.value

Upvotes: 2

Related Questions