Oracle 10g中約束與列屬性NULLABLE的關係

來源:互聯網
上載者:User

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

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.