Home » RDBMS Server » Server Administration » Implementation of boolean types.
icon4.gif  Implementation of boolean types. [message #127748] Wed, 13 July 2005 02:28 Go to next message
Achchan
Messages: 86
Registered: June 2005
Member
Hi all,
I have seen many databases that tables with columns such as IsMale,IsFemale,IsNull,Is...
All these were somekind of boolean type with yes/no or true/false or 1/0 values.In DBMSs that supports boolean or bit data type it can be implemented naturaly but in Oracle that does not have boolean or bit,people usually use number(1) or char(1) for this purpose.
My question: Is this design OK in your opinion? I personally think that this should be design using a relationship with other tables such as HumanSex or...for scalability reasons and so on (Not in fe/male situation! )
Any other ideas are highly appreciated.
-Thanks in advance
Re: Implementation of boolean types. [message #127838 is a reply to message #127748] Wed, 13 July 2005 11:18 Go to previous messageGo to next message
smartin
Messages: 1803
Registered: March 2005
Location: Jacksonville, Florida
Senior Member
Can you expand a bit more on what you mean by "is that ok?"

Do you have several situations in mind? Do you mean is it ok for a column to only have 2 possible values?

Wouldn't it all depend on the situation at hand?

Hmm, just re-read your question. Do you mean specifically the isSomething column situation?

I can see that being used in some sort of reporting or DSS situation. Maybe not with male/female but perhaps with something else where all you need to display is whether or not something is true. Perhaps like IsErrorCode45. Actually in that case I could see being used in oltp.

But in general I think in oltp you are storing the value of something, and your column should allow for all the possible values, rather than having a separate column for each value.

So in case of sex you would have a sex column with 1,2,3 for male, female, unknow, or m,f for male, female. Etc. You would not have 2 or 3 separate columns to hold a single piece of info, instead you would extract that sort of thing out in the display (which is why I could see it used in some sort of summary table).

But in other columns, perhaps yes/no is truly what the value is, like a table with a bunch of columns each column indicating error codes that are not mutually exclusive.

Design questions are very interesting to think about and discuss. Many times there is no one right way to go about it.
Re: Implementation of boolean types. [message #127866 is a reply to message #127748] Wed, 13 July 2005 15:13 Go to previous messageGo to next message
Frank Naude
Messages: 4587
Registered: April 1998
Senior Member
Just to add my 2c:

It is perfectly fine to use number(1) or char(1) to simulate boolean behaviour. Also, IsMale, IsFemale and IsNull are good names for functions, not columns.

Here is a possible implementation:

SQL> CREATE TABLE emp2 (
  2     id   NUMBER NOT NULL PRIMARY KEY,
  3     name VARCHAR2(30) NOT NULL,
  4     sex  CHAR(1) CHECK (sex in ('M','F'))
  5  )
  6  /

Table created.

SQL>
SQL> INSERT INTO emp2 VALUES (1, 'John', 'M');

1 row created.

SQL> INSERT INTO emp2 VALUES (2, 'Nancy', 'F');

1 row created.

SQL>
SQL> CREATE OR REPLACE FUNCTION isMale(p_id NUMBER) RETURN BOOLEAN AS
  2    v_sex emp2.sex%TYPE;
  3  BEGIN
  4     SELECT sex INTO v_sex FROM emp2 WHERE id = p_id;
  5     IF v_sex = 'M' THEN
  6        RETURN true;
  7     ELSE
  8        RETURN false;
  9     END IF;
 10  END;
 11  /

Function created.


Best regards.

Frank
icon14.gif  Re: Implementation of boolean types. [message #128281 is a reply to message #127866] Sat, 16 July 2005 23:45 Go to previous message
Achchan
Messages: 86
Registered: June 2005
Member
Thanks Martin and Frank.You ideas were very brightening.
Previous Topic: global_name parameter
Next Topic: specifying destination location for archive log files
Goto Forum:
  


Current Time: Fri Jan 10 08:25:14 CST 2025