You are on page 1of 12

Maintaining Tablespace and Datafiles

As a DBA, you are responsible for maintaining tablespaces and datafiles due to the growth of a users database. Your jobs responsibilities dictate that you should at least be informed of the following basic fundamental subjects: Maintaining Tablespaces using Oracle-Managed Files (OMF) Maintaining Tablespaces without using OMF Maintaining Datafiles using OMF Maintaining Datafiles without using OMF Using the AUTOEXTEND ON option Using a default storage option Using the INITIAL parameter Using the NEXT parameter Using the MINEXTENTS parameter Using the MAXEXTENTS parameter Using the PERMANENT ONLINE option Using the DBA_TABLESPACES view The EXTENT_MANAGEMENT column Managing a tablespace LOCALLY Adding a datafile to a tablespace Using the DBA_DATA_FILES view Using the DBA_TABLESPACES view The STATUS column Using the DB_CREATE_FILE_DEST parameter Commands: ALTER TABLESPACE ADD ALTER TABLESPACE OFFLINE ALTER TABLESPACE ONLINE DROP TABLESPACE INCLUDING HOST ERASE ALTER SYSTEM SET CREATE TABLESPACE Hands-on In this exercise you will learn how to maintain tablespaces and datafiles in the database with and without using Oracle-Managed Files (OMF). Now, connect to SQL*Plus as the system/manager user. SQL> CONNECT system/manager@school AS SYSDBA Create a tablespace using user defined file Create a permanent tablespace with the AUTOEXTEND ON option, and a default storage option with an initial size of 100k, a next size of 100k, a minimum extent of 10, and a maximum extent of 200. SQL> CREATE TABLESPACE myfirst_tablespace DATAFILE 'c:_tablespace_01.dbf'

SIZE 10M AUTOEXTEND ON DEFAULT STORAGE (INITIAL 100K NEXT 100K MINEXTENTS 10 MAXEXTENTS 200) PERMANENT ONLINE / From now on, any object that will be created in this tablespace will have their default storage the same as the tablespace default storage unless they have been specified by user. View tablespace information Query the DBA_TABLESPACES view to display the tablespace name and their extent management columns. SQL> SELECT tablespace_name, extent_management FROM dba_tablespaces / Notice the EXTENT_MANAGEMENT column. The default for managing the tablespace is LOCAL. For performance reason, use locally managed tablespace. Add a datafile to a tablespace Add more datafile to MYFIRST_TABLESPACE. SQL> ALTER TABLESPACE myfirst_tablespace ADD DATAFILE 'c:_tablespace_02.dbf' SIZE 10M / View datafile information Query the DBA_DATA_FILES view to display all information. SQL> SELECT * FROM dba_data_files WHERE tablespace_name = 'MYFIRST_TABLESPACE' / Notice that MYFIRST_TABLESPCE has two datafiles. Change tablespace status Set the tablespace status to OFFLINE, and then query the DBA_TABLESPACES view to display the tablespace name and status columns. SQL> ALTER TABLESPACE myfirst_tablespace OFFLINE / SQL> SELECT tablespace_name, status FROM dba_tablespaces / Check the STATUS column. Notice that it is OFFLINE. Now, set the tablespace status to ONLINE. SQL> ALTER TABLESPACE myfirst_tablespace ONLINE / Drop a tablespace Drop the tablespace, including all of the objects in it plus the constraints.

SQL> DROP TABLESPACE myfirst_tablespace INCLUDING CONTENTS CASCADE CONSTRAINTS / SQL> HOST ERASE c:_tablespace_01.dbf SQL> HOST ERASE c:_tablespace_02.dbf Notice that since we did not use Oracle-Managed Files, we should delete the datafiles from the system. Create a tablespace using Oracle-Managed file Create a tablespace using Oracle-Managed Files (OMF). First, you should alter the system to set the DB_CREATE_FILE_DEST parameter to a valid sub-directory. Then create a table with a no datafile option. The database will then create and maintain the datafiles in the defined Oracle file destination, for example c:directory. SQL> ALTER SYSTEM SET db_create_file_dest='c:' / SQL> CREATE TABLESPACE my2nd_tablespace / Query the DBA_DATA_FILES directory view to display all of the information. SQL> SELECT * FROM dba_data_files WHERE tablespace_name = 'MY2ND_TABLESPACE' / Check the Oracle database naming convention. Notice that the first eight characters of the tablespace name is part of the datafile name. Drop an OMF tablespace Drop the tablespace including all of the objects in it including the constraints. SQL> DROP TABLESPACE my2nd_tablespace INCLUDING CONTENTS CASCADE CONSTRAINTS / Notice that since we use Oracle-Managed Files we do not need to delete the datafile from the system. Oracle automatically deletes it from the system. Q: Describe a tablespace. Q: How do you create a tablespace in a database? Q: How do you maintain a tablespace using the Oracle-Managed file technique? Q: How do you maintain a tablespace using the User-Managed file technique? Q: How do you maintain a datafile using the Oracle-Managed file technique? Q: How do you maintain a datafile using the User-Managed file technique? Q: Describe the AUTOEXTEND ON option in the CREATE TABLESPACE statement. Q: Describe the following storage options in the CREATE TABLESPCES statement. INITIAL parameter NEXT parameter MINEXTENTS parameter MAXEXTENTS parameter Q: Describe the PERMANENT ONLINE option.

