本文主要描述了ORACLE数据库在应用开发前期的数据库设计相关表空间、用户、表的初始化工作。
1、登录数据库
C:\Users\Administrator>SQLPLUS system/[email protected]/ora150
2、查看数据文件存储路径
SQL> SELECT FILE_NAME FROM DBA_DATA_FILES WHERE ROWNUM <3;
3、创建表空间、索引表空间
CREATE TABLESPACE TBS_CI_COLL NOLOGGING BLOCKSIZE 16K DATAFILE 'D:\ORADATA\ORA150\TBS_CI_COLL.ORA' SIZE 1024M REUSE AUTOEXTEND ON NEXT 256M MAXSIZE UNLIMITED EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;
CREATE TABLESPACE TBS_CI_COLL_IDX NOLOGGING BLOCKSIZE 16K DATAFILE 'D:\ORADATA\ORA150\TBS_CI_COLL_IDX.ORA' SIZE 1024M REUSE AUTOEXTEND ON NEXT 256M MAXSIZE UNLIMITED EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;
4、创建数据库用户
CREATE USER CI_COLL PROFILE DEFAULT IDENTIFIED BY CI_COLL DEFAULT TABLESPACE TBS_CI_COLL ACCOUNT UNLOCK;
5、数据库用户授权
GRANT CONNECT TO CI_COLL WITH ADMIN OPTION;
GRANT RESOURCE TO CI_COLL;
GRANT CREATE ANY TABLE TO CI_COLL;
GRANT INSERT ANY TABLE TO CI_COLL;
GRANT SELECT ANY TABLE TO CI_COLL;
GRANT UPDATE ANY TABLE TO CI_COLL;
GRANT DELETE ANY TABLE TO CI_COLL;
GRANT ALTER ANY TABLE TO CI_COLL;
GRANT DROP ANY TABLE TO CI_COLL;
GRANT CREATE ANY VIEW TO CI_COLL;
GRANT DROP ANY VIEW TO CI_COLL;
GRANT SELECT ANY SEQUENCE TO CI_COLL;
GRANT UNLIMITED TABLESPACE TO CI_COLL;
GRANT EXECUTE ANY PROCEDURE TO CI_COLL;
6、创建普通表,分区表
普通表:
CREATE TABLE CI_COLL.T1(MSG VARCHAR2(10)) TABLESPACE TBS_CI_COLL;
分区表:
CREATE TABLE CI_COLL.T2(TIME_N TIMESTAMP) PARTITION BY RANGE (TIME_N) (PARTITION M_MAX_MON VALUES LESS THAN (MAXVALUE) TABLESPACE TBS_CI_COLL);
7、切割分区表新增月度分区
ALTER TABLE CI_COLL.T2 SPLIT PARTITION M_MAX_MON AT (TO_DATE('20150301','YYYYMMDD')) INTO (PARTITION M_201502 TABLESPACE TBS_CI_COLL, PARTITION M_MAX_MON TABLESPACE TBS_CI_COLL) UPDATE GLOBAL INDEXES;
8、指定分区表数据查询
INSERT INTO CI_COLL.T2 VALUES (to_date('20150102','yyyymmdd'));
INSERT INTO CI_COLL.T2 VALUES (to_date('20150202','yyyymmdd'));
SELECT * FROM CI_COLL.T2 PARTITION(M_201502) T;
9、表空间使用情况查询
SELECT A.TABLESPACE_NAME 表空间名,
TOTAL 表空间大小,
FREE 表空间剩余大小,
(TOTAL - FREE) 表空间使用大小,
ROUND((TOTAL - FREE) / TOTAL, 4) * 100 使用率
FROM (SELECT TABLESPACE_NAME, ROUND(SUM(BYTES) / (1024 * 1024), 4) FREE
FROM DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) A,
(SELECT TABLESPACE_NAME, ROUND(SUM(BYTES) / (1024 * 1024), 4) TOTAL
FROM DBA_DATA_FILES
GROUP BY TABLESPACE_NAME) B
WHERE A.TABLESPACE_NAME = B.TABLESPACE_NAME;