本文主要是关于Oracle数据库表中字段的增加、删除、修改和重命名的操作。
一:增加字段语法:
alter table tablename add (column datatype default value,….);
说明:alter table 表名 add (字段名 字段类型 默认值 是否为空);
例:alter table sf_users add (HeadPIC blob);
例:alter table sf_users add (userName varchar2(30) default '空' not null);
二 :修改字段的语法:
alter table tablename modify (column datatype default value,….);
说明:alter table 表名 modify (字段名 字段类型 默认值 是否为空);
例:alter table sf_InvoiceApply modify (BILLCODE number(4));
三:删除字段的语法:
alter table tablename drop (column);
说明:alter table 表名 drop column 字段名;
例:alter table sf_users drop column HeadPIC;
四:列重命名
说明:alter table 表名 rename column 列名 to 新列名 (其中:column是关键字)
例:alter table sf_InvoiceApply rename column PIC to NEWPIC;
五:表重命名
说明:alter table 表名 rename to 新表名
例:alter table sf_InvoiceApply rename to sf_New_InvoiceApply;
表空间重命名
SQL> alter tablespace chen rename to chen00;
Tablespace altered.
SQL> select table_name,tablespace_name from user_tables where table_name='T2';
TABLE_NAME TABLESPACE_NAME
T2 CHEN00
六: 数据文件重命名
Oracle 重命名数据文件的两种方法:ALTER TABLESPACE RENAME DATAFILE和ALTER DATABASE RENAME FILE
语法:
ALTER DATABASE RENAME FILE 'old_name' to 'new_name'
ALTER TABLESPACE tablespace_name RENAME DATAFILE 'old_name' TO 'new_name'
通过这两种方法重命名数据文件必须保证:
1 目标文件存在(The operating system files continue to exist)
2 数据库在open状态下重命名数据文件必须保证要重命名的数据文件所在的表空间处于offline的状态
由于在open状态下system和sysaux表空间不能够被offline,所以在open状态只能重命名除去system和sysaux之外的数据文件。
ALTER DATABASE RENAME FILE 不仅仅可以重命名数据文件,同样可以重命名tempfiles, or redo log file。
SQL> select name from v$dbfile;
NAME
----
/u01/app/oracle/oradata/orcl/users01.dbf
/u01/app/oracle/oradata/orcl/undotbs01.dbf
/u01/app/oracle/oradata/orcl/sysaux01.dbf
/u01/app/oracle/oradata/orcl/system01.dbf
/u01/app/oracle/oradata/orcl/example01.dbf
/home/oracle/chen01.dbf
6 rows selected.
SQL> alter tablespace chen00 offline;
Tablespace altered.
[oracle@chen ~]$ mv chen01.dbf /u01/app/oracle/oradata/orcl/chen0001.dbf
SQL> alter tablespace chen00 rename datafile '/home/oracle/chen01.dbf' to '/u01/app/oracle/oradata/orcl/chen0001.dbf';
Tablespace altered.
SQL> alter tablespace chen00 online;
Tablespace altered.
SQL> select name from v$dbfile;
NAME
----
/u01/app/oracle/oradata/orcl/users01.dbf
/u01/app/oracle/oradata/orcl/undotbs01.dbf
/u01/app/oracle/oradata/orcl/sysaux01.dbf
/u01/app/oracle/oradata/orcl/system01.dbf
/u01/app/oracle/oradata/orcl/example01.dbf
/u01/app/oracle/oradata/orcl/chen0001.dbf
6 rows selected.