Sunteți pe pagina 1din 30

Chapter 6 Creating and Managing Tablespaces

As discussed in Chapter 3, Oracle assigns all of its data to tablespaces. Tablespaces are like folders that contain segments (table, index, rollback, temporary). Any object you create, such as a table, must be assigned to some existing tablespace. To Create, Alter, or Drop a tablespace, you must access an account (such as System) that has these permissions. The following statement creates a tablespace named DATA and attaches a single data file to it. The data file data_01.dbf is created in the path specified. The data file has a size of 2 Megabytes. Reuse means that Oracle will reuse a file that has been detached from the database if its size is exactly 2 Megabyes. Any segment created in this tablespace will be assigned the storage parameters indicted unless they are changed when the object is created. Table 1 describes the default storage parameters.
SQL> 2 3 4 5 6 Create Tablespace Data Datafile '/oraclec/data/DB08/disk1/data_01.dbf' Size 2M Reuse Default Storage ( Initial 32K Next 32K Minextents 2 Maxextents 121 PctIncrease 0);

Tablespace created.

Table 1 Default Storage Parameters


Parameter
Initial

Description
The first extent created will be of this size. It should be defined as a multiple of the db_block_size. All segments must contain at least this one extent. The second extent created will be of this size. The number of extents created when the segment is first created. For example, if this value is set to 2, an extent of the size specified in Initial plus an extent of the size specified in Next is created when the segment is created. The maximum number of extents that can be assigned to the segment. If all extents fill with data and this parameter is reached, a cannot extend error will be returned. If no value is specified, the default value of 121 will be enforced. If you try to provide a value greater than 121 for this parameter, it will default back to 121. Percent Increase allows your segment to grow at an increasing rate. The first two extents will be of a size determined by the Initial and Next parameters. The third extent will be 1 + PCTINCREASE/100 times the second extent. The fourth extent will be 1 + PCTINCREASE/100 times the third extent, etc. PctIncrease can be set at any integer between 0 and 100. If not specified, the default value will be 50.

Next Minextents

Maxextents

PctIncrease

Chapter 6 Tablespaces and Users

Page 2

Table 2 illustrates the growth of a segment with various PctIncrease settings. Extent size means the size of each individual extent. Segment size is the total size of the segment (the sum of all the extents). The table assumes that both the Initial and Next parameters are 32K. Setting PctIncrease to zero will produce uniform extent sizes for all extents except possibly the initial extent. However, if PctIncrease is set to zero, SMON will not automatically coalesce free space. Setting the PctIncrease to any positive value in the default storage parameters of the tablespace will direct SMON to coalesce free space.

Table 2 Table Growth And PctIncrease


PctIncrease = 25
Extents First Second Third Fourth Fifth Extent Size 32K 32K 40K 50K 62.5K Segment Size 32K 64K 104K 154K 216.5K

PctIncrease = 50
Extent Size 32K 32K 48K 72K 108K Segment Size 32K 64K 112K 184K 292K

PctIncrease = 75
Extent Size 32K 32K 56K 98K 171.5K Segment Size 32K 64K 120K 218K 389.5K

Another tablespace, called INDEXES, is created below. The INDEXES tablespace is defined with two data files. Since indexes should be located on a different disk from the data, if possible, the data files for INDEXES are located on disks 3 and 4. When multiple files are defined in the Create Tablespace command, the Datafile keyword is used only once. The names of the data files are separated by commas as shown.
SQL> 2 3 4 5 Create Tablespace INDEXES Datafile '/oraclec/data/DB08/disk3/indexes_01.dbf' Size 2M Reuse, '/oraclec/data/DB08/disk4/indexes_02.dbf' Size 2M Reuse Default Storage ( Initial 32K Next 32K Minextents 2 Maxextents 121 PctIncrease 0);

Tablespace created.

You can view information concerning your tablespaces through the DBA_Tablespaces view. A description of the columns in this view not described above is shown in the table below followed by the information returned when all data are retrieved through the view. Column Name
MIN_EXTLEN

Description
This is the minimum extent size that may be created by any segment in the tablespace. This can be AVAILABLE or OFFLINE. There will be several situations when a tablespace would be taken offline. Note that when the tablespace is created, it is immediately brought online and is available for use.

STATUS

Chapter 6 Tablespaces and Users

Page 3

CONTENTS

This can be PERMANENT or TEMPORARY. Use Temporary if the tablespace is to be used by Oracle for sorts. You cant create objects in a temporary tablespace. LOGGING indicates that activity on the segments in this tablespace are recorded in the REDO log. NOLOGGING indicates activity is not recorded.

LOGGING

SQL> select * from dba_tablespaces;


TABLESPACE INITIAL NEXT MIN MAX PCT NAME EXTENT EXTENT EXTENTS EXTENTS INCREASE ---------- ------- -------- ------- ------- -------SYSTEM 10240 10240 1 121 50 RBS 131072 131072 2 50 0 TEMP 512000 512000 1 121 0 TOOLS 10240 10240 1 121 50 USERS 10240 10240 1 121 50 DATA 32768 32768 2 121 0 INDEXES 32768 32768 2 121 0 MIN EXTLEN -----0 0 0 0 0 0 0 STATUS --------ONLINE ONLINE ONLINE ONLINE ONLINE ONLINE ONLINE CONTENTS --------PERMANENT PERMANENT PERMANENT PERMANENT PERMANENT PERMANENT PERMANENT LOGGING ------LOGGING LOGGING LOGGING LOGGING LOGGING LOGGING LOGGING

The rows highlighted in the results above show the DATA and INDEXES tablespaces that were just created. The Minextents, Maxextents, and PctIncrease parameters reflect those values identified in the DEFAULT STORAGE clause of the CREATE TABLESPACE statement. The INITIAL and NEXT parameters are doubled since the minextents was set to 2. The following view provides information on the data files that Oracle owns. These data files are associated with tablespaces and were created when the tablespace was created or altered. The result of the query below indicates the full name and path of each data file and the tablespace that owns it. The size of each data file in both bytes and blocks is provided along with the current status of the data file. The data files that were created earlier in this chapter are highlighted.
SQL> select file_name, tablespace_name, bytes, blocks, status 2 from dba_data_files 3 order by tablespace_name;
FILE_NAME -------------------------------------/oraclec/data/DB08/disk1/data_01.dbf /oraclec/data/DB08/disk3/indexes_01.dbf /oraclec/data/DB08/disk4/indexes_02.dbf /oraclec/data/DB08/disk1/indx01.dbf /oraclec/data/DB08/disk1/rbs01.dbf /oraclec/data/DB08/disk1/system01.dbf /oraclec/data/DB08/disk1/temp01.dbf /oraclec/data/DB08/disk1/tools01.dbf /oraclec/data/DB08/disk1/users01.dbf 9 rows selected. TABLESPACE_NAME BYTES BLOCKS STATUS --------------- --------- ------ --------DATA 2097152 256 AVAILABLE INDEXES 2097152 256 AVAILABLE INDEXES 2097152 256 AVAILABLE INDX 5242880 640 AVAILABLE RBS 20971520 2560 AVAILABLE SYSTEM 104857600 12800 AVAILABLE TEMP 10485760 1280 AVAILABLE TOOLS 8388608 1024 AVAILABLE USERS 5242880 640 AVAILABLE

