簡體   English   中英

將值更改為非空后無效的 ALTER TABLE 選項

[英]invalid ALTER TABLE option after changing a value to not null

create table agenda
(
    id         number(3),
    first_name varchar(20),
    last_name  varchar2(20),
    phone_nr   char(14)
);

alter table agenda 
    alter column phone_nr char(14) not null;

在我嘗試將phone_nr變量更改phone_nr空后,出現錯誤

無效的 ALTER TABLE 選項

我是 SQL 的初學者,我真的不明白問題出在哪里。

AFAIK,您不能使用alter column子句來更改列的可為空性,但您可以使用modify column子句:

alter table agenda modify column phone_nr char(14) not null;
-- Here -----------^

您是否確保所有phone_nr值都設置為不為空的值? 你只能alter它是not null ,如果所有的值都是一些不為空,否則會自相矛盾,因為它的價值將是null ,即使表說,它不能null 你可以試試這個:

UPDATE agenda SET phone_nr = '0-000-000-0000' WHERE phone IS NULL;

現在,由於所有為 null 的phone_nr值都設置為非 null 的值,因此您可以在表中將其alterNOT NULL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM