테이블스페이스 개요 및 사용법

공부/SQL

테이블스페이스

 

테이블스페이스란 데이터베이스에 테이블을 저장하는 가장 큰 개념으로써 테이블들이 모여있는 공간,

                        물리적인 데이터의 논리적인 저장장소입니다.

또한 Temporary tablespace는 임시자료를 저장하는 공간으로  정렬작업 등 수행하는 경우 PGA공간이 부족 할 때 사용합니다.

 ----------------------------------------------------------------------------------------------------

1. 테이블스페이스 생성 및 조회

 

1-1 테이블스페이스 생성

conn / as sysdba // 테이블스페이스는 관리자로 로그인해야 생성 가능합니다

 

형식) Create tablespace 테이블스페이스명 datafile '경로포함 파일명' size nm

SQL> create tablespace oradata2 datafile 'C:\webtest\1.db\oradata2.dbf' size 10m;

 

1-2 테이블스페이스 조회

       SQL>select * from v$tablespace;

 ----------------------------------------------------------------------------------------------------

2. 테이블스페이스 연결

 

2-1 테이블 연결

형식) Create table 테이블명(테이블생성SQL) tablespace 연결시킬테이블스페이스명

SQL> create table t2(c1 number) tablespace oradata2;

 

2-2 계정 연결

형식) Create User 생성시킬유저명 indentified by 암호 default tablespace 연결시킬테이블스페이스명 [temporary tablespace 임시ts명];

SQL> create user kim2 identified by kim2 default tablespace oradata2;

SQL> grant resource, connect to kim2;  //kim2계정 오라클 사용 및 접속 권한 부여

 

2-3 연결중인 테이블스페이스 확인

SQL> select username, default_tablespace from user_users;

SQL> select table_name, tablespace_name from user_tables;

        //접속 계정의 테이블명과 테이블스페이스 출력하므로 2-1에서 생성한 테이블은 조회 불가능

 ----------------------------------------------------------------------------------------------------

 

3. 테이블스페이스 변경

 

3-1 계정 변경

형식) alter user 수정user default tablespace 새로변경할 ts명

SQL> conn / as sysdba    //관리자만 계정의 테이블스페이스 변경 가능

SQL> alter user test1 default tablespace oradata2;

 

3-2 테이블 변경

형식) alter table 수정t move tablespace 새로변경할 ts명 

SQL> alter table t1 move tablespace oradata2; //일반계정 소유테이블에 한하여 변경 가능

 

 

===========================================================================

 

[문제] shop2 테이블스페이스를 c:\webtest\shop2.dbf 100m생성하고,

        계정(shopuser/t1234) 생성하고 shop2테이블스페이스와 연결하여라

 

sql>conn / as sysdba

sql>create tablespace shop2 datafile 'c:\webtest\shop2.dbf' size 100m;

sql>create user shopuser identified by t1234 default tablespace shop2;

 

===========================================================================

 

4. 임시테이블스페이스 생성 및 조회

 

4-1 임시테이블스페이스 생성

형식) create temporary tablespace 임시ts명 tempfile '경로포함 파일명' size nm;

SQL>create temporary tablespace temp3 tempfile 'C:\webtest\temp3.dbf' size 50m;

 

4-2 테이블스페이스 조회

select tablespace_name,   file_name,    autoextensible         from dba_data_files;

        논리적인 ts명      물리적인ts명  용량확장가능 유무

 

autoextensible => YES : 용량이 부족하면 확장 가능한 상태

                         NO : 추가한 테이블스페이스인 경우 default 는 확장이 제한적

 

5. 테이블스페이스 용량 확장

 

5-1 공간 늘리기 

형식)alter tablespace 변경시킬ts명 add datafile '경로포함해서 새로운 파일명' size nm;

SQL> alter tablespace shop2 add datafile 'C:\webtest\shop22.dbf' size 300m;

 

5-2 자동증가 설정

형식)alter database datafile '경로포함해서 변경할 파일명.dbf' autoextend on next 증가치(n)m maxsize 최대용량크기(m)

SQL>alter database datafile 'C:\webtest\1.db\oradata2.dbf' autoextend on next 2m maxsize 200;   ////50m-> 52,54,200m;

 

6. 테이블스페이스 삭제

형식) drop tablespace 삭제시킬 ts명 including contents and datafiles;

SQL>drop tablespace shop2 including contents and datafiles;

======================================================================================

SQL> create tablespace oradata2 datafile 'C:\webtest\1.db\oradata2.dbf' size 10m; SQL> create table t2(c1 number) tablespace oradata2; SQL> create user kim2 identified by kim2 default tablespace oradata2; SQL> alter user test1 default tablespace oradata2; SQL> alter table t1 move tablespace oradata2; SQL> create temporary tablespace temp3 tempfile 'C:\webtest\temp3.dbf' size 50m; SQL> create user test3 identified by t1234 default tablespace oradata2 temporary tablespace temp3; SQL> alter tablespace shop2 add datafile 'C:\webtest\shop22.dbf' size 300m; SQL> alter database datafile 'C:\webtest\1.db\oradata2.dbf' autoextend on next 2m maxsize 200; SQL> drop tablespace shop2 including contents and datafiles;