`

Oracle中的DDL语句

阅读更多

Oracle中的DDL语句


一、表格(table)

1.创建


  • 创建语句创建
create table t_1(
id int primary key,
name varchar(20)
);
  •  利用其他的表创建
create table t_2 as select * from dept;-----复制dept表
create table t_2 as select * from dept where 1=2;-----创建与dept相同表结构的表
2.删除
  • 丢弃表格(包括字段和记录)
drop table t_1;
  • 删除数据(包括记录,不删除字段)
delete from t_1;
truncate table t_1;
注意:
delete只是将表中的数据并不释放 这些数据所占的空间 truncate不仅删除表中的数据而且还释放 数据所在的空间

3.重命名

rename t_1 to t;


二、表格中的字段(column)

  1. 增: alter table t_1 add score int ;
  2. 删:    alter table t_1 drop (score );
  3. 改:    alter table t_1 modify scale int
  4. 重命名: alter table t_1 rename column scale to grade

三、约束(constraint)

1.增

alter table t_3 modify loc not null ;
alter table t_1 add constraint pk_t primary key(id)
alter table t_3 add constraint un_score  unique(score );
2.重命名: alter table t rename constraint score_unique to score_uniq
3.删除: 
ALTER TABLE table_name DROP CONSTRAINT constraint_name |PRIMARY KEY
例1
ALTER TABLE t DROP CONSTRAINT  score_uniq  ;
例2
ALTER TABLE t DROP PRIMARY KEY CASCAED
四、小结
1.rename t_1 to t_2
   rename column c1 to c2
   rename constraint cn1 to cn2
2.对字段的操作和对constraint的操作都是以alter table 表名开始
  1)字段
     增 add score int
     删 drop (score)
     改  modify score int varchar(20)
   2)constraint
      not null:增 modify score not null
                     删 modify score null
       primary key:增 add constraint pk primary key(score)
                          删  drop constraint primary key cascade
       unique :增 add constraint uq unique(score)
                    删 drop constraint uq
     

补充:

约束信息可以从USER_CONSTRAINTSuser_cons_columns 表中查看到

 

  • 大小: 45.5 KB
分享到:
评论
1 楼 avalonzst 2014-05-09  
mark一下.写的真不错.

相关推荐

Global site tag (gtag.js) - Google Analytics