简体   繁体   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;

After I tried to change the phone_nr variable to not null, I get an error在我尝试将phone_nr变量更改phone_nr空后,出现错误

Invalid ALTER TABLE option无效的 ALTER TABLE 选项

I'm a beginner at SQL and I don't really understand where the problem is.我是 SQL 的初学者,我真的不明白问题出在哪里。

AFAIK, you can't use an alter column clause to change a column's nullability, but you could use a modify column clause instead: AFAIK,您不能使用alter column子句来更改列的可为空性,但您可以使用modify column子句:

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

Did you make sure that all the phone_nr values are set to something that isn't null?您是否确保所有phone_nr值都设置为不为空的值? You can only alter it to be not null if all values are something that is not null, otherwise it would contradict itself because its value would be null even though the table says it cannot be null .你只能alter它是not null ,如果所有的值都是一些不为空,否则会自相矛盾,因为它的价值将是null ,即使表说,它不能null You can try this:你可以试试这个:

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

Now, since all phone_nr values that were null are set to a value that is not null you can alter it to be NOT NULL in the table.现在,由于所有为 null 的phone_nr值都设置为非 null 的值,因此您可以在表中将其alterNOT NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM