Flags in a database rows, best practices

前端 未结 8 1575
南旧
南旧 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:18

    I would recommend using a BOOLEAN datatype if your database supports this.

    Otherwise, the best approach is to use NUMBER(1) or equivalent, and put a check constraint on the column that limits valid values to (0,1) and perhaps NULL if you need that. If there is no built-in type, using a number is less ambiguous that using a character column. (What's the value for true? "T" or "Y" or "t")

    The nice thing about this is that you can use SUM() to count the number of TRUE rows.

    SELECT COUNT(1), SUM(ActiveFlag)
    FROM myusers;
    

提交回复
热议问题