Q: What does it mean that a tablespace LOCALLY managed? Q: Describe the DBA_TABLESPACES view. Q: How do you add a datafile to an existing tablespace? Q: When do you use the DB_CREATE_FILE_DEST parameter? Q: What do the following SQL statement do? What are the differences between the first CREATE statement and the second CREATE statement? SQL> CREATE TABLESPACE myfirst_tablespace DATAFILE 'c:_tablespace_01.dbf' SIZE 10M AUTOEXTEND ON DEFAULT STORAGE (INITIAL 100K NEXT 100K MINEXTENTS 10 MAXEXTENTS 200) PERMANENT ONLINE / SQL> CREATE TABLESPACE my2nd_tablespace / Q: How do you drop a tablespace? Q: How do you drop a tablespace if it contains objects?

Maintaining a TEMPORARY tablespace


Introduction As a DBA, you are responsible for maintaining a TEMPORARY tablespace due to a users big transaction sorting. Your jobs responsibilities dictate that you should at least be informed of the following basic fundamental subjects: Maintaining a TEMPORARY tablespace with OMF Maintaining a TEMPORARY tablespace without OMF Using the DB_CREATE_FILE_DEST parameter Creating a TEMPORARY tablespace with OMF Using the DBA_TABLESPACES view TABLESPACE_NAME INITIAL_EXTENT MAX_EXTENTS CONTENTS LOGGING STATUS Using the DBA_DATA_FILES directory view Using the DBA_TEMP_FILES directory view Using the V$SESSION dictionary view Using the V$SORT_SEGMENT view Dropping a TEMPORARY tablespace Creating a TEMPORARY tablespace using original syntax Advantages of using OMF Creating a TEMPORARY tablespace using the TEMPFILE clause Using Oracle-Managed Files

Using the UNIFORM option Using the DBA_TEMP_FILES view Commands: ALTER SYSTEM SET CREATE TEMPORARY TABLESPACE DROP TABLESPACE CREATE TABLESPACE TEMPORARY Hands-on In this exercise you will learn how to maintain a temporary tablespace with or without using Oracle-Managed Files (OMF) and more. First, connect to SQL*Plus as the system/manager user. SQL> CONNECT system/manager@school AS SYSDBA Define a create file destination Let's first make sure that the DB_CREATE_FILE_DEST value is set to a valid sub-directory. SQL> ALTER SYSTEM SET db_create_file_dest='c:' / Create a temporary tablespace (OMF) Now, create a temporary tablespace with Oracle-Managed Files (OMF). Users create temporary segments in a tablespace when a disk sort is required to support their use of select statements containing the GROUP BY, ORDER BY, DISTINCT, or UNION, or the CREATE INDEX statements. SQL> CREATE TEMPORARY TABLESPACE mytemp / Query the DBA_TABLESPACES view to display the tablespace name, initial extent, max extents, contents, logging, and tablespace status. SQL> SELECT tablespace_name, initial_extent, max_extents, contents, logging, status FROM dba_tablespaces / Notice that the CONTENTS column is set to TEMPORARY. The default init size is 1 Megabytes and the maximum extend is unlimited. Query the DBA_DATA_FILES directory view. SQL> SELECT file_id, file_name, tablespace_name, status FROM dba_data_files / Notice that the temporary tablespace is not there. Query the DBA_TEMP_FILE directory view. SQL> SELECT * FROM dba_temp_files / Note the naming convention.