Locally Managed Tablespaces When you create a tablespace as shown in the section above, the tablespace is dictionary-managed. All information concerning the structure and parameters of the

Chapter 6 Tablespaces and Users

Page 4

tablespace is stored in the data dictionary. The data dictionary must be updated every time a segment acquires a new extent. Rollback information and Redo log information on this update are generated. Oracle8i and later versions allow a tablespace to be locally managed. The information regarding the tablespace is not recorded in the data dictionary. In a locally managed tablespace, a bitmap index is created in the headers of the data files associated with the tablespace. Each bit of the index is associated with a database block located in the file. The bit indicates if the block is used or free. Oracle uses the bitmap indexes to decide where to place the newly acquired extents. Since the information on the locally managed tablespace is not stored in the data dictionary, no Redo Log or Rollback information is generated for changes to the tablespace. (Redo Log and Rollback information is still generated for changes to the data in the tablespace). Locally managed tablespaces are more efficient than data dictionary-managed tablespaces, especially when the tablespace contains a large number of extents and has a high level of activity. There is no need to coalesce the tablespace because the bitmap index handles the management of space. However, locally managed tablespaces require uniform extent sizes. The extent size is determined at the time of tablespace creation and cannot be overwritten in the storage clause of the tables. Storage parameters such as Next, Minextents, Maxextents, and PctIncrease do not apply to any object residing in the tablespace. A value may be determined for Initial but it must be a multiple of the Uniform Size set for the tablespace. For example, if you are creating a table and enter a value of 40K for Initial, the number of extents of the uniform size will be acquired to cover 40K. In the example below with a 32K uniform size, two extents will be acquired. The statement below creates a locally managed tablespace named DATA2 and associates one file with the tablespace. There is no default storage clause. All extents will be sized at 32K.
SQL> Create Tablespace DATA2 2 Datafile '/oraclec/data/DB08/disk1/data2_01.dbf' Size 4M Reuse 3 Extent Management Local Uniform Size 32K; Tablespace created.

The query below shows that DATA2 is locally managed and that the extents are allocated in a uniform manner.
SQL> Select Tablespace_Name, 2 Extent_Management, 3 Allocation_Type 4 From DBA_Tablespaces; TABLESPACE_NAME -----------------------------SYSTEM TOOLS RBS EXTENT_MAN ---------DICTIONARY DICTIONARY DICTIONARY ALLOCATIO --------USER USER USER

Chapter 6 Tablespaces and Users

Page 5

TEMP USERS INDX DATA INDEXES DATA2 9 rows selected.

DICTIONARY DICTIONARY DICTIONARY DICTIONARY DICTIONARY LOCAL

USER USER USER USER USER UNIFORM

Modifying and Removing Tablespaces Use the Alter Tablespace command to add one or more data files to a tablespace. The first example below adds a 4 megabyte data file to the DATA tablespace and places it on disk 1. The second example adds a 2 megabyte data file to the DATA2 tablespace and places it on disk 2.
SQL> Alter Tablespace DATA 2 Add Datafile '/oraclec/data/DB08/disk1/data_02.dbf' Size 4M Reuse; Tablespace altered.

SQL> Alter Tablespace DATA2 2 Add Datafile '/oraclec/data/DB08/disk2/data2_02.dbf' Size 2M Reuse; Tablespace altered.

Relocating a Data File Sometimes you may need to move a data file from one location to another. This may occur because you have more resources available to you or you believe placing the file in another location would lead to greater efficiency. Moving a data file is a four step process as outlined below.
Take the tablespace offline so no one can access the data while the file is being moved. Use the operating system command to move the file from one location to another. If you are using unix, remember that file names are case sensitive. Also, dont press enter in the middle of a file name or quoted string. Rename the data file in Oracle. Bring the tablespace back online so its data can be accessed.

Figure 1 illustrates the commands required to move the data file added to the DATA tablespace earlier in this section to disk 2.

Chapter 6 Tablespaces and Users

Page 6

Figure 1 Moving a Tablespaces Data File

Detecting Fragmentation As data are added to segments, the segments must eventually acquire new extents. Oracle places these extents at locations in the data file according to the following rules:
Oracle first scans the disk and tries to find an exact fit. Oracle next scans the disk looking for a best fit. If Oracle doesnt have enough contiguous space to acquire the extent, SMON coalesces the free space if the PctIncrease value in the tablespaces default storage clause exceeds zero. The contiguous space is acquired for the extent, if possible. If the space isnt available, a Cannot Extent error is generated.

When extensive activity occurs on the segments in the tablespace, the disk will become fragmented as shown in Figure 2. The Data Extents (DE) associated with the tables indicated are interrupted by segments of Free Space (FS).

Figure 2 Fragmented Data File


DE Emp FS DE Dept DE Emp FS FS FS DE Emp FS FS

DE Dept

Coalescing the tablespace will merge the contiguous segments of free space into a single segment of free space. Coalescing does not change the location of the actual data. As shown in Figure 3, fragmentation still exists after the tablespace is coalesced. The exp

Chapter 6 Tablespaces and Users

Page 7

and imp utility programs described in Chapter 10 may be used to reorganize the disk. Extents of tables may be located contiguously and all segments of free space may be merged into a single large segment using exp and imp.

Figure 3 Coalesced Data File


DE Emp FS DE Dept DE Emp FS FS DE Emp DE Dept FS

To examine the fragmentation on the tablespaces, use the dba_free_space view. The following view from DBA_Free_Space shows the amount of free space that is available on each tablespace. The results provide the tablespace name along with the file identifier so one could join the tablespace (logical object) to its file(s) (physical objects). You can see that SYSTEM occurs in two rows, RBS two rows, TEMP one row, DATA three rows, DATA2 two rows, and INDEXES two rows. If a tablespace is listed more than once, then the tablespace contains fragments of free space. The highlighted DATA tablespace has three fragments of free space of sizes (in blocks) 211, 12, and 511. The sum of all of the fragments represents the total free space in the tablespace. The Blocks column simply indicates the size of free space segments in database blocks instead of bytes.
Select * From DBA_Free_Space Order By Tablespace_Name; TABLESPACE_NAME FILE_ID BLOCK_ID BYTES BLOCKS RELATIVE_FNO ---------------- ------- -------- ---------- ------ -----------DATA 7 46 1728512 211 7 DATA 7 18 98304 12 7 DATA 11 2 4186112 511 11 DATA2 12 13 1998848 244 12 DATA2 10 17 4063232 496 10 INDEXES 8 6 2056192 251 8 INDEXES 9 6 2056192 251 9 INDX 6 2 5234688 639 6 RBS 3 642 15720448 1919 3 RBS 3 450 1048576 128 3 SYSTEM 1 6979 47693824 5822 1 SYSTEM 1 6971 65536 8 1 TEMP 4 2 10477568 1279 4 TOOLS 2 18 32768 4 2 TOOLS 2 10 32768 4 2 TOOLS 2 22 8216576 1003 2 TOOLS 2 14 32768 4 2 TOOLS 2 6 32768 4 2 TOOLS 2 2 32768 4 2 USERS 5 2 5234688 639 5 20 rows selected.

