在本教程中,您将学习如何使用oracle alter table modify
列语句来更改现有列的定义。
要更改表中列的定义,请按如下所示使用alter table modify
列语法:
alter table table_name
modify column_name action;
语句很直接。要修改表的列,需要指定要执行的列名,表名和操作。
oracle允许执行多种操作,但以下是主要常用的操作:
null
值要修改多个列,请使用以下语法:
alter table table_name
modify (
column_name_1 action,
column_name_2 action,
...
);
首先,为演示创建一个名为accounts
的新表:
-- 12c语法
create table accounts (
account_id number generated by default as identity,
first_name varchar2(25) not null,
last_name varchar2(25) not null,
email varchar2(100),
phone varchar2(12) ,
full_name varchar2(51) generated always as(
first_name || ' ' || last_name
),
primary key(account_id)
);
其次,向accounts
表中插入一些行:
insert into accounts(first_name,last_name,phone)
values('trinity',
'knox',
'410-555-0197');
insert into accounts(first_name,last_name,phone)
values('mellissa',
'porter',
'410-555-0198');
insert into accounts(first_name,last_name,phone)
values('leeanna',
'bowman',
'410-555-0199');
第三,通过使用下面的select语句验证插入操作:
select
*
from
accounts;
执行上面查询语句,得到以下结果 -
在oracle 12c中,可以将表列定义为不可见或可见。不可见列不可用于查询,如:
select
*
from
table_name;
或者,
describe table_name;
都是查到不到不可见列的。
但是,可以通过在查询中显式指定不可见列来查询:
select
invisible_column_1,
invisible_column_2
from
table_name;
默认情况下,表列是可见的。可以在创建表或使用alter table modify
列语句时定义不可见列。
例如,以下语句使full_name
列不可见:
alter table accounts
modify full_name invisible;
执行再次查询表中数据,得到以下结果 -
以下语句返回accounts
表除了full_name
列以外的所有列中返回数据:
select
*
from
accounts;
这是因为full_name
列是不可见的。要将列从不可见变为可见,请使用以下语句:
alter table accounts
modify full_name visible;
以下语句将email
列更改为接受非空(not null
)值:
alter table accounts
modify email varchar2( 100 ) not null;
但是,oracle发出以下错误:
sql error: ora-02296: cannot enable (ot.) - null values found
因为当将列从可为null
改为not null
时,必须确保现有数据符合新约束(也就是说,如果原来数据中null
是不行的)。
为了解决这个问题,首先更新email
列的值:
update
accounts
set
email = lower(first_name || '.' || last_name || '@oraok.com') ;
请注意,lower()函数将字符串转换为小写字母。
然后改变email
列的约束:
alter table accounts
modify email varchar2( 100 ) not null;
现在,它应该就会按预期那样工作了。
假设要添加国际代码到phone
列上,比如:前缀加上+86
。 在修改列的值之前,必须使用以下语句扩大phone
列的大小:
alter table accounts
modify phone varchar2( 24 );
现在,我们可以更新电话号码的数据了:
update
accounts
set
phone = '+86 ' || phone;
以下语句验证更新:
select
*
from
accounts;
执行上面查询语句结果中,应该可以看到原电话号码前缀有加上+86
的国际区号了。
要缩短列的大小,请确保列中的所有数据都符合新的大小。
例如,尝试将phone
列的大小缩减到12
个字符:
alter table accounts
modify phone varchar2( 12 );
oracle数据库发出以下错误:
sql error: ora-01441: cannot decrease column length because some value is too big
要解决这个问题,首先,应该从电话号码中删除国际代码(即:+86
):
update
accounts
set
phone = replace(
phone,
'+86 ',
''
);
replace()函数用一个新的子字符串替换一个子字符串。在这种情况下,它将用空字符串替换+86
。
然后缩短phone
列的大小:
alter table accounts
modify phone varchar2( 12 );
假设按以下两列的格式填写全名:
last_name, first_name
为此,可以更改虚拟列full_name
的表达式,如下所示:
alter table accounts
modify full_name varchar2(52)
generated always as (last_name || ', ' || first_name);
以下语句验证修改:
select
*
from
accounts;
执行上面查询语句,可以看到以下结果 -
添加一个名为status
的新列,默认值为1
到accounts
表中。参考以下语句 -
alter table accounts
add status number( 1, 0 ) default 1 not null ;
当执行了该语句,就会将accounts
表中的所有现有行的status
列中的值设置为1
。
要将status
列的默认值更改为0
,请使用以下语句:
alter table accounts
modify status default 0;
可以在accounts
表中添加一个新行来检查status
列的默认值是0
还是1
:
insert into accounts ( first_name, last_name, email, phone )
values ( 'julia',
'madden',
'julia.madden@oraok.com',
'410-555-0200' );
现在,查询accounts
表中的数据:
select
*
from
accounts;
执行上面查询语句,应该看类似下面的结果 -
正如所看到的那样,id
为4
的账户的status
列的值是0
。
在本教程中,您已学习如何使用oraclealter table modify
列语句来更改表中现有列的定义。