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

后端 未结 3 2321
盖世英雄少女心
盖世英雄少女心 2021-02-19 16:03

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?
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-19 17:07

    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.

提交回复
热议问题