Chapter 6 Tablespaces and Users

Page 8

The results from the query below show the total amount of free space in each tablespace without regard to fragmentation.

SQL> select tablespace_name, sum(bytes) as bytesfree 2 from dba_free_space 3 group by tablespace_name; TABLESPACE_NAME BYTESFREE ------------------------------ ---------DATA 6012928 DATA2 6094848 INDEXES 4177920 INDX 5234688 RBS 16769024 TEMP 10477568 TOOLS 8380416 USERS 5234688 8 rows selected.

To remove the contiguous free space segments, coalesce the tablespace as shown in the example below.
SQL> Alter Tablespace DATA Coalesce; Tablespace altered.

Read Only Tablespaces To prevent data from being changed in any segment in a tablespace, alter the tablespace to read only. As shown in Figure 4, tables in read only tablespaces cannot be updated and new rows cannot be added.

Chapter 6 Tablespaces and Users

Page 9

Figure 4 Read Only Tablespaces

To allow modifications to data, use the following SQL command.


SQL> Alter Tablespace DATA Read Write; Tablespace altered.

Resizing a Data File Once a data file has been created, its size may be modified with the alter database command. As long as disk space is available, the data file may be increased in size. However, to shrink a data file, the file must be empty. To shrink a data file, you must temporality remove the objects from the file, shrink the file, and reload the objects. The command below illustrates resizing a data file.
SQL> Alter Database DB08 2 Datafile '/oraclec/data/DB08/disk2/data_02.dbf' Resize 8M; Database altered.

Removing Data Files and Tablespaces If a data file is no longer needed, it may be dropped as shown below. The data file does not need to be empty to drop it. Even though the file will no longer be recognized by the database, it still exists. Use the operating system command (rm in unix) to remove the file.
SQL> Alter Database DB08 2 Datafile '/oraclec/data/DB08/disk2/data2_02.dbf' Offline Drop; Database altered.

Chapter 6 Tablespaces and Users

Page 10

A tablespace may be dropped with the Drop Tablespace command. The command below drops a hypothetical tablespace called Data3.
SQL> Drop Tablespace Data3; Tablespace dropped.

If the tablespace contains any objects, you must add the clause Including Contents as shown below.
SQL> Drop Tablespace DATA3 Including Contents; Tablespace dropped.

If existing tables in another tablespace reference the tables in the tablespace to be dropped, the foreign key constraints must be dropped as well with the command shown below.
SQL> Drop Tablespace DATA3 Including Contents Cascade Constraints; Tablespace dropped.

Remember that dropping the tablespace detaches the files from the database. However, the files still exist. The files that have been detached should be removed with the operating system command.

Managing Users
A User account must be created for anyone who needs to access the database. The User becomes a system object. When users are created, they must be given an initial password. Normally users are assigned a default tablespace and given a quota on that tablespace. Users may also be assigned a temporary tablespace. Creating Users To create a user, connect to the SYSTEM account and enter the CREATE USER command shown in Figure 5. The first line of the create user command requires you to define a name. In this case, the user will be called Elaine. The IDENTIFIED BY clause requires you to provide the user with a password. Elaines password will be welcome. Neither the name nor the password are case sensitive. Elaine will be able to log in as ELAINE/WELCOME or Elaine/Welcome or any other combination of upper and lower case letters. The DEFAULT TABLESPACE is the tablespace where all of Elaines tables and indexes will be stored automatically if she doesnt specify another tablespace. The tablespace must exist or Oracle will generate an error. The TEMPORARY TABLESPACE must also exist and was likely created during initial database creation. Quotas may be provided on the tablespaces. In the example, Elaine has a 5M quota on the DATA tablespace and an unlimited quota on the TEMP tablespace.

Chapter 6 Tablespaces and Users

Page 11

Even though the Elaine user is created, she will not be allowed to access Oracle since she has no permissions on the database.

Figure 5 Creating a User

Granting the connect and resource roles to Elaine gives her a number of permissions on the database. Well examine these roles in detail in Chapter 9. Not only can Elaine access Oracle, she can create tables, indexes, views, triggers, procedures, and many other objects. The roles are granted in Figure 6 and Elaine is connected.

Figure 6 Granting Connect and Resource Roles

Chapter 6 Tablespaces and Users

Page 12

In Figure 7, a user named George is created with a password of welcome and a default tablespace of DATA. A 5M quota is placed on the tablespace DATA. The final line of the CREATE USER statement contains the clause PASSWORD EXPIRE. PASSWORD EXPIRE will require George to change his password the first time he connects to Oracle as shown. Note that the password he enters doesnt display on the screen.

Figure 7 Creating A User With Password Expire

Creating Users Through a Script File An efficient method for creating users is to develop a script file in the operating system and access it from within SQL. Exit from SQL and create a file in your home directory called newuser.sql. In that file enter the commands to create a user replacing the users name with &1. Enter the command to grant connect and resource to a user again replacing the users name with &1. The file should look something like the following:
Create User &1 Identified By welcome Default Tablespace Data Quota 5M on Data Temporary Tablespace Temp Quota Unlimited on Temp; Grant Connect, Resource to &1;

Note that you must place a semicolon after the Quota Unlimited on Temp line and again after the Grant statement. Save this file. Connect to Oracle as SYSTEM and enter the following:
SQL> @newuser jerry

Chapter 6 Tablespaces and Users

Page 13

Oracle will run your script file replacing &1 with jerry. Note that the file is called newuser.sql but you dont need to use the .sql part when calling the file. By simply retyping the command above and changing the name from jerry to Kramer, you can create the Kramer user. A user may be granted privileges on the database to perform DBA functions. This user will be created by the script file from above and then be granted the DBA role in addition to the Connect and Resource roles. A user, named Superman, is created and given DBA privileges in the two statements below.
SQL> @newuser Superman SQL> Grant DBA To Superman;

