PostgreSQL/Data Type/Boolean

Материал из SQL эксперт
Перейти к: навигация, поиск

boolean value "t"

   <source lang="sql">

postgres=# postgres=# postgres=# CREATE TABLE myTable (isbn text, in_stock boolean); CREATE TABLE postgres=# postgres=# INSERT INTO myTable VALUES ("0385121679", true); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("039480001X", "t"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("044100590X", "true"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451198492", false); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0394900014", "0"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0441172717", "1"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451160916"); INSERT 0 1 postgres=# postgres=# select * from myTable;

   isbn    | in_stock

+----------
0385121679 | t
039480001X | t
044100590X | t
0451198492 | f
0394900014 | f
0441172717 | t
0451160916 |

(7 rows) postgres=# postgres=# SELECT * FROM myTable WHERE in_stock != "t";

   isbn    | in_stock

+----------
0451198492 | f
0394900014 | f

(2 rows) postgres=# postgres=# drop table myTable; DROP TABLE postgres=#

      </source>
   
  


Checking Boolean values: yes

   <source lang="sql">

postgres=# postgres=# CREATE TABLE myTable (isbn text, in_stock boolean); CREATE TABLE postgres=# postgres=# INSERT INTO myTable VALUES ("0385121679", true); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("039480001X", "t"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("044100590X", "true"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451198492", false); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0394900014", "0"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0441172717", "1"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451160916"); INSERT 0 1 postgres=# postgres=# -- Checking Boolean values postgres=# postgres=# SELECT * FROM myTable WHERE in_stock = "yes";

   isbn    | in_stock

+----------
0385121679 | t
039480001X | t
044100590X | t
0441172717 | t

(4 rows) postgres=# postgres=# postgres=# drop table myTable; DROP TABLE postgres=# postgres=#

      </source>
   
  


Checking for "false" Boolean values

   <source lang="sql">

postgres=# postgres=# postgres=# CREATE TABLE myTable (isbn text, in_stock boolean); CREATE TABLE postgres=# postgres=# INSERT INTO myTable VALUES ("0385121679", true); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("039480001X", "t"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("044100590X", "true"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451198492", false); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0394900014", "0"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0441172717", "1"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451160916"); INSERT 0 1 postgres=# postgres=# select * from myTable;

   isbn    | in_stock

+----------
0385121679 | t
039480001X | t
044100590X | t
0451198492 | f
0394900014 | f
0441172717 | t
0451160916 |

(7 rows) postgres=# postgres=# -- Checking for "false" Boolean values postgres=# postgres=# SELECT * FROM myTable WHERE in_stock = "no";

   isbn    | in_stock

+----------
0451198492 | f
0394900014 | f

(2 rows) postgres=# postgres=# postgres=# postgres=# drop table myTable; DROP TABLE postgres=# postgres=#

      </source>
   
  


Compare the value of a boolean field against any of the values

   <source lang="sql">

postgres=# postgres=# postgres=# CREATE TABLE myTable (isbn text, in_stock boolean); CREATE TABLE postgres=# postgres=# INSERT INTO myTable VALUES ("0385121679", true); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("039480001X", "t"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("044100590X", "true"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451198492", false); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0394900014", "0"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0441172717", "1"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451160916"); INSERT 0 1 postgres=# postgres=# select * from myTable;

   isbn    | in_stock

+----------
0385121679 | t
039480001X | t
044100590X | t
0451198492 | f
0394900014 | f
0441172717 | t
0451160916 |

(7 rows) postgres=# postgres=# -- Compare the value of a boolean field against any of the values postgres=# postgres=# SELECT * FROM myTable WHERE NOT in_stock;

   isbn    | in_stock

+----------
0451198492 | f
0394900014 | f

(2 rows) postgres=# postgres=# drop table myTable; DROP TABLE postgres=#

      </source>
   
  


Implying Boolean "true"

   <source lang="sql">

postgres=# postgres=# CREATE TABLE myTable (isbn text, in_stock boolean); CREATE TABLE postgres=# postgres=# INSERT INTO myTable VALUES ("0385121679", true); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("039480001X", "t"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("044100590X", "true"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451198492", false); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0394900014", "0"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0441172717", "1"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451160916"); INSERT 0 1 postgres=# postgres=# -- Implying Boolean "true" postgres=# postgres=# SELECT * FROM myTable WHERE in_stock;

   isbn    | in_stock

+----------
0385121679 | t
039480001X | t
044100590X | t
0441172717 | t

(4 rows) postgres=# postgres=# drop table myTable; DROP TABLE postgres=#

      </source>
   
  


NOT for boolean value

   <source lang="sql">

postgres=# postgres=# postgres=# CREATE TABLE myTable (isbn text, in_stock boolean); CREATE TABLE postgres=# postgres=# INSERT INTO myTable VALUES ("0385121679", true); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("039480001X", "t"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("044100590X", "true"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451198492", false); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0394900014", "0"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0441172717", "1"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451160916"); INSERT 0 1 postgres=# postgres=# select * from myTable;

   isbn    | in_stock

+----------
0385121679 | t
039480001X | t
044100590X | t
0451198492 | f
0394900014 | f
0441172717 | t
0451160916 |

(7 rows) postgres=# postgres=# postgres=# SELECT * FROM myTable WHERE NOT in_stock;

   isbn    | in_stock

+----------
0451198492 | f
0394900014 | f

(2 rows) postgres=# postgres=# postgres=# postgres=# drop table myTable; DROP TABLE

      </source>
   
  


NULL will not register as either true or false

   <source lang="sql">

postgres=# postgres=# postgres=# CREATE TABLE myTable (isbn text, in_stock boolean); CREATE TABLE postgres=# postgres=# INSERT INTO myTable VALUES ("0385121679", true); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("039480001X", "t"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("044100590X", "true"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451198492", false); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0394900014", "0"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0441172717", "1"); INSERT 0 1 postgres=# INSERT INTO myTable VALUES ("0451160916"); INSERT 0 1 postgres=# postgres=# select * from myTable;

   isbn    | in_stock

+----------
0385121679 | t
039480001X | t
044100590X | t
0451198492 | f
0394900014 | f
0441172717 | t
0451160916 |

(7 rows) postgres=# postgres=# -- NULL will not register as either true or false postgres=# postgres=# SELECT * FROM myTable WHERE in_stock IS NULL;

   isbn    | in_stock

+----------
0451160916 |

(1 row) postgres=# postgres=# postgres=# drop table myTable; DROP TABLE postgres=#

      </source>
   
  


The difference between true and "true"

   <source lang="sql">

postgres=# postgres=# -- The difference between true and "true" postgres=# postgres=# SELECT true AS boolean_t, postgres-# "true" AS string_t, postgres-# false AS boolean_f, postgres-# "false" AS string_f;

boolean_t | string_t | boolean_f | string_f

+----------+-----------+----------
t         | true     | f         | false

(1 row) postgres=#

      </source>
   
  


Using the boolean type

   <source lang="sql">

postgres=# postgres=# postgres=# -- Using the boolean type postgres=# postgres=# CREATE TABLE test1 (a boolean, b text); CREATE TABLE postgres=# INSERT INTO test1 VALUES (TRUE, "sic est"); INSERT 0 1 postgres=# INSERT INTO test1 VALUES (FALSE, "non est"); INSERT 0 1 postgres=# postgres=# SELECT * FROM test1;

a |    b

---+---------

t | sic est
f | non est

(2 rows) postgres=# postgres=# SELECT * FROM test1 WHERE a;

a |    b

---+---------

t | sic est

(1 row) postgres=# postgres=# drop table test1; DROP TABLE postgres=#

      </source>