How to create a Boolean column in Oracle?

2

As far as I know, Oracle does not provide Boolean valueType . I am working briefly with Oracle and would like to know the best solution to "simulate" a true / false value in the Oracle database?

CHAR(1) (Y/N) ?
INTEGER (0/1) ?
    
asked by anonymous 29.02.2016 / 14:11

4 answers

2

To create follows

create table tbool (bool char check (bool in (0,1));
    insert into tbool values(0);
    insert into tbool values(1);'
    
29.02.2016 / 14:13
3

Good afternoon, DeRamon.

I searched here and saw that there are people who also use RAW (1), it seems to be good practice.

However, some argue that you can use CHAR (1) or even NUMBER (1), and you can still create a Constraint, allowing input of only 0 and 1 in this numeric field.

I would opt for CHAR (1), I imagine it will make your database cleaner and easier to understand.

I hope I have helped.

    
29.02.2016 / 14:21
2

NUMBER (0/1) is the default.

Frames such as EntityFramework, NHibernate, Dapper and etc, identify by default when executing a command in Oracle database, that 0 = false and 1 = true.

    
06.03.2016 / 19:22
0

To create a Boolean field in Oracle, you can create this:

Create a field to receive the Boolean value:

ALTER TABLE table_name
ADD column_name_check char(1) DEFAULT '1';

When you enter a record, by default, if it is not entered, it will be filled as 1.

Now, to ensure that it has values only as 1 or 0, add the constraint:

ALTER TABLE table_name ADD
CONSTRAINT name_constraint 
column_name_check (ONOFF in ( '1', '0' ));
    
03.12.2018 / 14:57