建立表空间是用create tablespace命令完成的,
create tablespace sp001 datafile 'd:/sp001.dbf' size 20m uniform size 128k
说明: 执行完上述命令后,会建立名称为data001的表空间,并为该表空间建立名称为
data01.dbf的数据文件,区的大小为128k,
使用数据表空间
create table mypart(deptno number(4),dname varchar2(14),loc varchar2(13),loc
varchar2(13) ) tablespace sp001;
修改表空间的状态
alter tablespace 表空间 read only; //设为只读
alter tablespace 表空间名 offline ;//使表空间脱机
alter tablespace 表空间名 online;//使表空间联机
alter tablespace 表空间名 read write;//使表空间可读写
(1) 知道表空间名,显示该表空间包括的所有表
select * from all_tables where tablespace_name ='表空间名'
(2)知道表空间名,查看该表属于哪个表空间
select tablespace_name , table_name from user_tables where table_name ='emp'
删除表空间
一般情况下,由特权用户或是dba表操作,如果是其他用户操作,那么要求要求用户具有drop
tablespace系统权限
例 : drop tablespace '表空间' including contents and datafiles;
说明: including contents 表示删除表空间时,删除该空间的所有数据库对象。而datafiles
表示将数据库文件也删除。
扩展表空间
(1) 增加数据文件
sql>alter tablespace sp01 add datafile 'd:/test/sp01.dbf' size 20m
(2) 增加数据文件的大小
sql>alter tablespace 表空间名 'd:/test/sp01.dbf' resize 20m;
这里需要注意的是数据文件的大小不要超过500m.
(3) 设置文件的自动增长
sql>alter tablespace 表空间名 'd"/test/sp01.dbf' autoextend on next 10m maxsize
500m;
移动数据文件
一般情况下是指从一个磁盘下移动到另一个磁盘中。
步骤:
(1)确定数据文件所在的表空间
select tablespace_name from dba_data_files where file_name ='d:/sp001.dbf';
(2) 使表空间脱机
确保数据文件的一致性,将表空间转变为offline的状态
alter tablespace sp01 offline;
(3)使用命令移动数据文件到指定的目标位置
host move d:/sp001.dbf c: /sp001.dbf
(4) 执行alter tablespace 命令
在物理上移动了数据后,还必须执行alter tablespace 命令对数据库文件进行逻辑修改
sql>alter tablespace sp01 rename datafile 'd:/test/sp01.dbf' to 'c:/test/sp01.db'
(5)使表空间联机
在移动了数据文件后,为了使用户可以访问该表空间,必须将其转变为online状态:
sql>alter tablespace data01 online;
创建表的事例:
create table goods(goodsId char(8) primary key,--主键
goodsName varchar2(30),
unitprice number(10,2) check (unitprice >0),
category varchar2(8),
provider varchar2(30));
create table customer(customerId char(8) primary key,--主键
name varchar2(50) not null,
address varchar2(50),
email varchar2(50) unique,--唯一
sex char(2) default '男' check (sex in('男','女')),
cardId char(18));
create table purchase(customerId char(8)references customer(customerId), --外键
goodId char(8) references goods(goodsId),
nums number(10) check(nums between 1 and 30));
向表中的字段添加索引
alter table goods modify goodName not null;
alter table customer add constraint sss unique(cardId);
alter table customer add costraint aaa check(address in('东城','西城'));
//sss和aaa都是预定义取得别名(就是指临时定义的)
删除约束
当不再需要某个约束时,可以删除。
alter table 表名 drop constraint 约束名称
在删除主键约束的时候,可能有错误,比如:
alter table 表名 drop primary key;
这是因为如果在两张表存在主从关系,那么在删除主表的主键约束时,必须带上cascade选项
比如 alter table 表名 drop primary key cascade;