Oracle 10g中約束與列屬性NULLABLE的關係
結論:
columname type not null與check (columnname is not null)的結果是不一樣的
因此:
1、不需要手工去匹配NULLABLE屬性,當所有顯式導致NULLABLE由Y變N的約束被刪除後,NULLABLE自然會恢複為Y。
2、盡量不要使用CHECK來實現NOT NULL,可以使用MODIFY或直接在欄位後聲明
drop table zwxtest04;
create table zwxtest04
(
id integer
);
alter table zwxtest04 add constraint zwxtest04c2 check (id is not null);
select * from user_tab_columns where table_name='ZWXTEST04';
select * from user_constraints where table_name='ZWXTEST04';
--NULLABLE 為Y ,約束並不會導致NULLABLE變動
drop table zwxtest04;
create table zwxtest04
(
id integer not null
);
select * from user_tab_columns where table_name='ZWXTEST04';
select * from user_constraints where table_name='ZWXTEST04';
-- NULLABLE 為N ,同時自動添加一個C型的NOT NULL的約束
drop table zwxtest04;
create table zwxtest04
(
id integer
);
alter table zwxtest04 id not nul;
select * from user_tab_columns where table_name='ZWXTEST04';
select * from user_constraints where table_name='ZWXTEST04';
-- NULLABLE 為N ,同時自動添加一個C型的NOT NULL的約束
drop table zwxtest04;
create table zwxtest04
(
id integer
);
alter table zwxtest04 add constraint zwxtest04c3 primary key (id );
select * from user_tab_columns where table_name='ZWXTEST04';
select * from user_constraints where table_name='ZWXTEST04';
-- NULLABLE 為N ,建立P型約束,建立UNIQUE索引
alter table zwxtest04 drop constraint zwxtest04c3 ;
--NULLABLE為Y