现在的位置: 首页 > 综合 > 正文

Oracle表结构修改SQL

2014年03月17日 ⁄ 综合 ⁄ 共 2547字 ⁄ 字号 评论关闭

大概:    1、删除表的列:  

                  alter   table   yourtable   drop   cloumn   ....  

             2、增加表的列:  

                  alter   table   yourtable   add   .....  

             3、表的重命名:  

                  rename   name1   to   name2  

             4、修改列的数据类型:  

                  alter   table   yourtable   modify   .....

 

1、增加新字段

:alter table table_name add (filed_name varchar2(20) default 'unknown');

                      
如:SQL> alter table testtable   add(name
varcha2r(50));表已更改。

 

2,

修改表字段

不为空:  

alter table table_name modify (filed_name
varchar2(30) not null );  

      修改表字段

:alter table table_name modify (filed_name
varchar2(30) default 'baby'); 

      同时:   

alter table table_name modify (filed_name
varchar2(30) default 'baby' not null );注意not null 放在最后。

                       如:SQL> alter table testtable modify
(name varchar2(20) default 'baby');

                     
表已更改。

                     
SQL> select * from testtable;

                     
可以看到以下所示:        ID NAME                 ADDRESS

---------- -------------------- --------------------

         1 baby

但是在修改default值时,只有在新增加数据时,才会体现出来;

如:SQL> insert into testtable (id,name) values(2,'hh'); //已创建 1 行。

        SQL> select * from testtable;

        ID NAME                 ADDRESS

---------- -------------------- --------------------

         1 baby

         2 hh                   unkown

3,删除表字段

:alter table table_name; drop column column_name;

                        
通常在系统不忙的时候删除不使用的字段,可以先设置字段为unused;

                      
如:SQL> alter table testtable set unused column address

                     
表已更改。

SQL> desc testtable;

名称                                      是否为空? 类型

----------------------------------------- -------- ----------------------------

ID                                                 NUMBER(38)

NAME                                               VARCHAR2(20)

再执行删除:

SQL> alter table testtable drop unused column;

表已更改。

SQL> desc testtable;

名称                                      是否为空? 类型

----------------------------------------- -------- ----------------------------

ID                                                 NUMBER(38)

NAME                                               VARCHAR2(20)

4,表重命名

:rename table_name1 to table_name2;

如:SQL> rename testtable to test2;

表已重命名。

SQL> desc test2;

名称                                      是否为空? 类型

----------------------------------------- -------- -------------------------

ID                                                 NUMBER(38)

NAME                                               VARCHAR2(20)

5,清空表中的数据

:TRUNCATE TABLE table_name;如:SQL> truncate table
test2;表被截断。SQL> select * from test2;未选定行

 

6,给表增加注释:

COMMENT ON TABLE
table_name;如:SQL> comment on table test2 is 'This is a test table';

                         
注释已创建。

7,删除表结构

:DROP TABLE table_name;

                     
此操作属DDL,会自动提交且不可回滚;

 

 

 

 

 

抱歉!评论已关闭.