Oracle基本表操作2
2014-06-24 11:25
309 查看
红色为格式关键词 黑色为自己定义的标识符与选择的关键词
新建表:
create table test
(
id number not null primary key ,
name varchar2(10) not null
)
增加sex、age列:
alter table test
add sex char(8) default ' ' ;
alter table test
add age number(2) default 0;
为name创建索引
create index u_1
on test(name)
注:不能为主键创建索引,因为主键默认了索引,不能为一个字段创建多个索引,后创的无效
为sex列添加检查约束,sex在male、female之间选择:
alter table test2
add constraint s_1
check (sex in('male','female'));
为age列添加检查约束,age值位于0~100之间:
alter table test
add constraint s_2
check (age between 0
and 100);
创建外键约束:
create constraint
fk_t1_tn foreign key(id)
references table_name (id)
约束类型:check检测、unique唯一、primary key主键、not null 不为空、foreign key 外键
注:创建约束时,首先保证表中记录值满足所创建约束的条件,否则创建失败
约束的禁用:
disable constraint
constraint_name
约束的重新启用:
enable constraint
constraint_name
删除约束:
drop constraint
constraint_name
向表中插入记录:
insert into test
values(id1,name1,sex1,age1)
注:数据类型要匹配
克隆表test:
create table test_bak
as select * from
test
结果:克隆了表test的结构与记录,但不包括test表中的约束
为表创建别名:
create synonym
table_name for new_table
更改表名:
rename table_name
to new_name
创建自动增长序列sequence:
create sequence seq_name
increment by 1
start with 0
maxvalue 100|
nomaxvalue
minvalue 0|nominvalue
cycle|nocycle
cache
n |nocache
新建表:
create table test
(
id number not null primary key ,
name varchar2(10) not null
)
增加sex、age列:
alter table test
add sex char(8) default ' ' ;
alter table test
add age number(2) default 0;
为name创建索引
create index u_1
on test(name)
注:不能为主键创建索引,因为主键默认了索引,不能为一个字段创建多个索引,后创的无效
为sex列添加检查约束,sex在male、female之间选择:
alter table test2
add constraint s_1
check (sex in('male','female'));
为age列添加检查约束,age值位于0~100之间:
alter table test
add constraint s_2
check (age between 0
and 100);
创建外键约束:
create constraint
fk_t1_tn foreign key(id)
references table_name (id)
约束类型:check检测、unique唯一、primary key主键、not null 不为空、foreign key 外键
注:创建约束时,首先保证表中记录值满足所创建约束的条件,否则创建失败
约束的禁用:
disable constraint
constraint_name
约束的重新启用:
enable constraint
constraint_name
删除约束:
drop constraint
constraint_name
向表中插入记录:
insert into test
values(id1,name1,sex1,age1)
注:数据类型要匹配
克隆表test:
create table test_bak
as select * from
test
结果:克隆了表test的结构与记录,但不包括test表中的约束
为表创建别名:
create synonym
table_name for new_table
更改表名:
rename table_name
to new_name
创建自动增长序列sequence:
create sequence seq_name
increment by 1
start with 0
maxvalue 100|
nomaxvalue
minvalue 0|nominvalue
cycle|nocycle
cache
n |nocache
相关文章推荐
- oracle基本操作语句(适合初学者)
- oracle基本操作之(一)
- linux下oracle 基本操作
- oracle 基本操作
- Oracle基本操作
- ORACLE 8i 基本操作
- oracle 9i基本操作的探索与总结
- ORACLE SQL*PLUS 基本连接与退出操作
- Solaris下Oracle的基本操作
- oracle基本操作
- Oracle基本操作语句(一)
- 1-oracle的基本操作之----《对表的操作》
- ORACLE基本操作(二)
- Oracle基本操作
- Oracle基本操作语句(二)
- Oracle命令行工具基本操作及SQL 命令
- oracle基本操作语句
- Oracle基本操作SQL
- ORACLE 基本操作
- Oracle基本操作SQL