Oracle 经常使用的改动语句
一、表Table
測试表:MY_TEST_TABLE
-- Create table create table MY_TEST_TABLE ( A VARCHAR2(30), B NUMBER(10) default 1, C DATE ) tablespace BOSSWG_CFG pctfree 10 initrans 1 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited ); -- Add comments to the table comment on table MY_TEST_TABLE is 'test'; -- Add comments to the columns comment on column MY_TEST_TABLE.A is 'aaaa'; comment on column MY_TEST_TABLE.B is 'bbbb'; comment on column MY_TEST_TABLE.C is 'cccc';
0.添加字段
alter table MY_TEST_TABLE add D VARCHAR2(50);
-- Add comments to the columns
comment on column MY_TEST_TABLE.D
is 'dddddd';
1. 改动表字段名
alter table MY_TEST_TABLE rename column A to D; //将A字段名改成D
2. 改动表字段类型
alter table MY_TEST_TABLE modify B varchar2(10); //将B字段的number(10)类型改动为varchar2(10)
3. 改动表字段默认值
alter table MY_TEST_TABLE modify B default 2; //将B字段的默认值1改成2
4. 改动表字段的非空属性
alter table MY_TEST_TABLE modify B not null; // 将B字段改成非空
5. 改动表字段的备注
comment on column MY_TEST_TABLE.B // 改动B字段的备注值
is 'bbbb2';
6. 主键操作
alter table MY_TEST_TABLE
add constraint PK_MY_TEST_TABLE primary key (A); //加入主键
alter table MY_TEST_TABLE
drop constraint PK_MY_TEST_TABLE cascade; //删除主键
//假设要改动主键的话。即必须先删除主键,再加入主键。
7.check约束操作
alter table MY_TEST_TABLE
add constraint CKC_MY_TEST_TABLE_B //加入约束
check (B in(1,2,3));
alter table MY_TEST_TABLE
drop constraint CKC_MY_TEST_TABLE_B; //删除约束
//假设要改动约束。则必须先删除约束,再加入约束。
8. 外键操作
alter table MY_TEST_TABLE
add constraint FK_MY_TEST_DTABLE_A foreign key (A) //加入外键
references MY_TEST_TABLE2 (A) on delete cascade;
alter table MY_TEST_TABLE
drop constraint FK_MY_TEST_DTABLE_A; //删除外键
//假设要改动外键。必须先删除外键,再加入外键
二、序列SEQ
測试序列:MY_TEST_SEQ
create sequence MY_TEST_SEQ minvalue 1 maxvalue 9999999999 start with 1 increment by 1 cache 20;
1. 改动minvalue与maxvalue值:
alter sequence MY_TEST_SEQ
minvalue 20
maxvalue 99999999999;
2. 改动nextvalue值:
alter sequence MY_TEST_SEQ increment by 8 nocache; //由原来nextvalue值为2。改动为nextvalue值为10select MY_TEST_SEQ.nextval from dual;
alter sequence MY_TEST_SEQ increment by 1 cache 20; //这句必须有。不然会重置cache和增长率
alter sequence MY_TEST_SEQ increment by -8 nocache; //由原来nextvalue值为10,改动为nextvalue值为2
select MY_TEST_SEQ.nextval from dual;
alter sequence MY_TEST_SEQ increment by 1 cache 20; //这句必须有。不然会重置cache和增长率