Oracle's lack of a Bit datatype for table columns

后端 未结 9 997
醉酒成梦
醉酒成梦 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 03:58

    I prefer char(1) over number(1), since with some reasonable choice of characters, it is obvious which character has which boolean meaning.

    Of course you should fight all the different varations, choose one and ensure it's use by putting check constraints on the columns.

    Although it probably is to late in your case, generating the schema from another tool often takes care at least of the consistency issue. I personally prefer hibernate for this purpose, but that is very situation specific.

    And of course that is a glaring obmission. To make it worse, PL/SQL has a boolean, but you can't use it in SQL statements.

    0 讨论(0)
  • 2021-02-05 03:58

    Use a CHAR(1), and a constraint to allow only 1 and 0.

    ...

    col CHAR(1),
    CONSTRAINT cons_atable_col1 CHECK (col1 IN ('1','0'))
    
    0 讨论(0)
  • 2021-02-05 04:02

    Oracle internally uses "bits" (not a datatype per se) in different Data Dictionary views.

    For example, dba_users view has :

    ..
            , DECODE (BITAND (u.spare1, 128), 128, 'YES', 'NO')
    ..
            , DECODE (BITAND (u.spare1, 256), 256, 'Y', 'N')
    ..
    

    which shows a way to workaround this in a way. If you don't have to modify "boolean" bits often, you could employ the same approach that Oracle had since Oracle 6 (at least). Create a table with a NUMBER column, and a VIEW on top of that that hides complexity of BITAND operations.

    ps. On a side note, Oracle JDBC has a "Bit" datatype https://docs.oracle.com/cd/E16338_01/appdev.112/e13995/oracle/jdbc/OracleTypes.html#BIT as well as you already know PL/SQL has Boolean. Although it probably doesn't help you much. See BITAND approach above if it suites your case.

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

    According to this Oracle guide - you should use NUMBER(3). Crazy, but true.

    http://docs.oracle.com/cd/B19306_01/gateways.102/b14270/apa.htm

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

    Here is an Ask Tom discussion on the topic. Gives an Oracle-centric view on the issue.

    As for storage, char(1) is actually a bit (no pun intended) more efficient:

    SQL> CREATE TABLE xx (c CHAR(1), n NUMBER);
    
    Table created
    
    SQL> insert into xx values('T', 1);
    
    1 row inserted
    
    SQL> select dump(c), dump(n) from xx;
    
    DUMP(C)             DUMP(N)
    ------------------- -------------
    Typ=96 Len=1: 84    Typ=2 Len=2: 193,2
    
    0 讨论(0)
  • 2021-02-05 04:13

    https://docs.oracle.com/cd/E17952_01/refman-5.5-en/char.html

    As DCookie said, char(1) is more efficient. Because VARCHAR2(VARCHAR) empty contains 1 byte, but when we store 1 character then empty 1 byte size + with character 1 byte size --> 2 byte need to store 1 character in varchar

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