ALTER TABLESPACE
Function Description
Modifies the attributes of an existing tablespace.
Precautions
Only the tablespace owner or users granted the
ALTER TABLESPACEpermission can execute this command. System administrators have this permission by default.The following operations can only be performed when the database is in the
OPENstate:Adding a data file
Deleting a data file
Modifying the
AUTOEXTENDattribute of a fileRenaming a tablespace
The following operation can only be performed when the database is in the
OPEN RESTRICTEDstate:- Modifying the name of a data file
The
AUTOOFFLINEattribute can only be set for user tablespaces.
Syntax
Rename a tablespace.
ALTER TABLESPACE 'tablespace_name'
RENAME TO 'new_tablespace_name'Shrink or punch the tablespace size.
ALTER TABLESPACE 'tablespace_name'
{ SHRINK SPACE KEEP integer [ K | M | G | T ]
| PUNCH { SIZE integer [ K | M | G ] } }Modify the AUTOOFFLINE or AUTOEXTEND attribute of a tablespace.
ALTER TABLESPACE 'tablespace_name'
{ AUTOOFFLINE { ON | OFF }
| AUTOEXTEND
{ OFF
| ON [ NEXT integer [ K | M | G ] ] [ MAXSIZE { integer [ K | M | G ] | UNLIMITED }]
}
}Add a data file to a tablespace or delete a data file from a tablespace.
ALTER TABLESPACE 'tablespace_name'
{ ADD DATAFILE
{ 'file_name' SIZE integer [ K | M | G ] [COMPRESS]
[AUTOEXTEND
{ OFF
| ON [ NEXT integer [ K | M | G ] ] [ MAXSIZE { integer [ K | M | G ] | UNLIMITED }]
}
] [ segments integer ]
}
| DROP DATAFILE 'file_name' }Rename a data file in a tablespace.
ALTER TABLESPACE 'tablespace_name'
RENAME DATAFILE 'old_file_name' TO 'new_file_name'Parameter Description
Common parameters:
integer: Indicates a positive integer excluding0.K: Indicates the unit KB.M: Indicates the unit MB.G: Indicates the unit GB.T: Indicates the unit TB.tablespace_name: Indicates the tablespace to be modified.Value range: an existing tablespace name.
new_tablespace_name: Indicates the new name of the tablespace.Value range: a string that conforms to the identifier naming convention.
RENAME TO 'new_tablespace_name': Renames a tablespace.SHRINK SPACE KEEP integer [ K | M | G | T ]: Shrinks the tablespace size.In the
RESTRICTEDmode with no residual transactions, this command can be used to rebuild the UNDO tablespace.In the
OPENmode, any tablespace except TEMP can be shrunk.The TEMP tablespace can be shrunk only in the
OPEN RESTRICTEDmode.The size of a tablespace that can be shrunk depends on the size of each data file's contiguous free space starting from the high water mark in the tablespace.
If the user-defined
KEEPsize is smaller than the actual reservable space, the user-defined size takes precedence. Otherwise, the actual reservable space takes precedence.The
SHRINKoperation, if interrupted, may lead to residual data.
Value range:
1Mto8000TPUNCH { SIZE integer [ K | M | G ] }: Punches idle pages in the tablespace.The encrypted, temporary, UNDO, and default tablespaces cannot be punched.
Punched pages cannot be reused by the original tablespace.
Value range:
1Mto500GAUTOOFFLINE { ON | OFF }: Enables or disables the automatic offline function for the tablespace.For a tablespace with
AUTOOFFLINEset toON, during database startup, any file open failure will cause the tablespace to be automatically taken offline. However, after startup, such exceptions will not trigger automatic offline.For a tablespace with
AUTOOFFLINEset toON, if a file is damaged or other faults occur during database startup, you can load the database to theMOUNTstate. IfAUTOOFFLINEis set toOFF, the database cannot be started when a file in the tablespace is damaged or other faults occur.
AUTOEXTEND { OFF | ON [ NEXT integer [ K | M | G ] ] [ MAXSIZE { integer [ K | M | G ] | UNLIMITED }] }: Sets the automatic extension attribute for the tablespace or data file.If
AUTOEXTENDis not specified orAUTOEXTENDis set toOFF, automatic extension is disabled by default.If
AUTOEXTENDis set toON, the following attributes can be configured:NEXTindicates the extension size. If it is not specified, the default value 16 MB is used.MAXSIZEindicates the extension upper limit. If it is not set or is set toUNLIMITED, the extension upper limit for the UNDO tablespace is 32 GB, and that for other tablespaces is 8 TB. The user-specified upper limit cannot exceed this range.If both
MAXSIZEandNEXTare set, the value ofMAXSIZEmust be no less than that ofNEXT.
ADD DATAFILE { 'file_name' SIZE integer [ K | M | G ] [COMPRESS] [AUTOEXTEND ...] [ segments integer ] }: Adds a data file to the tablespace.file_nameindicates the data file name. If a relative path is specified, the file is stored in thedatadirectory under the data directory by default.COMPRESSspecifies the new data file as a compressed file. A compressed file is used to store tables with compression attributes. When the table compression feature is used, a compressed file needs to be created in the corresponding tablespace.For details about
AUTOEXTEND, see the preceding description.segments integerindicates the number of segments to be extended. TheSEGMENTSclause can only be used for the UNDO tablespace in theRESTRICTEDmode. When it is used, only one data file can be added at a time. The lower limit of theSEGMENTSclause is1. Before the value of_UNDO_SEGMENTSreaches the upper limit1024, the upper limit of the clause is the value of1024minus the value of_UNDO_SEGMENTS. If the value of_UNDO_SEGMENTSis already1024, extension cannot be performed. TheSEGMENTSclause is a remedial measure used in theRESTRICTEDmode. It involves multiple resource persistence steps. Therefore, the atomicity of operations cannot be guaranteed during the entire process. If an exception occurs when theSEGMENTSclause is used, you need to handle it as required.The mechanism of UNDO SEGMENTS extension is incompatible with that of UNDO SPACE switching. Therefore, if UNDO SEGMENTS extension is performed before UNDO SPACE switching, an error is reported.
RENAME DATAFILE 'old_file_name' TO 'new_file_name': Renames a data file in the tablespace.
Examples
- Add data files to the tablespace
tbs_student.
-- Create the tablespace tbs_student.
CREATE TABLESPACE tbs_student DATAFILE '-dfile_tbs_01' SIZE 32M AUTOEXTEND ON NEXT 10M;
-- Add the data files my_datafile (size: 32M), manager_dfile (size: 32M), and section_dfile (size: 32M) to the tablespace tbs_student.
ALTER TABLESPACE tbs_student ADD DATAFILE '-my_datafile' SIZE 32M, 'manager_dfile' SIZE 32M, 'section_dfile' SIZE 32M;- Delete the data file
manager_dfilefrom the tablespacetbs_student.
ALTER TABLESPACE tbs_student DROP DATAFILE 'manager_dfile';- Rename the data file
my_datafilein the tablespacetbs_studenttonew_my_datafilein theOPEN RESTRICTEDstate.
-- Rename the data file my_datafile in the tablespace tbs_student to new_my_datafile in the OPEN RESTRICTED state.
ALTER TABLESPACE tbs_student RENAME DATAFILE 'my_datafile' TO 'new_my_datafile';- Enable automatic extension for the tablespace
tbs_studentso that the tablespace can be automatically expanded when it is full. You can manually specify the extension size.
ALTER TABLESPACE tbs_student AUTOEXTEND ON NEXT 5M;- Rename the tablespace
tbs_studenttodata_tbs_student.
ALTER TABLESPACE tbs_student RENAME TO data_tbs_student;