SQL非空而不是非空

时间:2022-04-11 15:26:16

I am using postgreSQL. I have a column that:

我正在使用postgreSQL。我有一个专栏:

NOT NULL

However when I want to insert a row with an empty string as like:

但是当我想插入一个空字符串的行时:

''

it doesn't give me an error and accepts. How can I check insert value should be not empty? (Neither empty nor null)

它不会给我一个错误并接受。如何检查插入值应该不为空? (既不是空的也不是空的)

PS: My column defined as:

PS:我的专栏定义为:

"ads" character varying(60) NOT NULL

2 个解决方案

#1


16  

Add a constraint to column definition. For example something like:

向列定义添加约束。例如:

ads character varying(60) NOT NULL CHECK (ads <> '')

For more, see http://www.postgresql.org/docs/current/static/ddl-constraints.html

有关更多信息,请参阅http://www.postgresql.org/docs/current/static/ddl-constraints.html

#2


7  

Found in the current documentation of postgreSQL you can do the following to achieve what you want:

在postgreSQL的当前文档中找到,您可以执行以下操作来实现您想要的:

CREATE TABLE distributors (
    did    integer PRIMARY KEY DEFAULT nextval('serial'),
    name   varchar(40) NOT NULL CHECK (name <> '')
);

From the documentation:

从文档:

CHECK ( expression )

检查(表达式)

The CHECK clause specifies an expression producing a Boolean result which new or updated rows must satisfy for an insert or update operation to succeed. Expressions evaluating to TRUE or UNKNOWN succeed. Should any row of an insert or update operation produce a FALSE result an error exception is raised and the insert or update does not alter the database. A check constraint specified as a column constraint should reference that column's value only, while an expression appearing in a table constraint may reference multiple columns.

CHECK子句指定一个表达式,该表达式产生一个布尔结果,新的或更新的行必须满足这些结果才能使插入或更新操作成功。表达式评估为TRUE或UNKNOWN成功。如果插入或更新操作的任何行产生FALSE结果,则会引发错误异常,并且插入或更新不会更改数据库。指定为列约束的检查约束应仅引用该列的值,而出现在表约束中的表达式可引用多个列。

Currently, CHECK expressions cannot contain subqueries nor refer to variables other than columns of the current row.

目前,CHECK表达式不能包含子查询,也不能引用当前行的列以外的变量。

#1


16  

Add a constraint to column definition. For example something like:

向列定义添加约束。例如:

ads character varying(60) NOT NULL CHECK (ads <> '')

For more, see http://www.postgresql.org/docs/current/static/ddl-constraints.html

有关更多信息,请参阅http://www.postgresql.org/docs/current/static/ddl-constraints.html

#2


7  

Found in the current documentation of postgreSQL you can do the following to achieve what you want:

在postgreSQL的当前文档中找到,您可以执行以下操作来实现您想要的:

CREATE TABLE distributors (
    did    integer PRIMARY KEY DEFAULT nextval('serial'),
    name   varchar(40) NOT NULL CHECK (name <> '')
);

From the documentation:

从文档:

CHECK ( expression )

检查(表达式)

The CHECK clause specifies an expression producing a Boolean result which new or updated rows must satisfy for an insert or update operation to succeed. Expressions evaluating to TRUE or UNKNOWN succeed. Should any row of an insert or update operation produce a FALSE result an error exception is raised and the insert or update does not alter the database. A check constraint specified as a column constraint should reference that column's value only, while an expression appearing in a table constraint may reference multiple columns.

CHECK子句指定一个表达式,该表达式产生一个布尔结果,新的或更新的行必须满足这些结果才能使插入或更新操作成功。表达式评估为TRUE或UNKNOWN成功。如果插入或更新操作的任何行产生FALSE结果,则会引发错误异常,并且插入或更新不会更改数据库。指定为列约束的检查约束应仅引用该列的值,而出现在表约束中的表达式可引用多个列。

Currently, CHECK expressions cannot contain subqueries nor refer to variables other than columns of the current row.

目前,CHECK表达式不能包含子查询,也不能引用当前行的列以外的变量。