Retrieving Information on Users You can view information on users through the dba_users view as shown below.
SQL> select 2 username, user_id, password, account_status, lock_date, expiry_date, 3 default_tablespace, temporary_tablespace, created, profile 4 from dba_users order by username;
USERNAME USER ID -------- ---DBSNMP 16 ELAINE 30 GEORGE 31 JERRY 27 KRAMER 32 OUTLN 11 SUPERMAN 26 SYS 0 SYSTEM 5 TRACESVR 19 ACCOUNT LOCK EXPIRY STATUS DATE DATE ----------------- ------- ---- -----E066D214D5421CCC OPEN 092CA7BCF360A117 OPEN 666D4EF49E031243 OPEN 88F7D29BBCA21A5E OPEN 621FB93B53107919 OPEN 4A3BA55E08595C81 OPEN 42153BFAF4C2A023 OPEN D4C5016086B2DC6A OPEN D4DF7931AB130E37 OPEN F9DA8977092B7B81 OPEN PASSWORD DEFAULT TABLESPACE ---------SYSTEM DATA DATA DATA DATA SYSTEM DATA SYSTEM TOOLS SYSTEM TEMPORARY TABLESPACE ---------SYSTEM TEMP TEMP TEMP TEMP SYSTEM TEMP TEMP TEMP SYSTEM CREATED --------02-JAN-03 26-MAY-03 01-JUN-03 26-MAY-03 01-JUN-03 02-JAN-03 26-MAY-03 02-JAN-03 02-JAN-03 02-JAN-03 PROFILE ------DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT

Note that the password is encrypted. Even though Jerry, Elaine, and Kramer all have a password of welcome, the encrypted passwords are different. We will discuss the Account Status, Lock Date, Expiry Date, and Profile later. Modifying and Removing Users Its often necessary to change a password for a user account. You can always change your own password. A DBA may change the password for any user. The statement below illustrates changing a password to loislane.
SQL> Alter User Superman Identified By loislane; User Altered.

Use the Alter User command to change quotas or tablespaces or add quotas to tablespaces. The two statements shown below express quotas on the DATA2 and INDEXES tablespaces for Jerry and Elaine.

Chapter 6 Tablespaces and Users

Page 14

SQL> Alter User Jerry Quota 10M On DATA2 Quota Unlimited On INDEXES; User altered. SQL> Alter User Elaine Quota 10M On DATA2 Quota Unlimited On INDEXES; User altered.

If you want to remove a user, say Kramer, from the database, enter:
SQL> Drop User Kramer; User dropped.

The above command will drop the user Kramer but the objects he created in the database will remain. If you want to remove both the user Kramer and the objects he created, enter:
SQL> Drop User Kramer Cascade; User dropped.

Locking User Accounts If you want to prevent Kramer from accessing his account but still leave his account intact, enter the following
SQL> Alter User Kramer Account Lock;

User altered.

Kramer will not be able to access Oracle through his account until you unlock it with the following statement.
SQL> Alter User Kramer Account UnLock;

User altered.

Monitoring Users Sessions You can view the users currently connected to Oracle through the v$session view as shown below.
SQL> select 2 SID, serial#, username 3 from v$session where username is not null; SID SERIAL# USERNAME ---------- ---------- -----------------8 54 SYSTEM 8 rows selected.

Chapter 6 Tablespaces and Users

Page 15

