建立表空間是用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;