oracle表空间使用2。Oracle磁盘管理中最高的逻辑层是表空间。必须在Oracle11g中创建的四个表空间是system、sysaux、temp和undotbs1。2系统:存储oracle表空间概念...
2表空间Oracle磁盘管理中的最高逻辑层是表空间,Oracle11g中必须创建的4个表空间是SYSTEM,SYSAUX,TEMP,UNDOTBS1。
2SYSTEM:存储数据字典等,pl/sql代码等。
2SYSAUX:存储与数据库选项相关的数据2TEMP:用于大的排序操作2UNDUTBS1:为读一致性和恢复的目的,存储事务信息。
表空间的下一层是段,一个段只能驻留在一个表空间中;一个或多个区可以组成一个段,每个区只能驻留在一个数据文件中;一组连续的数据块可以组成一个区。
如果要查询表空间与对应的数据文件的相关信息,可以从dba_data_files数据字典中查询表空间及其包含的数据文件,举例如下:SQLcoltablespace_namefora10;SQLcolfile_namefora50;SQLcolbytesfor999,999,999;SQLSelecttablespace_name,file_name,bytesfromdba_data_filesorderbytablespace_name;1、SYSTEM表空间SYSTEM表空间存放内部数据和数据字典,主要存放SYS用户的各个对象和其他用户的少量对象。
例如:查询USERS表空间中存放的数据对象及其类型和拥有者。
SQLcolownerfora10;SQLcolsegment_namefora30;SQLcolsegment_typefora20;SQLselectsegment_type,segment_name,ownerfromdba_segmentswheretablespace_name='USERS';2、SYSAUX表空间SYSAUX表空间充当SYSTEM表空间的辅助表空间,主要用于存储除数据字典以外的其他数据对象。
例如,查询SYSAUX表空间所存放的用户及其所拥有的对象数量:Selectowneras用户,count(segment_name)as对象数量fromdba_segmentswheretablespace_name='SYSAUX'groupbyowner;3、创建表空间创建表空间的语法如下:Create[smallfile|bigfile]tablespacetablespace_nameDatafile'/path/filename'sizenum[k|m]reuse['/path/filename'sizenum[k|m]reuse][,][autoextend[on|off]next]num[k|m][maxsize[unlimited|num[k|m]]][mininumextentnum[k|m]][defaultstoragestorage][online|offline][logging|nologging][permanent|temporary][extentmanagementdictionary|local[autoallocate|uniformsizenum[k|m]]];说明:?smallfile|bigfile:表示创建的是小文件表空间还是大文件表空间?autoextend[on|off]next:表示数据文件为自动扩展或非自动扩展,如为自动扩展则需要设置next的值。
?maxsize:表示数据文件自动扩展时,允许数据文件扩展的最大长度字节数,如果指定unlimited关键字,则不需要指定字节长度。
?minimumextent:指出在表空间的extent的最小值,这个参数可以减少空间碎片,保证在表空间的extent是这个数值的整数倍。
?online|offline:创建表空间时可以指定为在线或离线。
?permanent|temporary:指定创建表空间是永久表空间或临时表空间。
默认为永久表空间。
?logging|nologging:指定该表空间内的表在加载数据时是否产生日志,默认为产生日志,即使设定为nologging,但在进行insert,update,delete操作时,oracle仍会将信息记录到redologbuffer中。
?extentmanagementdictionary|local:指定表空间的扩展方式是使用数据字典管理还是本地化管理。
默认为本地化管理。
?autoallocate|uniformsize:如果采用本地化管理,在表空间扩展时,指定每次区的扩展大小是系统自动指定还是按照同等大小进行。
如果设定uniform关键字,默认扩展大小为1MB。
?reuse:表示如果该文件存在,则清除该文件再重建该文件;若文件不存在,则创建该文件。
?defaultstorage:设定以后要创建的表、索引、簇的存储参数值。
4、删除表空间?删除空的表空间,但是不包含物理文件droptablespacetablespace_name;?删除非空表空间,但是不包含物理文件droptablespacetablespace_nameincludingcontents;?删除空表空间,包含物理文件droptablespacetablespace_nameincludingdatafiles;?删除非空表空间,包含物理文件droptablespacetablespace_nameincludingcontentsanddatafiles;?如果其他表空间中的表有外键等约束关联到了本表空间中的表的字段,就要加上CASCADECONSTRAINTSdroptablespacetablespace_nameincludingcontentsanddatafilesCASCADECONSTRAINTS;5、案例?创建表空间,然后删除该表空间。
CreatetablespaceexampletbDatafile'E:examp01.dbf'size5Mautoextendonnext128kmaxsize1000m,'E:examp02.dbf'size5Mautoextendonnext128kmaxsize1000m;说明:以上例子创建的表空间由examp01.dbf和examp02.dbf两个文件组成。
创建完成后,可以发现在相应路径下增加了2个文件。
droptablespaceexampletb;?创建表空间和表,然后删除该表空间。
CreatetablespaceexampletbDatafile'E:examp01.dbf'size5Mautoextendonnext128kmaxsize1000mreuse,'E:examp02.dbf'size5Mautoextendonnext128kmaxsize1000mreuse;createtablescott.student(idnumber,nameVARCHAR2(10))tablespaceexampletb;说明:向student表插入数据时,数据将存储在表空间exampletb中,而exampletb表空间拥有一个或多个数据文件,所以student数据最终存储到examp01和examp02的数据文件中。
droptablespaceexampletbincludingcontents;?创建表空间,然后删除该表空间及数据文件。
CreatetablespaceexampletbDatafile'E:examp01.dbf'size5Mautoextendonnext128kmaxsize1000mreuse,'E:examp02.dbf'size5Mautoextendonnext128kmaxsize1000mreuse;droptablespaceexampletbincludingdatafiles;?创建表空间和表,然后删除该表空间及数据文件。
CreatetablespaceexampletbDatafile'E:examp01.dbf'size5Mautoextendonnext128kmaxsize1000m,'E:examp02.dbf'size5Mautoextendonnext128kmaxsize1000m;createtablescott.student(idnumber,nameVARCHAR2(10))tablespaceexampletb;droptablespaceexampletbincludingcontentsanddatafiles;说明:如果droptablespace语句中含有datafiles,那datafiles之前必须有contents关键字,不然会提示错误。
?创建两个表空间,分别在其中创建主码表和外码表,然后删除包含主码表的表空间及数据文件。
Createtablespaceexampletb1Datafile'E:examp03.dbf'size5Mautoextendonnext128kmaxsize1000m;Createtablespaceexampletb2Datafile'E:examp02.dbf'size5Mautoextendonnext128kmaxsize1000m;createtabletest1(mobilenumber(13)primarykey)tablespaceexampletb1;createtabletest2(mobilenumber(13)referencestest1(mobile))tablespaceexampletb2;droptablespaceexampletb1includingcontentsanddatafilescascadeconstraints;