`

Oracle之創建和管理表

 
阅读更多

一、創建表

//創建一個student表
create table student(
ID number(6),   //學號
NAME varchar2(25),  //名字
BIRTH date,   //生日
GRADE number(3,1) //成績 整數部分最大為3位,小數部分是1位
)
//創建表的第二種方式,將已有表中的列,作為一個新表
//将student1表中某些列创建成一个新表
create table student
as 
select id,name,age from student1
where 1=2 //表示创建的表为空表,若不加此条件,则会将已有表中的数据也会加入到新表中

 二、在已創建的表中增加列

//在student 表中增加AGE列
alter table student
add (AGE number(2) default 18)//默認為18歲

 三、修改列

alter table student
modify (ID number(8)) //修改ID列的位數為8,若表中有數據則無法修改

 四、刪除列

alter table student
drop column AGE //刪除AGE列

 五、重命名列

alter table student
rename column ID to IDCARD //修改列ID 的名字為IDCARD

 六、清空表

//清空student表中的數據
truncate table student

 七、注释comments

(1)给指定表添加注释

-- Add comments to the table   
comment on table TABLENAME  
  is '表的注释';  

 (2)给指定表指定列添加注释。

-- Add comments to the columns   
comment on column TABLENAME.COLUMNNAME  
  is '指定表指定列注释';  

 (3)相关视图

USER_TAB_COMMENTS
DBA_TAB_COMMENTS
ALL_TAB_COMMENTS
USER_COL_COMMENTS
DBA_COL_COMMENTS
ALL_COL_COMMENTS

 如:select  * from user_tab_comments where table_name = 'student';

八、TRUNCATE,DELETE,DROP放在一起比较:
TRUNCATE TABLE:删除内容、释放空间但不删除定义(表结构)。
DELETE TABLE:删除内容不删除定义(表结构),不释放空间。
DROP TABLE:删除内容和定义(表结构),释放空间。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics