oracle加入not null約束

來源:互聯網
上載者:User

標籤:article   ons   tab   ora   建立   oracle   create   style   size   

在建立表時。為列加入not null約束,形式例如以下:
column_name data_type
[constraint constraint_name] not null
當中,constraint constraint_name 表示為約束指定名稱。
也能夠為已建立的表中加入not null約束,這時就須要使用alter table... modify語句。形式例如以下:
alter table table_name modify column_name [constraint constraint_name] not null;


刪除not null約束
假設須要刪除表中的裂傷的not null約束,依舊是使用alter table...modify語句,形式例如以下:
alter table table_name modify column_name null;


詳細的操作例如以下:
SQL> create table person(
  2  pid number(4) not null,
  3  pname varchar2(20),
  4  psex char(2)
  5  );
表已建立。


SQL> desc person;
 名稱                                      是否為空白? 類型
 ----------------------------------------- -------- -------------------
 PID                                       NOT NULL NUMBER(4)
 PNAME                                              VARCHAR2(20)
 PSEX                                               CHAR(2)
SQL> alter table person modify pname not null;
表已更改。


SQL> desc person;
 名稱                                      是否為空白?

類型
 ----------------------------------------- -------- ------------------
 PID                                       NOT NULL NUMBER(4)
 PNAME                                     NOT NULL VARCHAR2(20)
 PSEX                                               CHAR(2)
SQL> insert into person values(1,‘aaa‘,‘女‘);
已建立 1 行。
SQL> insert into person values(1,‘aaa‘,null);
已建立 1 行。


SQL> insert into person values(1,null,null);
insert into person values(1,null,null)                           *
第 1 行出現錯誤:
ORA-01400: 無法將 NULL 插入 ("SYSTEM"."PERSON"."PNAME")
SQL> alter table person modify pname null;
表已更改。


SQL> desc person;
 名稱                                      是否為空白? 類型
 ----------------------------------------- -------- ------------------
 PID                                       NOT NULL NUMBER(4)
 PNAME                                              VARCHAR2(20)
 PSEX                                               CHAR(2)
SQL> insert into person values(1,null,null);
已建立 1 行。
SQL>

oracle加入not null約束

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.