Open a new session and connect to SQLPlus as the ISELF user and then do the following statements. SQL> CONNECT iself/schooling SQL> SET SQLPROMP 'iself > ' SQL> SELECT e1.ename FROM emp e1, emp e2, emp e3, emp e4 ORDER BY 1 / While the other session is running, come back and query the following statement. SQL> SET SQLPROMPT 'dba > ' SQL> SELECT s.username, tablespace, contents, extents, blocks FROM v$session s, v$sort_usage WHERE s.saddr = session_addr / Or you can query the V$SORT_SEGMENT table. SQL> SELECT tablespace_name, extent_size, total_extents, max_sort_blocks FROM v$sort_segment / The sort segment high-water mark information is exist for duration of the instance. Starting the instance will clean this table. This a good way to find out how big the users sort segments has become. Drop a temporary tablespace Drop the mytemp tablespace and create it again with the old method. SQL> DROP TABLESPACE mytemp / Notice that your datafile will be deleted. Create a temporary tablespace using User-Managed file SQL> CREATE TABLESPACE mytemp DATAFILE 'c:_01.tmp' SIZE 20M TEMPORARY / Drop a temporary tablespace (UMF) Drop the mytemp tablespace and delete the datafile from the system. Note that we need to delete the datafile since we did not create the temporary tablespace using Oracle-Managed Files (OMF). SQL> DROP TABLESPACE mytemp / SQL> HOST ERASE c:_01.tmp TEMPIFLE and UNIFORM options Now, create a temporary tablespace using the TEMPFILE and UNIFORM options. Make sure to use Oracle-Managed Files. SQL> CREATE TEMPORARY TABLESPACE mytemp

TEMPFILE 'mytemp_01.tmp' SIZE 20M EXTENT MANAGEMENT LOCAL UNIFORM SIZE 10M / The uniform extent sizing is used to simplify how extents are allocated to objects. Query the DBA_TEMP_FILES view. SQL> SELECT * FROM dba_temp_files / Here, you see the MYTEMP tablespace. Notice that the tablespace is located in %ORACLE_HOME%sub-directory. Remember that all tablespaces in the database will use the standard block size defined for the database.

Questions:
Q: How do you create a TEMPORARY tablespace using Oracle-Managed File (OMF) technique? Q: How do you create a TEMPORARY tablespace using User-Managed File (UMF) technique? Q: What is the DB_CREATE_FILE_DEST parameter? Q: Describe the DBA_TABLESPACES view. Q: Describe the DBA_DATA_FILES view. Q: Describe the following views. DBA_DATA_FILES directory view DBA_TEMP_FILES directory view V$SESSION dictionary view V$SORT_SEGMENT view Q: How do you drop a temporary tablespace? Q: Create a temporary tablespace using an Oracle original syntax. Q: Create a temporary tablespace using the TEMPFILE clause. Q: Describe the UNIFORM option. Q: What do the following SQL statements do? SQL> SELECT tablespace_name, initial_extent, max_extents, contents, logging, status FROM dba_tablespaces / SQL> SELECT s.username, tablespace, contents, extents, blocks FROM v$session s, v$sort_usage WHERE s.saddr = session_addr / SQL> SELECT tablespace_name, extent_size, total_extents, max_sort_blocks FROM v$sort_segment / SQL> CREATE TEMPORARY TABLESPACE mytemp TEMPFILE 'mytemp_01.tmp' SIZE 20M EXTENT MANAGEMENT LOCAL

UNIFORM SIZE 10M /

Maintaining a Tablespace
Introduction As a DBA, you are responsible for maintaining tablespaces and datafiles due to a users usage of the tablespace. If your user does not update any tables in the tablespace, you may want to change the tablespace mode to the READ ONLY mode. Or if you have any I/O problems on a disk, you can relocate the tablespace to a new not busy disk. Your jobs responsibilities dictate that you should at least be informed of the following basic fundamental subjects: Maintaining a tablespace mode READ ONLY Relocating a tablespace Using OMF Without using OMF Using the DBA_TABLESPACES dictionary view TABLESPACE_NAME STATUS Altering a tablespace mode to READ ONLY Altering a tablespace mode to READ WRITE Performing activities in a READ ONLY tablespace mode Dropping table in a READ ONLY mode Using the DBA_TABLESPACES view Relocating a tablespace Copying a datafile Altering the database to a new location Changing a tablespace status Commands: ALTER TABLESPACE READ ONLY ALTER TABLESPACE READ WRITE ALTER TABLESPACE OFFLINE ALTER TABLESPACE ONLINE HOST COPY HOST ERASE ALTER DATABASE RENAME FILE Hands-on In this exercise you will learn how to maintain the tablespace mode (READ ONLY) and relocate the tablespace with or without using Oracle-Managed Files (OMF) and more. Now, let's connect to SQL*Plus as the system/manager user. SQL> CONNECT system/manager@school AS SYSDBA

