It is necessary to implement a logging, messages are planned to store in DB in a "log" table. Among other fields each message will have a "status" field: 0 - successful, 1 - wrong data, 2 - wrong user, 3 - whatever, 4 - etc...
The straightforward idea is to store the "Status" field as "int" column in the same table... To push data in table special enumeration will be created, something like this (let's use C#.NET for example, but any language will work too):
enum LogStatusEnum
{
Successful=0,
WrongData=1,
WrongUser=2,
}
void main()
{
LogStatusEnum status = LogStatusEnum.Successful;
int statusValue = (int)status;
string query = "INSERT INTO log (Status, ...) VALUES ("+statusValue+",...)";
}
There is also another approach: to create additional table, something like "log_status" with fields "StatusId" (int, autoincrement), "StatusCode" (varchar), "StatusDescription" (varchar) that will contain a separate record for each status field (with a foreign key applied to both tables). In this case before adding data into "log" table ID for the required "code" should be fetched in advance with query:
query = "SELECT Id FROM LogStatus WHERE StatusCode='"+GetStatusCode(status)+"'";
and this (received) ID will be used to populate "log" table.
In both cases you need to keep in sync both DB side and application side. But from my perspective, the 2nd approach is a little bit better:
- more safe: you need to be sure that your "status" is really present in DB before adding data, you will have a constrain (wrong status won't be added).
- more data-driven (it is hard to say for me why it is better, but I fill that).
In order to get these benefits you need to pay: perform a request to DB to get status ID by status code.
Do you think it is reasonable to implement the 2nd approach? Or the 1st will fit too? Do you see any other pros of the 2nd approach or cons of the 1st one?
Any thoughts are welcome.
Thank you in advance.