Oracle's lack of a Bit datatype for table columns

后端 未结 9 999
醉酒成梦
醉酒成梦 2021-02-05 03:13

Oracle does not support a bit datatype or any other type for true/false scenarios. Does one use the char(1) field instead by using a specific letter to

相关标签:
9条回答
  • 2021-02-05 04:16

    I'm not an English native so I tend to use either 1 and 0 or '1' and '0'. Using 'Y' and 'N' make little sense if you aren't coding in English (yes, native language coding does exist). Using 'SI' and 'NO' or 'S' and 'N' doesn't look professional (just like naming variables with accented letters). Ones and zeroes, on the contrary, are pretty standard if you've coded in C, PHP or JavaScript. In any case, I always add the appropriate constraint to disallow any other character. Apart from subjective issues, I don't think there're noticeable performance gain in choosing CHAR or NUMBER. I like numbers a little more because I don't need to quote them :)

    I agree it's a glaring omission but I've read seriously heated discussions on the subject in some Oracle forums; it's a kind of religious issue. Some claim that booleans belong to application data types and have no place in the database core. Honestly, I believe it's one of those We Have Been So Long Without It That We Had Better Say It Was On Purpose things.

    By the way, MySQL has a BOOLEAN type but it's a synonym for TINYINT(1) so it eventually equals to 1 and 0; which is fine, because it also has the constants TRUE and FALSE that evaluate to 1 and 0.

    0 讨论(0)
  • 2021-02-05 04:16

    Number(1) is no better than char(1). Especially if it will be in addition to the existing char(1). That will just add to the confusion.

    FWIW, Oracle in internal views (such as USER_TAB_COLUMNS) uses varchar2(3) (YES and NO). Not sure if they are 100% consistent here, though.

    0 讨论(0)
  • 2021-02-05 04:18

    The question is old but until the latest release of oracle is used it is still a valid question.

    I would solve the problem this way: Create a table which holds the possible values for true/false plus localized display text f.e. T$KEYWORDS ITEMNO ITEMTEXT ITEMTEXT_DE ITEMTEXT_FE ... 0 False Falsch 1 True Wahr

    Instead of True/False this could be also Selected, Not Selected, etc.

    And then add a foreigh key to your column to this table. That way you have only valid values and they do not change with localization.

    ANother good solution imho is using a check constraint on your data column. This ofc does not work if your values could be different in the same database/column depending on localization of clients.

    alter table tblLocations add flag number CONSTRAINT <constraintname> CHECK (flag IN (1,0));

    0 讨论(0)
提交回复
热议问题