[英]How to insert a boolean value to a table in PostgreSQL?
I come from MySQL to PostgreSQL then, I created test
table with BOOLEAN state
column in PostgreSQL as shown below:我从MySQL到PostgreSQL然后,我在 PostgreSQL 中创建了带有 BOOLEAN state
state
的test
表,如下所示:
CREATE TABLE test (
state BOOLEAN -- Here
);
But, I couldn't insert TRUE
with 1
and FALSE
with 0
to test
table as shown below even though the SQL queries below work in MySQL :但是,即使下面的 SQL 查询在MySQL中工作,我也无法插入带有
1
的TRUE
和带有0
的FALSE
来test
表,如下所示:
INSERT INTO test VALUES (1);
INSERT INTO test VALUES (0);
Then, I got the error below:然后,我收到以下错误:
ERROR: column "state" is of type boolean but expression is of type integer
So, how to insert a boolean value to a table?那么,如何向表中插入一个 boolean 值呢?
You can insert TRUE
with '1'
and FALSE
with '0'
in PostgreSQL as shown below:您可以在PostgreSQL中插入带有
'1'
TRUE
和带有'0'
的FALSE
,如下所示:
INSERT INTO test VALUES ('1');
INSERT INTO test VALUES ('0');
Then, t
which is TRUE
and f
which is FALSE
are inserted to test
table as shown below:然后,
TRUE
的t
和为FALSE
的f
插入到test
表中,如下所示:
postgres=# SELECT * FROM test;
state
-------
t
f
(2 rows)
In addtion, these SQL queries below also work to insert TRUE
and FALSE
to test
table as shown below:此外,下面的这些 SQL 查询也可以将
TRUE
和FALSE
插入到test
表中,如下所示:
TRUE
: TRUE
:INSERT INTO test VALUES (tRuE);
INSERT INTO test VALUES ('TrUe');
INSERT INTO test VALUES ('T');
INSERT INTO test VALUES ('t');
INSERT INTO test VALUES ('YeS');
INSERT INTO test VALUES ('Y');
INSERT INTO test VALUES ('y');
INSERT INTO test VALUES ('oN');
FALSE
: FALSE
:INSERT INTO test VALUES (fAlSe);
INSERT INTO test VALUES ('FaLsE');
INSERT INTO test VALUES ('F');
INSERT INTO test VALUES ('f');
INSERT INTO test VALUES ('No');
INSERT INTO test VALUES ('N');
INSERT INTO test VALUES ('n');
INSERT INTO test VALUES ('oFf');
The standard way to insert boolean values in PostgreSQL is to use the literal boolean values true
or false
or any expression that evaluates to a boolean.在 PostgreSQL 中插入 boolean 值的标准方法是使用文字 boolean 值
true
或false
或任何计算结果为 boolean 的表达式。
For example:例如:
create table test (
state boolean
);
insert into test (state) values (true);
insert into test (state) values (false);
insert into test (state) values (3 * 5 > 10);
select * from test;
Returns:退货:
state
-----
t
f
t
See running example in db<>fiddle .请参阅db<>fiddle中的运行示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.