Oracle中的DDL语句

SQL Statements
SELECT Data retrieval
INSERT / UPDATE / DELETE / MERGE Data manipulation language(DML)
CREATE / ALTER / DROP / RENAME / TRUNCATE Data definition language(DDL)
COMMIT / ROLLBACK / SAVEPOINT Transaction control
GRANT / REVOKE Data control language(DCL)

一、表格(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_CONSTRAINTS 和user_cons_columns 表中查看到

猜你喜欢

转载自hmx1388.iteye.com/blog/1925003