View tablespace information Query the DBA_TABLESPACES directory view to display the tablespace name and status columns. SQL> SELECT tablespace_name, status FROM dba_tablespaces / Take notes on the STATUS of the USERS tablespace. Populate a table Now, create a table in the USERS tablespace and write a stored procedure to populate that table. SQL> CREATE TABLE table1 (col1 number, col2 varchar2(20)) TABLESPACE users / (Procedure to populate it) SQL> BEGIN SQL> FOR i IN 1..100 LOOP SQL> INSERT INTO table1 VALUES (i, 'AA' || i); SQL> END LOOP; SQL> COMMIT; SQL> END; SQL> / The table should be populated with 100 inserted records. Change tablespace status Now, alter the USERS tablespace status to READ ONLY. SQL> ALTER TABLESPACE users READ ONLY / Query the DBA_TABLESPACES view to display the tablespace name and status columns again. SQL> SELECT tablespace_name, status FROM dba_tablespaces / Take notes on the STATUS of the USERS tablespace. Note that it is in the READ ONLY mode. Create a table in the READ ONLY tablespace. SQL> CREATE TABLE table2 (col1 number, col2 varchar2(20)) TABLESPACE users / Notice that you cannot create any tables in that tablespace. Try to add a record into table1 which is in the READ ONLY tablespace. SQL> INSERT INTO table1 VALUES (100, 'AA100') /

Notice that a record cannot be added at this time, nor you can drop the table. Change a tablespace status to READ WRITE mode Change the tablespace status to the READ WRITE mode. SQL> ALTER TABLESPACE users READ WRITE / Drop the table. SQL> DROP TABLE table1 / Now, you can drop the table, since the tablespace status is READ WRITE. Relocate a tablespace Now, let's relocate the USERS tablespace from c:\oracle\oradata\school\users01.dbf to c:\newfolder\users01.dbf. Query the DBA_DATA_FILES view to check where the USERS tablespace is located. SQL> SELECT file_name, status FROM dba_data_files WHERE tablespace_name = 'USERS' / Take a note of its status. Set the USERS tablespace status to OFFLINE. SQL> ALTER TABLESPACE users OFFLINE / Now, copy the USERS datafile to the newfolder subdirectory and then, delete the original. SQL> -- Copy the file(s). SQL> HOST COPY C:\oracle\oradata\school\users01.dbf c:\newfolder\*.* SQL> -- Erase the file(s). SQL> HOST ERASE C:\oracle\oradata\school\users01.dbf Alter the database and change the original location to the new location. Notice that the alter statement will change and update the content of the controlfile since the database structure was changed. SQL> ALTER DATABASE RENAME FILE 'c:\oracle\oradata\school\users01.dbf' TO 'c:\newfolder\users01.dbf' / Change the tablespace status to ONLINE. SQL> ALTER TABLESPACE users ONLINE /

Query the DBA_DATA_FILES view to check the relocation. SQL> SELECT file_name, status FROM dba_data_files WHERE tablespace_name = 'USERS' / Looks like the relocation process was completed successfully. Now, relocate the datafile back to its original location. Relocate a tablespace to its original location -- 1) Set the USERS tablespace status back to OFFLINE. -- 2) Copy the USERS datafile from the newfolder back to its original location. -- 3) The datafile. -- 4) Alter the database to rename the datafile location. -- 5) Then, set the tablespace status to ONLINE. Offline the tablespace. SQL> ALTER TABLESPACE users OFFLINE / Copy the file(s). SQL> HOST COPY \ c:\newfolder\users01.dbf C:\oracle\oradata\school\users01.dbf Erase the file(s). SQL> HOST ERASE C:\newfolder\users01.dbf Alter the database. SQL> ALTER DATABASE RENAME FILE 'c:\newfolder\users01.dbf' TO 'c:\oracle\oradata\school\users01.dbf' / Online the tablespace. SQL> ALTER TABLESPACE users ONLINE / Query the DBA_DATA_FILES view to check the relocation. SQL> SELECT file_name, status FROM dba_data_files WHERE tablespace_name = 'USERS' / It looks like the relocation process was completed successfully.

Questions:
Q: How do you change a tablespace mode to the READ ONLY mode? Q: How do you relocate a tablespace? Q: How do you change a tablelspace mode to the READ WRITE mode? Q: How do you OFFLINE a tablespace mode? Q: How do you ONLINE a tablespace mode? Q: When do you OFFLINE a tablespace mode? Q: When can you perform the following SQL statement? SQL> ALTER DATABASE RENAME FILE 'c:\oracle\oradata\school\users01.dbf' TO 'c:\newfolder\users01.dbf' /

You might also like