[已解決]數據庫建表時,check存在的bug

在數據庫建表的時候,如果我們設置了約束檢查,如下所示

create table aa
  (
    trancode char(4) not null ,
    fieldname char(10) not null ,
    iotype char(1) 
        default 'I',
    sequence smallint not null ,    
    check (iotype IN ('I' ,'O' )),
  );

check是爲了是iotype只能在I和O中

但是如果插入語句爲

insert into pjyjk(trancode,fieldname,iotype,sequence) values("9899","O9899",null,"100")

那麼可以正常插入,約束並沒有檢查到插入的iotype爲null,這樣可能會引起後續的錯誤

所以,解決方法是,遇到這樣的表,需要對字段約束時,再設置爲not null即可

iotype char(1) 
        default 'I',not null

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章