How to create a Boolean column on Oracle?

Asked

Viewed 10,668 times

2

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

CHAR(1) (Y/N) ?
INTEGER (0/1) ?

4 answers

4

NUMBER (0/1) is the default.

Frames like Entityframework, Nhibernate, Dapper, etc., identify by default when running a command in the Oracle database, that 0 = false and 1 = true.

3

Good afternoon, Deramon.

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

However, some argue that one can use CHAR(1) or even NUMBER(1), and still create a Constraint, allowing only 0 and 1 input 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’ve helped.

  • 1

    I hadn’t read about the RAW. I also did some research before, and so I asked the question to see if anyone has ever had this kind of situation, and if they have ever had a problem using char or number. Until then, CHAR(1) has been the most indicated. I’ll wait to see if anyone else can add anything else. Thanks for the tip.

2


To create follows

create table tbool (bool char check (bool in (0,1));
    insert into tbool values(0);
    insert into tbool values(1);`
  • perfect! very good and thank you

1

To create a boolean field on Oracle, you can create it this way:

Create a field to receive the boolean value:

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

When entering a record, by default, if not informed, it will be filled as 1.

Now, to ensure that it only has values like 1 or 0, add the Constraint:

ALTER TABLE table_name ADD
CONSTRAINT name_constraint 
column_name_check (ONOFF in ( '1', '0' ));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.