To force off a user, enter the following replacing the SID and serial# with values from above. This command only terminates the session, it does NOT lock the users account.
SQL> Alter System Kill Session (SID,serial#);

Limiting User Resources Profiles are objects that allow the DBA to set limits on various resources. By assigning profiles to users, the resources that users can exhaust is limited. The general form of the Create Profile statement is shown below:
CREATE PROFILE profile_name LIMIT parameter1 value parameter2 value;

After the profile is created, it is assigned to a user as shown below:


ALTER USER user_name PROFILE profile_name;

A partial list of the parameters of a profile is shown in Table 3. There is another set of parameters available that involve password management. The password management parameters will be discussed with security issues in Chapter 9.

Table 3 Profile Parameters


Parameter Value Type
Integer

Description

Sessions_Per_User

Number of concurrent session the user is allowed. If the user connects to other Oracle products such as Oracle * Forms, this value should be set to at least 5. Amount of CPU time used per session.

CPU_Per_Session

Hundredths of a second Hundredths of a second Number of DB Blcoks Number of DB Blcoks Minutes

CPU_Per_Call

Amount of CPU time used per single SQL request.

Logical_Reads_Per_ Session Logical_Reads_Per_ Call Idle_Time

Number of blocks that can be read during a session.

Number of blocks that can be read with a single SQL request. If the user doesnt enter something into SQL for this amount of time, the session will be terminated. Amount of time the user may be connected to the database during a 24 hour period. Limits the size of the private memory that can be used for storing a users private SQL.

Connect_Time

Minutes

Private_SGA_ Per_Session

Number of DB Blcoks

Chapter 6 Tablespaces and Users

Page 16

Composite_Limit

Integer

A composite of the parameters underlined. Even though no individual resource limit is reached, Composite_Limit can impose a limit on the total of the resources used. Set a resource value for each parameter using ALTER RESOURCE COST. For Example, ALTER RESOURCE COST Connect_Time 10; Each minute of connect time will generate a resource use of 10. Set the resource limit for all other parameters. Provide a value for the Composite_Limit.

The following example creates a profile named USER_PROFILE that sets idle time to ten minutes, the cpu that can be used in a session to ten seconds, and the number of blocks that can be read by a single SQL statement to 20.
SQL> 2 3 4 Create Profile USER_PROFILE Limit Idle_Time 10 CPU_Per_Session 1000 Logical_Reads_Per_Call 20;

If no profile is assigned to a user, the user will be assigned the Default Profile. Default will initially have all parameters set to unlimited. You can alter the default profile but not drop it. You can modify any profile with the Alter Profile command. In the example below, the number of sessions per user is limited to 10 in the USER_PROFILE.
SQL> Alter Profile USER_PROFILE Limit Sessions_Per_User 10; Profile altered.

To impose the limits established in the User_Profile on the Jerry account, use the command below.
SQL> alter user jerry profile USER_PROFILE; User altered.

The DBA_Profiles view shows the current values of the profiles parameters. The DEFAULT profile has values set to UNLIMITED. The USER_PROFILE created and altered above limited the four parameters highlighted below. The Resource Type described in this chapter is KERNEL. The PASSWORD Resource Type places restrictions on passwords and will be discussed in Chapter 9.
SQL> Select * From DBA_Profiles 2 Where Resource_Type = 'KERNEL' 3 Order By 1; PROFILE --------------DEFAULT DEFAULT DEFAULT DEFAULT RESOURCE_NAME --------------------------COMPOSITE_LIMIT SESSIONS_PER_USER CPU_PER_CALL LOGICAL_READS_PER_CALL RESOURCE -------KERNEL KERNEL KERNEL KERNEL LIMIT --------UNLIMITED UNLIMITED UNLIMITED UNLIMITED

Chapter 6 Tablespaces and Users

Page 17

DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT USER_PROFILE USER_PROFILE USER_PROFILE USER_PROFILE USER_PROFILE USER_PROFILE USER_PROFILE USER_PROFILE USER_PROFILE 18 rows selected.

CONNECT_TIME PRIVATE_SGA IDLE_TIME LOGICAL_READS_PER_SESSION CPU_PER_SESSION COMPOSITE_LIMIT PRIVATE_SGA SESSIONS_PER_USER CPU_PER_CALL LOGICAL_READS_PER_CALL CONNECT_TIME IDLE_TIME LOGICAL_READS_PER_SESSION CPU_PER_SESSION

KERNEL KERNEL KERNEL KERNEL KERNEL KERNEL KERNEL KERNEL KERNEL KERNEL KERNEL KERNEL KERNEL KERNEL

UNLIMITED UNLIMITED UNLIMITED UNLIMITED UNLIMITED DEFAULT DEFAULT 10 DEFAULT 20 DEFAULT 10 DEFAULT 1000

Managing Segments
If you have been granted permission on the tablespace, you can create a table and assign it to that tablespace. Also, you can write storage parameters in the definition of the table that will replace the ones defined in the CREATE TABLESPACE statement for that table. If you dont write a storage clause or dont change some parameters, the parameters defined for the tablespace will be used. The user Jerry issues the following CREATE TABLE statement. It places the table in the DATA tablespace created earlier in this chapter and changes the PctIncrease and Minextents parameters. Note that you use the keyword STORAGE in line 8 since these are the actual storage values for the table and not default storage for segments in the tablespace.

SQL> Create Table Emp 2 (ID Number(3) Primary Key, 3 Name Varchar2(20) Not Null, 4 Salary Number(6) Not Null, 5 Dept Char(3), 6 Hiredate Date Not Null) 7 Tablespace Data 8 Storage( 9 Minextents 1 PctIncrease 50); Table created.

In the above example, we know the Initial and Next are both 32K and the maxextents is 121 as defined in the CREATE TABLESPACE statement. In the example below, Jerry creates the DEPARTMENT table in the DATA2 tablespace changing the size of the Initial storage parameter. Since DATA2 is a locally managed tablespace, the Next , Minextents, Maxextents, and PctIncrease parameter values are not used. The DEPARTMENT table will be created with two extents each of size 32K.

Chapter 6 Tablespaces and Users

Page 18

SQL> 2 3 4 5 6

Create Table DEPARTMENT (Dept Char(3) Primary Key, DeptName Varchar2(40)) Tablespace DATA2 Storage( Initial 48K);

Table created.

The example below shows the creation of an index on EMPs Dept column. The index is located in the INDEXES tablespace.
SQL> Create Index EMP_DEPT_IDX On EMP(Dept) Tablespace INDEXES; Index created.

The data from Chapter 2 are loaded into the EMP and DEPARTMENT tables through a script file. Managing Space Usage The database block size is determined when the database is initially created and cannot be changed. The segments data are stored in the blocks. The database block size is known to be 8k (use show parameter db_block_size from the SYSTEM account to find this parameters value). Since the file owned by the DATA tablespace is 2M, there are (2M/8K) = 250 blocks available. One block is used by the database to store an index to the other blocks. That leaves 249 blocks available to store user data. Each block also stores some information required by the database to associate the block with a table and store pointers to the rows contained in the block. The space used by the database is identified as overhead in Figure 8. Although the amount of space used by overhead may vary, it is generally considered to be about five percent of the available space in the block. Figure 8 illustrates space usage within a single data block. Values for two parameters may be set to determine how the space is used, PctFree and PctUsed. PctFree is the percent of the space in the block that will be left unused when the data for rows are placed in the block. PctUsed is the percentage of the space in a block that forms a lower threshold necessary for adding data to the block. When data are deleted, the amount of used space in the block decreases. However, new data are not immediately inserted into the block to replace the deleted data. When enough data have been deleted from the block so that the PctUsed threshold has been reached, the blocks id is placed on the freelist. The freelist is a list of block ids indicating the blocks that are available to accept data. New rows inserted into the table may now be placed in the block until the block fills as it did originally. As an example, suppose the PctFree is ten percent and the Pctused is forty percent (the defaults). The block is initially filled until its eight-five percent full (100 (PctFree + Overhead)). Data are deleted from the block. When the level of data in the block falls to forty percent, the block will be placed on the freelist.

Chapter 6 Tablespaces and Users

Page 19

Figure 8 Space Usage in a Data Block

It is necessary to leave unused space in a data block to store additional data added to the rows that exist in the block. Some data types, such as Varchar2, store data in a variable length format. If a null value is later changed to a real value or the length of an existing data value is increased, the data must be stored somewhere in the block. The data will be stored in available free space in the block referenced by a pointer in the datas original location. If there is not enough free space available in the block, the entire row is moved to another block. Moving an entire row from one block to another because there is not enough free space to handle updates is called migration. Because migration reduces database performance, there should be enough free space in the block to handle updates. Setting the PctFree too high is also inefficient since the space is never used. If tables contain static data, set the PctFree to a low value such as five percent. If the table contains data that are occasionally updated, leave the PctFree at ten percent. If the table contains data that change frequently and data values usually increase, set the PctFree to a relatively high value. Normally the PctFree should not be set to a value greater than twenty-five percent. The CUSTOMER table is created in the example below on Elaines account. The value of PctIncrease is changed to twenty-five percent and the table is located in the DATA tablespace. The shaded line illustrates how to set the PctFree and PctUsed values. Note that they are not part of the Storage clause.

Chapter 6 Tablespaces and Users

Page 20

SQL> 2 3 4 5 6 7 8 9

Create Table CUSTOMER (CID Char(2) CName Varchar2(20) Region Char(1), ID Number(3)) Tablespace DATA Storage( PctIncrease 25) PctFree 15 PctUsed 50;

Primary Key, Not Null,

Table created.

The example below creates and index on the CUSTOMERs Region column placing it in the INDEXES tablespace.
SQL> Create Index CUST_REGION_IDX On CUSTOMER(Region) Tablespace INDEXES; Index created.

The Customer table is loaded with the data from Chapter 2 through a script file. Analyzing Tables You can use the Analyze Table command to compute various statistics for a table. Once you analyze the table, the statistics are stored in that table and can be viewed through the USER_Tables or DBA_Tables views. The statistics are not updated dynamically. The values of the statistics are recorded and do not change until another Analyze Table statement is run against the table. You can use these statistics to monitor space usage of the table. Also, the cost-based optimizer described in Chapter 8 uses these statistics to calculate the resource cost of queries. All tables can be analyzed. However, you should not analyze any of the tables owned by SYS. The general form of the Analyze Table command is shown below.
Analyze Table tablename < Compute|Estimate > Statistics < Sample n < Percent|Rows > >

If you Compute the statistics, all of the rows in the table are analyzed. This can be time consuming for tables containing a large number of rows. To save time, you can Estimate the statistics based upon a random sample of rows selected by Oracle. You can choose the size of the sample used to Estimate Statistics by specifying the number of rows or the percent of rows to be used in the sample. However, if you specify a sample size that will cause more than fifty percent of the rows to be included in the sample, all of the rows in the table will be analyzed. Figure 9 below shows that initially no statistics exist in Elaines CUSTOMER table. After Elaine analyzes the table, the statistics can be viewed.

Chapter 6 Tablespaces and Users

Page 21

Figure 9 Computing Statistics

In Figure10, the DEPARTMENT and EMP tables are analyzed from Jerrys account using Estimate Statistics. The DEPARTMENT table is analyzed using the default sample size and the EMP table is analyzed using forty percent of the rows.

Figure 10 Estimating Statistics

Additional statistics on the individual columns can be found in the view shown in Figure 11.

Chapter 6 Tablespaces and Users

Page 22

Figure 11 Viewing Statistics on Columns

The Analyze Table command can also be used to validate the structure of the blocks that contain the tables data. Normally the structure of a block is validated when the block is read into the database buffer cache. The command below validates the structure of the EMP table.
SQL> Analyze Table EMP Validate Structure; Table analyzed.

You can validate the structure of the table and its associated objects such as indexes with the following command.
SQL> Analyze Table DEPARTMENT Validate Structure Cascade; Table analyzed.

Retrieving Information on Tables The DBA_Tables view describes the storage parameters for each table in addition to the tablespace where the table resides and the owner of the table. Two new parameters are also included, InitTrans and MaxTrans. The value for InitTrans represents the space reserved to log one user who is accessing the tables data. The value from MaxTrans represents the maximum number of users who can concurrently access the tables data. The default values for InitTrans and MaxTrans are 1 and 255, respectively, and are rarely changed. The results of the query below show the tables in the database not owned by SYS or SYSTEM. The highlighted rows indicate the tables created above.
SQL> select owner, table_name, tablespace_name, pct_free, 2 pct_used, ini_trans, max_trans, initial_extent, 3 next_extent, min_extents, max_extents, pct_increase 4 from dba_tables 5 where owner not in ('SYS','SYSTEM');

Chapter 6 Tablespaces and Users

Page 23

OWNER ------OUTLN OUTLN JERRY JERRY ELAINE

TABLE NAME ----------OL$ OL$HINTS EMP DEPARTMENT CUSTOMER

TABLESPACE PCT PCT INI MAX INITIAL NEXT MIN MAX PCT NAME FREE USED TRANS TRANS EXTENT EXTENT EXTENTS EXTENTS INCREASE ---------- ---- ----- ---- ----- -------- ------ ------- ------- -------SYSTEM 10 40 1 255 16384 16384 1 505 50 SYSTEM 10 40 1 255 16384 16384 1 505 50 DATA 10 40 1 255 32768 32768 1 121 50 DATA2 10 40 1 255 49152 32768 1 2147483645 0 DATA 15 50 1 255 32768 40960 2 121 25

5 rows selected.

Viewing Information on User Quotas The DBA_TS_Quotas view displays the allocation that each user has on each tablespace. The results of selecting all of the data from this view are shown below.
SQL> Select * From DBA_TS_Quotas Order By Username; TABLESPACE_NAME --------------DATA INDEXES DATA2 TEMP DATA TEMP INDEXES DATA DATA2 TEMP DATA TEMP DATA USERNAME BYTES MAX_BYTES BLOCKS MAX_BLOCKS -------- ------- ---------- ------- ---------ELAINE 131072 5242880 16 640 ELAINE 65536 -1 8 -1 ELAINE 0 10485760 0 1280 ELAINE 0 -1 0 -1 GEORGE 0 5242880 0 640 GEORGE 0 -1 0 -1 JERRY 65536 -1 8 -1 JERRY 98304 5242880 12 640 JERRY 98304 10485760 12 1280 JERRY 0 -1 0 -1 KRAMER 0 5242880 0 640 SUPERMAN 0 -1 0 -1 SUPERMAN 0 5242880 0 640

13 rows selected.

The first two columns identify a unique tablespace, user combination. For example, the first four rows identify Elaines allocation on the four tablespaces she can access. The Bytes column indicates the number of bytes the users objects occupy in the tablespace. The Max_Bytes column indicates the quota that user has on the tablespace. In the first row, Elaine has used 131,072 bytes of her 5,242,880 byte (5M) quota on the DATA tablespace. The 1 under Max_Bytes means the quota is unlimited. The second row of the results shows that Elaines quota on the INDEXES tablespace is unlimited. The last two columns convert the bytes used and quota in bytes to database blocks. With a db_block_size of 8K, divide the number of bytes by 8K to compute the last two columns of the results. The query below presents the data from the DBA_TS_Quotas in another way. The total number of bytes used is computed for each tablespace. The first row of the results below shows that objects residing in the DATA tablespace have used 229,376 bytes.

Chapter 6 Tablespaces and Users

Page 24

SQL> Select 2 3 4 From 5 Group By

Tablespace_Name, SUM(Bytes) As Bytesused SUM(Blocks) As Blocksused DBA_TS_Quotas Tablespace_Name; BYTESUSED BLOCKSUSED ---------- ----------229376 28 98304 12 131072 16 0 0

TABLESPACE_NAME --------------DATA DATA2 INDEXES TEMP 4 rows selected.

Monitoring Space Usage Recalling that the DBA_Free_Space view described earlier in this Chapter provides information on the free space available in each tablespace, we can combine DBA_TS_Quotas with DBA_Free_Space for some useful analysis. The query below joins DBA_Free_Space and DBA_TS_Quotas to calculate the percent of available space used in each tablespace. From the results of the query below, only 2.23 percent of the space in the DATA tablespace is being used.
SQL> 2 3 4 5 6 7 8 Select a.Tablespace_Name, SUM(a.Bytes) As BytesFree, SUM(b.Bytes) As BytesUsed, SUM(b.Bytes)/(SUM(a.Bytes)+SUM(b.Bytes)) * 100 "Percent Used" DBA_Free_Space a, DBA_TS_Quotas b a.Tablespace_Name = b.Tablespace_Name a.Tablespace_Name;

As From Where Group By

TABLESPACE_NAME BYTESFREE BYTESUSED ------------------------ ---------- ---------DATA 30228480 688128 DATA2 12124160 196608 INDEXES 8093696 262144 TEMP 41910272 0

Percent Used -----------2.2257552 1.5957447 3.1372549 0

Data Dictionary Space Usage Views DBA_Segments View The results of the query below provide specific information on the segments. The segments residing on the SYSTEM and TOOLS tablespaces have been filtered out of the results for readability and because we are not interested in the SYSTEM segments at this point. The first row of the results shows that Elaine has created a Table named CUSTOMER in the DATA tablespace. The Elaines CUSTOMER table currently has two extents attached to it and it is allowed to grow to 121 extents.

Chapter 6 Tablespaces and Users

Page 25

The DBA_Segments view is one of the most useful data dictionary views because it provides access to all of the segments on the database. If we need to know information regarding both Tables and Indexes, this view can provide us with that information.
SQL> Select 1 2 3 Owner, Segment_Type, Tablespace_Name, Segment_Name, Extents, Max_Extents From DBA_Segments Where Tablespace_Name NOT IN ('SYSTEM',TOOLS) Order By Owner; SEGMENT_TYPE ------------TABLE INDEX INDEX TABLE TABLE INDEX INDEX INDEX ROLLBACK TABLESPACE_NAME --------------DATA DATA INDEXES DATA DATA2 DATA INDEXES DATA2 RBS SEGMENT_NAME EXTENTS MAX_EXTENTS ------------- ------- ----------CUSTOMER 2 121 SYS_C001043 2 121 CUST_REGION_IDX 2 121 EMP 1 121 DEPARTMENT 2 2147483645 SYS_C001033 2 121 EMP_DEPT_IDX 2 121 SYS_C001034 1 2147483645 RBS0 8 4096

OWNER ------ELAINE ELAINE ELAINE JERRY JERRY JERRY JERRY JERRY SYS

9 rows selected.

The following view from DBA_Segments provides some of the same information as the previous view concerning space usage, but adds some additional elements. The query below shows each tablespace and the bytes the segments occupy; however it also provides information on the segments stored in each tablespace. The first row of the results below indicates that the DATA tablespace contains four segments. The SYSTEM tablespace contains the 445 segments that make up the data dictionary. The RBS tablespace contains only one segment for the nonsystem rollback segment.
SQL> select tablespace_name, count(*) as segments, sum(bytes) as bytes 2 from dba_segments 3 group by tablespace_name; TABLESPACE_NAME SEGMENTS BYTES ------------------------------ ---------- ---------DATA 4 262144 DATA2 2 65536 INDEXES 2 131072 RBS 1 4194304 SYSTEM 445 57090048 6 rows selected.

The follow view of DBA_Segments shows the number of segments owned by each user along with the number of bytes the segments use.
SQL> Select Owner, Count(*) As Segments, Sum(Bytes) As Bytes 2 From DBA_Segments 3 Group By Owner;

Chapter 6 Tablespaces and Users

Page 26

OWNER SEGMENTS BYTES ------------------------------ ---------- ---------ELAINE 3 196608 JERRY 5 262144 OUTLN 5 81920 SYS 389 57794560 SYSTEM 52 3407872

The final view from DBA_Segments shows the number of segments by type of segment and the bytes used by each type.
SQL> Select Segment_Type, Count(*) As Segments, Sum(Bytes) As Bytes 2 From DBA_Segments 3 Group By Segment_Type; SEGMENT_TYPE SEGMENTS BYTES ------------------ ---------- ---------CACHE 1 8192 CLUSTER 9 2940928 INDEX 239 19488768 LOBINDEX 13 655360 LOBSEGMENT 13 688128 ROLLBACK 2 5029888 TABLE 177 32931840 7 rows selected.

DBA_Extents View The DBA_Extents view contains information on each of the extents within a segment. The example below shows the size of each extent in a segment in both bytes and blocks. The highlighted rows depict the extents used by Jerrys DEPARTMENT table. The DEPARTMENT table was created in the DATA2 tablespace using locally managed extents of a uniform 32K extent size. DEPARTMENT was created with an Initial storage parameter of 48K. To satisfy the Initial extent requirement, two uniform extents must be allocated. When using locally managed extents, the uniform extent size cannot be changed by changing the storage parameters.
SQL> Select 2 3 From 4 Order By Owner, Segment_Name, Segment_Type, Tablespace_Name, Extent_Id, Bytes DBA_Extents Where Tablespace_Name != SYSTEM Owner, Segment_Name; SEGMENT_ TYPE --------TABLE TABLE INDEX INDEX INDEX INDEX TABLESPACE EXTENT _NAME _ID BYTES BLOCKS ---------- ------- ------- ------DATA 0 32768 4 DATA 1 32768 4 INDEXES 0 32768 4 INDEXES 1 32768 4 DATA 1 32768 4 DATA 0 32768 4

OWNER -------ELAINE ELAINE ELAINE ELAINE ELAINE ELAINE

SEGMENT_NAME --------------CUSTOMER CUSTOMER CUST_REGION_IDX CUST_REGION_IDX SYS_C001043 SYS_C001043

Chapter 6 Tablespaces and Users

Page 27

JERRY JERRY JERRY JERRY JERRY JERRY JERRY JERRY SYS SYS SYS SYS SYS SYS SYS SYS

EMP SYS_C001054 DEPARTMENT DEPARTMENT EMP_DEPT_IDX EMP_DEPT_IDX SYS_C001055 SYS_C001054 RBS0 RBS0 RBS0 RBS0 RBS0 RBS0 RBS0 RBS0

TABLE INDEX TABLE TABLE INDEX INDEX INDEX INDEX ROLLBACK ROLLBACK ROLLBACK ROLLBACK ROLLBACK ROLLBACK ROLLBACK ROLLBACK

DATA DATA DATA2 DATA2 INDEXES INDEXES DATA2 DATA RBS RBS RBS RBS RBS RBS RBS RBS

0 1 0 1 0 1 0 0 0 1 2 3 4 5 6 7

32768 32768 32768 32768 32768 32768 32768 32768 524288 524288 524288 524288 524288 524288 524288 524288

4 4 4 4 4 4 4 4 64 64 64 64 64 64 64 64

22 rows selected.

The example below illustrates joining DBA_Extents with DBA_Data_Files to identify the file where each extent is stored. The highlighted rows in the results below show that the CUSTOMER and DEPARTMENT tables have each acquired two extents and the file where each extent is located.
SQL> 2 3 4 5 Select From Where And Order By Owner, Segment_Name, Extent_ID, Segment_Type,File_Name DBA_Extents a, DBA_Data_Files b a.File_ID = b.File_ID a.Tablespace_Name != 'SYSTEM' Owner, Segment_Type;

EXTENT SEGMENT OWNER SEGMENT_NAME _ID _TYPE FILE_NAME --------- --------------- ------ --------- --------------------------------------ELAINE ELAINE ELAINE ELAINE ELAINE ELAINE JERRY JERRY JERRY JERRY JERRY JERRY JERRY JERRY SYS SYS SYS SYS SYS SYS SYS SYS CUSTOMER CUSTOMER SYS_C001043 SYS_C001043 CUST_REGION_IDX CUST_REGION_IDX EMP DEPARTMENT DEPARTMENT EMP_DEPT_IDX EMP_DEPT_IDX SYS_C001054 SYS_C001054 SYS_C001055 RBS0 RBS0 RBS0 RBS0 RBS0 RBS0 RBS0 RBS0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 1 2 3 4 5 6 7 TABLE TABLE INDEX INDEX INDEX INDEX TABLE TABLE TABLE INDEX INDEX INDEX INDEX INDEX ROLLBACK ROLLBACK ROLLBACK ROLLBACK ROLLBACK ROLLBACK ROLLBACK ROLLBACK /oraclec/data/DB08/disk1/data_01.dbf /oraclec/data/DB08/disk1/data_01.dbf /oraclec/data/DB08/disk1/data_01.dbf /oraclec/data/DB08/disk1/data_01.dbf /oraclec/data/DB08/disk3/indexes_01.dbf /oraclec/data/DB08/disk4/indexes_02.dbf /oraclec/data/DB08/disk1/data_01.dbf /oraclec/data/DB08/disk1/data2_01.dbf /oraclec/data/DB08/disk2/data2_02.dbf /oraclec/data/DB08/disk3/indexes_01.dbf /oraclec/data/DB08/disk4/indexes_02.dbf /oraclec/data/DB08/disk1/data_01.dbf /oraclec/data/DB08/disk1/data_02.dbf /oraclec/data/DB08/disk1/data2_01.dbf /oraclec/data/DB08/disk1/rbs01.dbf /oraclec/data/DB08/disk1/rbs01.dbf /oraclec/data/DB08/disk1/rbs01.dbf /oraclec/data/DB08/disk1/rbs01.dbf /oraclec/data/DB08/disk1/rbs01.dbf /oraclec/data/DB08/disk1/rbs01.dbf /oraclec/data/DB08/disk1/rbs01.dbf /oraclec/data/DB08/disk1/rbs01.dbf

22 rows selected.

Chapter 6 Tablespaces and Users

Page 28

DBA_Objects View The DBA_Objects view retrieves information on all objects in the database. In addition to segments, information on objects such as Views and Database Links are retrieved. Before examining the results from DBA_Objects, Jerry creates a View on his EMP table as shown below.
SQL> 2 3 4 5 6 7 Create View EMP_DEPT_SAL As Select Dept, COUNT(*) As NDepts, SUM(SALARY) As TotalSal, AVG(SALARY) As AvgSal From EMP Group By Dept;

View created.

The following query against the DBA_Objects view returns information on the objects that users have created. The owner, name and type of object are displayed along with the date on which the object was created. The Last_DDL_Time is the date the last DDL statement was issued against the object. The view below shows the tables and indexes that were displayed in the prior views in this section. The highlighted rows show the SQL view that was created above as well as an SQL view that was created in Chapter 2.
SQL> Select Owner, Object_Name, Object_Type, Created, Last_DDL_Time 2 From DBA_Objects 3 Where Owner Not In ('SYS','SYSTEM','PUBLIC','OUTLN','DBSNMP') OWNER ------JERRY JERRY JERRY JERRY JERRY JERRY JERRY ELAINE ELAINE ELAINE OBJECT_NAME ---------------DEPARTMENT EMP EMP_DEPT_IDX EMP_DEPT_SAL EMP_SALARY SYS_C00996 SYS_C00997 CUSTOMER CUST_REGION_IDX SYS_C001001 OBJECT_TYPE ----------TABLE TABLE INDEX VIEW VIEW INDEX INDEX TABLE INDEX INDEX CREATED ---------06-JUN-03 06-JUN-03 06-JUN-03 06-JUN-03 01-JUN-03 06-JUN-03 06-JUN-03 06-JUN-03 06-JUN-03 06-JUN-03 LAST DDL_TIME ------------06-JUN-03 06-JUN-03 06-JUN-03 06-JUN-03 01-JUN-03 06-JUN-03 06-JUN-03 06-JUN-03 06-JUN-03 06-JUN-03

Modifying and Removing Tables The Alter Table command is used to modify the structure of the table. Using Alter Table to add columns or modify existing columns was discussed in Chapter 2. The Alter Table command may also be used to add or modify integrity constraints as explained in Chapter 9.

Chapter 6 Tablespaces and Users

Page 29

Modifying Storage Parameters You can change the storage parameters of a table as well as the PctFree and PctUsed. The following example changes the Next and PctIncrease parameters for the EMP table as well as increasing the PctUsed to sixty percent.
SQL> Alter Table EMP 2 Storage (Next 16K PctIncrease 40) 3 PctUsed 60; Table altered.

The new values apply prospectively. The sizes of existing extents are not modified because of the new values. Deallocating Unused Extents If many rows are deleted from a table that has acquired several extents, the extents are not automatically deallocated from the table. Once extents have been allocated, they must be deallocated using the statement shown below.
SQL> Alter Table EMP Deallocate Unused;

Table altered.

If you anticipate that data may be added to a table in the future, you may deallocate extents but require that the table be no smaller that a specified size. The statement below will deallocate unneeded extents but leave the size of the table at least 24K.
SQL> Alter Table EMP Deallocate Unused Keep 24K; Table altered.

Allocating New Extents You can explicitly allocate an extent to a table. The example below shows an extent being added to the CUSTOMER table. The extent will be sized according to the rules described earlier in this chapter for a new extent.
SQL> Alter Table CUSTOMER Allocate Extent;

Table altered.

You can allocate an extent to a table specifying the size of the extent and the data file in which the file should be located. In the example below, an extent is added to the EMP table with a size of 16K and is placed in the data_02.dbf file.

Chapter 6 Tablespaces and Users

Page 30

SQL> Alter Table EMP 2 Allocate Extent 3 (Size 16K Datafile '/oraclec/data/DB08/disk2/data_02.dbf'); Table altered.

Removing Tables Use the Drop Table command to remove the structure of a table and all of its data from the database. The Drop Table command cannot be rolled back so be sure you want to remove the table before issuing the command. The EMP table is dropped from Jerrys account in the example below.
SQL> Drop Table EMP; Table dropped.

If EMP is referenced as a foreign key in another table, you will receive the error shown below when you try to drop the table.
SQL> Drop Table EMP; Drop Table EMP * ERROR at line 1: ORA-02449: unique/primary keys in table referenced by foreign keys

By using the cascade constraints clause, you can drop the table and remove the constraint from the referencing table.
SQL> Drop Table EMP Cascade Constraints; Table dropped.

If you have DBA privileges, you can drop a table of another user. The following command run from the SYSTEM account will remove Elaines CUSTOMER table.
SQL> Drop Table Elaine.CUSTOMER; Table dropped.

S-ar putea să vă placă și