测试表: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; //删除外键
//如果要修改外键,必须先删除外键,再添加外键
测试序列: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值为10alter 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和增长率