boolean type db code example

Example 1: boolean in sql

#Oracle
#The recommended way of storing booleans in Oracle SQL is to use a NUMBER(1) field.
#This can store 1 as true and 0 as false.

CREATE TABLE testbool (
  sometext VARCHAR2(10),
  is_checked NUMBER(1)
);


#SQL Server
#A BIT data type is used to store bit values from 1 to 64.
#1 for TRUE and 0 for FALSE.

CREATE TABLE testbool (
  sometext VARCHAR(10),
  is_checked BIT
);


#MySQL
#A BIT data type is used to store bit values from 1 to 64.
#1 for TRUE and 0 for FALSE.

CREATE TABLE testbool (
  sometext VARCHAR(10),
  is_checked BIT(1)
);


#PostgreSQL
#PostgreSQL has a Boolean Data Type
#TRUE: represented by TRUE, ‘t’, ‘true’, ‘y’, ‘yes’, ‘on’, ‘1’
#FALSE: represented by FALSE, ‘f’, ‘false’, ‘n’, ‘no’, ‘off’, ‘0’

CREATE TABLE testbool (
  sometext TEXT,
  is_checked BOOLEAN
);

Example 2: boolean mysql

-- MySQL doesn't have a boolean. Instead, use TINYINT. BOOL and BOOLEAN
-- are aliases to TINYINT. 0 is true, while 1 is false for TINYINT.
-- You can also use TRUE, FALSE, true, false, True, or false as
-- aliases for 0 and 1 respectively.

CREATE TABLE IF NOT EXISTS boolTest (
  thisFieldIsTrue TINYINT DEFAULT 0 -- True by default
  thisFieldIsFalse TINYINT DEFAULT 1 -- False by default
  thisIsAlsoTru BOOL DEFAULT True -- True by default, see above for alternates to True and Bool
 )

Tags:

Sql Example