HiveBrain v1.2.0
Get Started
← Back to all entries
principleModerate

What is the best practice when creating boolean columns in Oracle?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
thewhatbooleanpracticecreatingcolumnswhenoraclebest

Problem

I faced again the serious problem, that Oracle simply doesn't have a boolean column type. I need to use char(1), or smallint, or some other.

What is the best practice, how to emulate boolean values in Oracle?

(Space consumption isn't important now - but a good cooperation with java/hibernate line were important).

Solution

I usually use a number(1) type combined with a check constraint:

some_flag number(1) not null check (some_flag in (1,0))


To make things crystal clear I also add a comment to that table:

comment on column some_table.some_flag is '0 is false, 1 is true';


so that the explanation on what "true" means can be seen when looking at the definition of the table.

I would avoid a character representation due to localization problems. But if you do, make sure you create the approriate check constraint. Because it's not clear if a char(1) would use T, Y, y, t (or even W or J which I have seen in German).

Code Snippets

some_flag number(1) not null check (some_flag in (1,0))
comment on column some_table.some_flag is '0 is false, 1 is true';

Context

StackExchange Database Administrators Q#76715, answer score: 10

Revisions (0)

No revisions yet.