Flags in a database rows, best practices

前端 未结 8 1585
南旧
南旧 2020-12-16 09:34

I am asking this out of a curiosity. Basically my question is when you have a database which needs a row entry to have things which act like flags, what is the best practice

8条回答
  •  难免孤独
    2020-12-16 10:08

    If the flags have very different meanings and are used directly in SQL queries or VIEWS, then using multiple columns of type BOOLEAN might be a good idea.

    Put each flag into an extra column, because you'll read and modify them separately anyway. If you want to group the flags, just give their column names a common prefix, i.e. instead of:

    CREATE TABLE ... (
        warnings INTEGER,
        errors   INTEGER,
        ...
    )
    

    you should use:

    CREATE TABLE ... (
        warning_foo BOOLEAN,
        warning_bar BOOLEAN,
        warning_...
        error_foo   BOOLEAN,
        error_bar   BOOLEAN,
        error_...   BOOLEAN,
        ...
    )
    

    Although MySQL doesn't have a BOOLEAN type, you can use the quasi standard TINYINT(1) for that purpose, and set it only to 0 or 1.

提交回复
热议问题