//、、、、、、、、、、、、、、、、创建表空间 \  赋予角色  \ 创建数据表  \  插入数据  \  创建序列  \ 添加注释   、、、、、、、、、、、、、、、、、、、、、、、、、、、

--创建表空间  
create tablespace new_tabspace
datafile 'E:\File_Orc\File\A.DBF'
size 100m
    
create tablespace haha
datafile 'E:\File_Orc\File\B.DBF'    
size 50m                                


--删除空间并

删除物理文件
drop tablespace haha including contents and datafiles

--创建用户
create user zhangsan
identified by 123
default tablespace  new_tabspace
 

--给用户赋予权限
grant connect,resource to zhangsan
grant dba to zhangsan


--创建数据表
--主人表
create table master(
id number(5) not null primary key,
name nvarchar2(50) not null
)

--插入数据
insert into master values(1,'aa')
insert into master values(2,'bb')

select * from master

--删除所插入的数据
delete from master


--创建序列
create sequence master_seq
start with 1   --从1开始
increment by 1 --每次增加1
nomaxvalue     --无最大值
cache 10       --每次增长10


--插入数据
insert into master values(master_seq.nextval,'张三')
insert into master values(master_seq.nextval,'李四')


--查看序列的当前值和下一个值
select master_seq.currval from dual
select master_seq.nextval from dual


--给主人表添加注释
comment on table master is '宠物'
comment on column master.id is '主人ID'


 

//、、、、、、、、、、、、、、、、、、、  给表添加约束  、、、、、、、、、、、、、、、

 

oracle建表约束主要有以下几大类:
   NN:NOT NULL 非空约束
   UK:UNIQUE KEY 唯一约束
   PK:PRIMARY KEY 主键约束
   FK:FOREIGN KEY 外键约束
   CK:CHECK 条件约束

 

//创建表的时候添加约束

create table pet(

id number primary key,                                       --主键约束

usrername  nvarchar2(50)  not null,                     --非空约束

email varchar2(30) unique,                                  --唯一约束

sal number(5) check(sal>1500),                           --核查约束

status char(1) default 1 not null,                        --check约束

master_ID number(5) references pet_type(id)       --外键约束 

)

 

 

--一些其它的相关操作

--修改数据表中某个字段的约束(以唯一约束来说)

alter table pet  add constraint UN_name unique(username)

 

--添加列

alter table 表名 add 新列列名 列数据类型 [default 0 not null] (添加列默认值为0)

--删除列
alter table 表名 drop 列名

--修改列
alter table 表名 alter column 列名 新添加的数据类型 (修改列)

 

posted on 2015-05-06 21:10  子雅思的蛐蛐  阅读(133)  评论(0编辑  收藏  举报