我希望有很多很多的爱,如果没有很多的爱,我希望有很多很多的钱;没有很多的钱,我希望拥有健康! I am like the road in the night listening to the footfalls of its memories in silence.

创建/复制/修改/删除表、字段(及属性)、数据

上一篇 / 下一篇  2009-01-24 13:17:49 / 个人分类:数据库知识(SQL)

说明:(sourceTable已有的表,needTable为所需的新建表,sourceColunm_n为sourceTable的字段名)


-- 1、创建新表并复制数据 
      Create   table   needTable   as     Select   *   from   sourceTable 


-- 2、创建新表但不复制数据  
      create  table   needTable   as     Select   *   from   sourceTable where rownum<1; --使条件不满足即可以不复制数据


-- 3、创建特定字段的表并复制数据
      create  table   needTable   as     Select  sourceColunm_0   from   sourceTable where rownum<1;


-- 4、创建特定字段的表但不复制数据
      create  table   needTable   as     Select  sourceColunm_0   from   sourceTable where rownum<1; --使条件不满足即可以不复制数据

 

-- 5、删除表
      drop table TestTableName


-- 6、删除表某个字段 
      Alter   table  TestTableName  drop   column   TestColunmName;  


-- 7、删除表某个字段
      Alter   table  TestTableName  drop   column   TestColunmName;  
  
--8、修改表列名:关于列名,没有直接的方法改变。但是可以通过其他方法达到改变列名的目的。 
     1) 增加相同属性的列:增加一个与原来字段 oldColunmName 相同结构的字段 newColunmName
        Alter   table   TestTableName   add(newColunmName   varchar2(20));  
     2)复制原来字段值给新字段: 将 oldColunmName 中的数据复制到 newColunmName 中  
        Update   A   Set   newColunmName=oldColunmName;  
     3) 删除原来字段 oldColunmName
        Alter   table   TestTableName   drop   column   oldColunmName;  
     4) 修改完成  
    
--9、修改表名:
     方法一:直接修改表明   
      Alter   table   oldTableName   rename   to   newTableName  
   
     方法二:新建一个表,并把原来的表的数据复制过去,之后删除原来的表
       1) 创建新表并复制数据  
          Create   table newTableName  as     Select   *   from   oldTableName
       2) 删除表A1  
          drop table oldTableName
           
--10、修改表字段属性, 如改类型、长度、是否为空:  
  alter   table   TestTableName   modify   (testColunmName   varchar2(20)   not   null);  
     要修改类型,字段必须是空的;  
     要修改长度,如果字段是空的,完全可以改,如果字段不空,则只能增加长度,不能减小;  
     要修改是否为空,字段必须符合constraint的要求 


TAG:

 

评分:0

我来说两句

Open Toolbar