开发者

Can I insert an empty string in a NOT NULL field?

Can I insert an empty string in a not null field?

insert into xyz(A,B) values(1,''); 开发者_运维知识库 // is this possible if B is NOT NULL?


Depends on DBMS.

Oracle no: '' and null are identical

SQL-Server yes: '' and null are different values.


Yes you can... The concept of the NULL value is a common source of confusion for newcomers to SQL, who often think that NULL is the same as an empty string '', or a value of zero.

This is not the case. Conceptually, NULL means "a missing unknown value" and it is treated somewhat differently from other values. For example, to test for NULL, you cannot use the arithmetic comparison operators such as =, <, or <> in most DBMSes.


As Daniel said, yes you can insert the zero-length string into a NOT NULL field (except on Oracle). However, if you want to rule out zero-length strings as well, you can add a constraint:

ALTER TABLE xyz ADD CONSTRAINT CHECK (b LIKE '_%');

Most modern databases have a regular-expression operator or function you could use for constraints as well, but the exact syntax varies from database to database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