您的位置:首页 > 数据库 > Oracle

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

 

 

 

 

 

 

 

 

 

 

 

   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息