Table Spaces and Users

download Table Spaces and Users

of 30

Transcript of Table Spaces and Users

  • 8/3/2019 Table Spaces and Users

    1/30

    Chapter 6

    Creating and Managing Tablespaces

    As discussed in Chapter 3, Oracle assigns all of its data to tablespaces. Tablespaces arelike folders that contain segments (table, index, rollback, temporary). Any object youcreate, 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 thesepermissions.

    The following statement creates a tablespace named DATA and attaches a single data fileto it. The data file data_01.dbfis created in the path specified. The data file has a size of2 Megabytes. Reuse means that Oracle will reuse a file that has been detached fromthe database if its size is exactly 2 Megabyes. Any segment created in this tablespacewill be assigned the storage parameters indicted unless they are changed when the objectis created. Table 1 describes the default storage parameters.

    SQL> Create Tablespace Data

    2 Datafile '/oraclec/data/DB08/disk1/data_01.dbf' Size 2M Reuse

    3 Default Storage (

    4 Initial 32K Next 32K

    5 Minextents 2 Maxextents 121

    6 PctIncrease 0);

    Tablespace created.

    Table 1

    Default Storage Parameters

    Parameter Description

    Initial The first extent created will be of this size. It should be defined as a multipleof the db_block_size. All segments must contain at least this one extent.

    Next The second extent created will be of this size.

    Minextents The number of extents created when the segment is first created. Forexample, if this value is set to 2, an extent of the size specified in Initial plusan extent of the size specified in Next is created when the segment is created.

    Maxextents The maximum number of extents that can be assigned to the segment. If allextents fill with data and this parameter is reached, a cannot extend error willbe 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 defaultback to 121.

    PctIncrease Percent Increase allows your segment to grow at an increasing rate. The firsttwo 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. Thefourth 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, thedefault value will be 50.

  • 8/3/2019 Table Spaces and Users

    2/30

    Chapter 6 Tablespaces and Users Page 2

    Table 2 illustrates the growth of a segment with various PctIncrease settings. Extent sizemeans 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 parametersare 32K. Setting PctIncrease to zero will produce uniform extent sizes for all extentsexcept 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 thedefault storage parameters of the tablespace will direct SMON to coalesce free space.

    Table 2

    Table Growth And PctIncrease

    PctIncrease =

    25

    PctIncrease =

    50

    PctIncrease =

    75Extents Extent

    Size

    Segment

    Size

    Extent

    Size

    Segment

    Size

    Extent

    Size

    Segment

    Size

    First 32K 32K 32K 32K 32K 32K

    Second 32K 64K 32K 64K 32K 64K

    Third 40K 104K 48K 112K 56K 120KFourth 50K 154K 72K 184K 98K 218K

    Fifth 62.5K 216.5K 108K 292K 171.5K 389.5K

    Another tablespace, called INDEXES, is created below. The INDEXES tablespace isdefined with two data files. Since indexes should be located on a different disk from thedata, if possible, the data files for INDEXES are located on disks 3 and 4. When multiplefiles are defined in the Create Tablespace command, the Datafilekeyword is used onlyonce. The names of the data files are separated by commas as shown.

    SQL> Create Tablespace INDEXES

    2 Datafile '/oraclec/data/DB08/disk3/indexes_01.dbf' Size 2M Reuse,

    3 '/oraclec/data/DB08/disk4/indexes_02.dbf' Size 2M Reuse4 Default Storage (

    5 Initial 32K Next 32K Minextents 2 Maxextents 121 PctIncrease 0);

    Tablespace created.

    You can view information concerning your tablespaces through the DBA_Tablespacesview. A description of the columns in this view not described above is shown in the tablebelow followed by the information returned when all data are retrieved through the view.

    Column Name Description

    MIN_EXTLEN This is the minimum extent size that may be created by any segment in thetablespace.

    STATUS This can be AVAILABLE or OFFLINE. There will be several situationswhen a tablespace would be taken offline. Note that when the tablespaceis created, it is immediately brought online and is available for use.

  • 8/3/2019 Table Spaces and Users

    3/30

    Chapter 6 Tablespaces and Users Page 3

    CONTENTS This can be PERMANENT or TEMPORARY. Use Temporary if thetablespace is to be used by Oracle for sorts. You cant create objects in atemporary tablespace.

    LOGGING LOGGING indicates that activity on the segments in this tablespace arerecorded in the REDO log. NOLOGGING indicates activity is not recorded.

    SQL> select * from dba_tablespaces;

    TABLESPACE INITIAL NEXT MIN MAX PCT MIN

    NAME EXTENT EXTENT EXTENTS EXTENTS INCREASE EXTLEN STATUS CONTENTS LOGGING

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

    SYSTEM 10240 10240 1 121 50 0 ONLINE PERMANENT LOGGING

    RBS 131072 131072 2 50 0 0 ONLINE PERMANENT LOGGING

    TEMP 512000 512000 1 121 0 0 ONLINE PERMANENT LOGGING

    TOOLS 10240 10240 1 121 50 0 ONLINE PERMANENT LOGGING

    USERS 10240 10240 1 121 50 0 ONLINE PERMANENT LOGGING

    DATA 32768 32768 2 121 0 0 ONLINE PERMANENT LOGGINGINDEXES 32768 32768 2 121 0 0 ONLINE PERMANENT 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 thosevalues identified in the DEFAULT STORAGE clause of the CREATE TABLESPACEstatement. The INITIAL and NEXT parameters are doubled since the minextents was setto 2.

    The following view provides information on the data files that Oracle owns. These datafiles are associated with tablespaces and were created when the tablespace was createdor altered. The result of the query below indicates the full name and path of each data fileand the tablespace that owns it. The size of each data file in both bytes and blocks isprovided along with the current status of the data file. The data files that were createdearlier 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 TABLESPACE_NAME BYTES BLOCKS STATUS

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

    /oraclec/data/DB08/disk1/data_01.dbf DATA 2097152 256 AVAILABLE

    /oraclec/data/DB08/disk3/indexes_01.dbf INDEXES 2097152 256 AVAILABLE

    /oraclec/data/DB08/disk4/indexes_02.dbf INDEXES 2097152 256 AVAILABLE

    /oraclec/data/DB08/disk1/indx01.dbf INDX 5242880 640 AVAILABLE

    /oraclec/data/DB08/disk1/rbs01.dbf RBS 20971520 2560 AVAILABLE

    /oraclec/data/DB08/disk1/system01.dbf SYSTEM 104857600 12800 AVAILABLE

    /oraclec/data/DB08/disk1/temp01.dbf TEMP 10485760 1280 AVAILABLE

    /oraclec/data/DB08/disk1/tools01.dbf TOOLS 8388608 1024 AVAILABLE

    /oraclec/data/DB08/disk1/users01.dbf USERS 5242880 640 AVAILABLE

    9 rows selected.

    Locally Managed Tablespaces

    When you create a tablespace as shown in the section above, the tablespace isdictionary-managed. All information concerning the structure and parameters of the

  • 8/3/2019 Table Spaces and Users

    4/30

    Chapter 6 Tablespaces and Users Page 4

    tablespace is stored in the data dictionary. The data dictionary must be updated everytime a segment acquires a new extent. Rollback information and Redo log information onthis 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 managedtablespace, a bitmap index is created in the headers of the data files associated with thetablespace. Each bit of the index is associated with a database block located in the file.The bit indicates if the block is usedor free. Oracle uses the bitmap indexes to decidewhere to place the newly acquired extents. Since the information on the locally managedtablespace is not stored in the data dictionary, no Redo Log or Rollback information isgenerated for changes to the tablespace. (Redo Log and Rollback information is stillgenerated for changes to the data in the tablespace).

    Locally managed tablespaces are more efficient than data dictionary-managedtablespaces, 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 bitmapindex handles the management of space. However, locally managed tablespaces requireuniform extent sizes. The extent size is determined at the time of tablespace creation andcannot be overwritten in the storage clause of the tables. Storage parameters such asNext, Minextents, Maxextents, and PctIncrease do not apply to any object residing in thetablespace. A value may be determined for Initial but it must be a multiple of the UniformSize set for the tablespace. For example, if you are creating a table and enter a value of40K for Initial, the number of extents of the uniform size will be acquired to cover 40K. Inthe example below with a 32K uniform size, two extents will be acquired.

    The statement below creates a locally managed tablespace named DATA2 andassociates one file with the tablespace. There is no default storage clause. All extentswill 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 allocatedin a uniform manner.

    SQL> Select Tablespace_Name,

    2 Extent_Management,

    3 Allocation_Type

    4 From DBA_Tablespaces;

    TABLESPACE_NAME EXTENT_MAN ALLOCATIO

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

    SYSTEM DICTIONARY USER

    TOOLS DICTIONARY USER

    RBS DICTIONARY USER

  • 8/3/2019 Table Spaces and Users

    5/30

    Chapter 6 Tablespaces and Users Page 5

    TEMP DICTIONARY USER

    USERS DICTIONARY USER

    INDX DICTIONARY USER

    DATA DICTIONARY USER

    INDEXES DICTIONARY USER

    DATA2 LOCAL UNIFORM

    9 rows selected.

    Modifying and Removing Tablespaces

    Use the Alter Tablespace command to add one or more data files to a tablespace. Thefirst example below adds a 4 megabyte data file to the DATA tablespace and places it ondisk 1. The second example adds a 2 megabyte data file to the DATA2 tablespace andplaces 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 mayoccur because you have more resources available to you or you believe placing the file inanother location would lead to greater efficiency. Moving a data file is a four step processas 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 filename 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 DATAtablespace earlier in this section to disk 2.

  • 8/3/2019 Table Spaces and Users

    6/30

    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 freespace if the PctIncrease value in the tablespaces default storage clause exceeds zero. Thecontiguous space is acquired for the extent, if possible. If the space isnt available, a Cannot Extenterror is generated.

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

    Figure 2

    Fragmented Data File

    DEEmp

    DEDept

    FS FS DEEmp

    FS DEEmp

    FS DEDept

    FS FS

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

  • 8/3/2019 Table Spaces and Users

    7/30

    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 bemerged into a single large segment using expand imp.

    Figure 3

    Coalesced Data File

    DEEmp

    DEDept

    FS DEEmp

    FS DEEmp

    FS DEDept

    FS

    To examine the fragmentation on the tablespaces, use the dba_free_space view. Thefollowing view from DBA_Free_Space shows the amount of free space that is available oneach tablespace. The results provide the tablespace name along with the file identifier soone could join the tablespace (logical object) to its file(s) (physical objects). You can seethat 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, thenthe tablespace contains fragments of free space. The highlighted DATA tablespace hasthree fragments of free space of sizes (in blocks) 211, 12, and 511. The sum of all of thefragments represents the total free space in the tablespace. The Blocks column simplyindicates 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 2TOOLS 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.

  • 8/3/2019 Table Spaces and Users

    8/30

    Chapter 6 Tablespaces and Users Page 8

    The results from the query below show the total amount of free space in each tablespacewithout 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 theexample 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 tablespaceto read only. As shown in Figure 4, tables in read only tablespaces cannot be updatedand new rows cannot be added.

  • 8/3/2019 Table Spaces and Users

    9/30

    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 databasecommand. 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 musttemporality remove the objects from the file, shrink the file, and reload the objects. Thecommand 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 thedatabase, it still exists. Use the operating system command (rm in unix) to remove thefile.

    SQL> Alter Database DB08

    2 Datafile '/oraclec/data/DB08/disk2/data2_02.dbf' Offline Drop;

    Database altered.

  • 8/3/2019 Table Spaces and Users

    10/30

    Chapter 6 Tablespaces and Users Page 10

    A tablespace may be dropped with the Drop Tablespace command. The command belowdrops a hypothetical tablespace called Data3.

    SQL> Drop Tablespace Data3;

    Tablespace dropped.

    If the tablespace contains any objects, you must add the clause Including Contents asshown below.

    SQL> Drop Tablespace DATA3 Including Contents;

    Tablespace dropped.

    If existing tables in another tablespace reference the tables in the tablespace to bedropped, the foreign key constraints must be dropped as well with the command shownbelow.

    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 theoperating system command.

    Managing Users

    A User account must be created for anyone who needs to access the database. The Userbecomes a system object. When users are created, they must be given an initialpassword. Normally users are assigned a default tablespace and given a quota on thattablespace. Users may also be assigned a temporary tablespace.

    Creating Users

    To create a user, connect to the SYSTEM account and enter the CREATE USERcommand shown in Figure 5. The first line of the create user command requires you todefine a name. In this case, the user will be called Elaine. The IDENTIFIED BY clauserequires 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 asELAINE/WELCOME or Elaine/Welcome or any other combination of upper and lower caseletters. The DEFAULT TABLESPACE is the tablespace where all of Elaines tables andindexes will be stored automatically if she doesnt specify another tablespace. Thetablespace must exist or Oracle will generate an error. The TEMPORARY TABLESPACEmust also exist and was likely created during initial database creation. Quotas may beprovided on the tablespaces. In the example, Elaine has a 5M quota on the DATAtablespace and an unlimited quota on the TEMP tablespace.

  • 8/3/2019 Table Spaces and Users

    11/30

    Chapter 6 Tablespaces and Users Page 11

    Even though the Elaine user is created, she will not be allowed to access Oracle sinceshe 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 onthe database. Well examine these roles in detail in Chapter 9. Not only can Elaineaccess Oracle, she can create tables, indexes, views, triggers, procedures, and manyother objects. The roles are granted in Figure 6 and Elaine is connected.

    Figure 6Granting Connect and Resource Roles

  • 8/3/2019 Table Spaces and Users

    12/30

    Chapter 6 Tablespaces and Users Page 12

    In Figure 7, a user named George is created with a password of welcome and a defaulttablespace of DATA. A 5M quota is placed on the tablespace DATA. The final line of theCREATE USER statement contains the clause PASSWORD EXPIRE. PASSWORDEXPIRE will require George to change his password the first time he connects to Oracleas 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 directorycalled newuser.sql. In that file enter the commands to create a user replacing the usersname with &1. Enter the command to grant connect and resource to a user againreplacing 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 Templine and againafter the Grantstatement. Save this file. Connect to Oracle as SYSTEM and enter thefollowing:

    SQL> @newuser jerry

  • 8/3/2019 Table Spaces and Users

    13/30

    Chapter 6 Tablespaces and Users Page 13

    Oracle will run your script file replacing &1 with jerry. Note that the file is called newuser.sqlbut you dont need to use the .sql part when calling the file. By simply retyping thecommand above and changing the name from jerry to Kramer, you can create the Krameruser.

    A user may be granted privileges on the database to perform DBA functions. This userwill be created by the script file from above and then be granted the DBA role in additionto the Connect and Resource roles. A user, named Superman, is created and given DBAprivileges 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> select2 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 PASSWORD ACCOUNT LOCK EXPIRY DEFAULT TEMPORARY CREATED PROFILE

    ID STATUS DATE DATE TABLESPACE TABLESPACE

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

    DBSNMP 16 E066D214D5421CCC OPEN SYSTEM SYSTEM 02-JAN-03 DEFAULT

    ELAINE 30 092CA7BCF360A117 OPEN DATA TEMP 26-MAY-03 DEFAULT

    GEORGE 31 666D4EF49E031243 OPEN DATA TEMP 01-JUN-03 DEFAULT

    JERRY 27 88F7D29BBCA21A5E OPEN DATA TEMP 26-MAY-03 DEFAULT

    KRAMER 32 621FB93B53107919 OPEN DATA TEMP 01-JUN-03 DEFAULT

    OUTLN 11 4A3BA55E08595C81 OPEN SYSTEM SYSTEM 02-JAN-03 DEFAULT

    SUPERMAN 26 42153BFAF4C2A023 OPEN DATA TEMP 26-MAY-03 DEFAULT

    SYS 0 D4C5016086B2DC6A OPEN SYSTEM TEMP 02-JAN-03 DEFAULTSYSTEM 5 D4DF7931AB130E37 OPEN TOOLS TEMP 02-JAN-03 DEFAULT

    TRACESVR 19 F9DA8977092B7B81 OPEN SYSTEM SYSTEM 02-JAN-03 DEFAULT

    Note that the password is encrypted. Even though Jerry, Elaine, and Kramer all have apassword of welcome, the encrypted passwords are different. We will discuss theAccount 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 changeyour 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 totablespaces. The two statements shown below express quotas on the DATA2 andINDEXES tablespaces for Jerry and Elaine.

  • 8/3/2019 Table Spaces and Users

    14/30

    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 databasewill 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 thefollowing 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 asshown 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.

  • 8/3/2019 Table Spaces and Users

    15/30

    Chapter 6 Tablespaces and Users Page 15

    To force off a user, enter the following replacing the SID and serial# with values fromabove. 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 assigningprofiles to users, the resources that users can exhaust is limited. The general form of theCreate 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 ofparameters available that involve password management. The password managementparameters will be discussed with security issues in Chapter 9.

    Table 3

    Profile Parameters

    Parameter Value

    Type

    Description

    Sessions_Per_User Integer 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.

    CPU_Per_Session Hundredthsof a second

    Amount of CPU time used per session.

    CPU_Per_Call Hundredthsof a second

    Amount of CPU time used per single SQL request.

    Logical_Reads_Per_Session

    Number ofDB Blcoks

    Number of blocks that can be read during a session.

    Logical_Reads_Per_

    Call

    Number of

    DB Blcoks

    Number of blocks that can be read with a single SQL

    request.

    Idle_Time Minutes If the user doesnt enter something into SQL for thisamount of time, the session will be terminated.

    Connect_Time Minutes Amount of time the user may be connected to thedatabase during a 24 hour period.

    Private_SGA_Per_Session

    Number ofDB Blcoks

    Limits the size of the private memory that can be usedfor storing a users private SQL.

  • 8/3/2019 Table Spaces and Users

    16/30

    Chapter 6 Tablespaces and Users Page 16

    Composite_Limit Integer A composite of the parameters underlined. Even thoughno individual resource limit is reached, Composite_Limitcan impose a limit on the total of the resources used.Set a resource value for each parameter using ALTERRESOURCE COST. For Example, ALTER RESOURCECOST Connect_Time 10; Each minute of connect timewill generate a resource use of 10. Set the resourcelimit for all other parameters. Provide a value for theComposite_Limit.

    The following example creates a profile named USER_PROFILE that sets idle time to tenminutes, the cpu that can be used in a session to ten seconds, and the number of blocksthat can be read by a single SQL statement to 20.

    SQL> Create Profile USER_PROFILE Limit

    2 Idle_Time 10

    3 CPU_Per_Session 1000

    4 Logical_Reads_Per_Call 20;

    If no profile is assigned to a user, the user will be assigned the DefaultProfile. Default willinitially have all parameters set to unlimited. You can alter the default profile but not dropit. You can modify any profile with the Alter Profile command. In the example below, thenumber 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_Profilesview shows the current values of the profiles parameters. TheDEFAULT profile has values set to UNLIMITED. The USER_PROFILE created andaltered above limited the four parameters highlighted below. The Resource Typedescribed in this chapter is KERNEL. The PASSWORD Resource Type placesrestrictions on passwords and will be discussed in Chapter 9.

    SQL> Select * From DBA_Profiles2 Where Resource_Type = 'KERNEL'

    3 Order By 1;

    PROFILE RESOURCE_NAME RESOURCE LIMIT

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

    DEFAULT COMPOSITE_LIMIT KERNEL UNLIMITED

    DEFAULT SESSIONS_PER_USER KERNEL UNLIMITED

    DEFAULT CPU_PER_CALL KERNEL UNLIMITED

    DEFAULT LOGICAL_READS_PER_CALL KERNEL UNLIMITED

  • 8/3/2019 Table Spaces and Users

    17/30

    Chapter 6 Tablespaces and Users Page 17

    DEFAULT CONNECT_TIME KERNEL UNLIMITED

    DEFAULT PRIVATE_SGA KERNEL UNLIMITED

    DEFAULT IDLE_TIME KERNEL UNLIMITED

    DEFAULT LOGICAL_READS_PER_SESSION KERNEL UNLIMITED

    DEFAULT CPU_PER_SESSION KERNEL UNLIMITED

    USER_PROFILE COMPOSITE_LIMIT KERNEL DEFAULT

    USER_PROFILE PRIVATE_SGA KERNEL DEFAULT

    USER_PROFILE SESSIONS_PER_USER KERNEL 10

    USER_PROFILE CPU_PER_CALL KERNEL DEFAULT

    USER_PROFILE LOGICAL_READS_PER_CALL KERNEL 20

    USER_PROFILE CONNECT_TIME KERNEL DEFAULT

    USER_PROFILE IDLE_TIME KERNEL 10

    USER_PROFILE LOGICAL_READS_PER_SESSION KERNEL DEFAULT

    USER_PROFILE CPU_PER_SESSION KERNEL 1000

    18 rows selected.

    Managing Segments

    If you have been granted permission on the tablespace, you can create a table and assignit to that tablespace. Also, you can write storage parameters in the definition of the tablethat 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 parametersdefined for the tablespace will be used. The user Jerry issues the following CREATETABLE statement. It places the table in the DATA tablespace created earlier in thischapter and changes the PctIncrease and Minextents parameters. Note that you use thekeyword STORAGE in line 8 since these are the actual storage values for the table andnot 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 Initialand Nextare both 32K and the maxextents is121 as defined in the CREATE TABLESPACE statement. In the example below, Jerrycreates the DEPARTMENT table in the DATA2 tablespace changing the size of the Initialstorage parameter. Since DATA2 is a locally managed tablespace, the Next , Minextents,Maxextents, and PctIncrease parameter values are not used. The DEPARTMENT tablewill be created with two extents each of size 32K.

  • 8/3/2019 Table Spaces and Users

    18/30

    Chapter 6 Tablespaces and Users Page 18

    SQL> Create Table DEPARTMENT

    2 (Dept Char(3) Primary Key,

    3 DeptName Varchar2(40))

    4 Tablespace DATA2

    5 Storage(

    6 Initial 48K);

    Table created.

    The example below shows the creation of an index on EMPs Dept column. The indexis 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 ascript file.

    Managing Space Usage

    The database block size is determined when the database is initially created and cannotbe changed. The segments data are stored in the blocks. The database block size isknown to be 8k (use show parameter db_block_size from the SYSTEM account to find thisparameters 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 tothe 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 thedatabase is identified as overhead in Figure 8. Although the amount of space used byoverhead may vary, it is generally considered to be about five percent of the availablespace in the block.

    Figure 8 illustrates space usage within a single data block. Values for two parametersmay be set to determine how the space is used, PctFree and PctUsed. PctFree is thepercent of the space in the block that will be left unused when the data for rows are placedin the block. PctUsed is the percentage of the space in a block that forms a lowerthreshold necessary for adding data to the block. When data are deleted, the amount ofused space in the block decreases. However, new data are not immediately inserted intothe block to replace the deleted data. When enough data have been deleted from theblock so that the PctUsed threshold has been reached, the blocks id is placed on thefreelist. The freelist is a list of block ids indicating the blocks that are available to acceptdata. New rows inserted into the table may now be placed in the block until the block fillsas it did originally. As an example, suppose the PctFree is ten percent and the Pctused isforty 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 theblock falls to forty percent, the block will be placed on the freelist.

  • 8/3/2019 Table Spaces and Users

    19/30

    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 variablelength format. If a null value is later changed to a real value or the length of an existingdata value is increased, the data must be stored somewhere in the block. The data will bestored in available free space in the block referenced by a pointer in the datas originallocation. If there is not enough free space available in the block, the entire row is movedto another block. Moving an entire row from one block to another because there is notenough free space to handle updates is called migration. Because migration reducesdatabase 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 tablescontain static data, set the PctFree to a low value such as five percent. If the tablecontains 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 toa relatively high value. Normally the PctFree should not be set to a value greater thantwenty-five percent. The CUSTOMER table is created in the example below on Elainesaccount. The value of PctIncrease is changed to twenty-five percent and the table islocated in the DATA tablespace. The shaded line illustrates how to set the PctFree andPctUsed values. Note that they are not part of the Storage clause.

  • 8/3/2019 Table Spaces and Users

    20/30

    Chapter 6 Tablespaces and Users Page 20

    SQL> Create Table CUSTOMER

    2 (CID Char(2) Primary Key,

    3 CName Varchar2(20) Not Null,

    4 Region Char(1),

    5 ID Number(3))

    6 Tablespace DATA

    7 Storage(

    8 PctIncrease 25)

    9 PctFree 15 PctUsed 50;

    Table created.

    The example below creates and index on the CUSTOMERs Region column placing it inthe 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. Onceyou analyze the table, the statistics are stored in that table and can be viewed through theUSER_Tables or DBA_Tables views. The statistics are not updated dynamically. Thevalues of the statistics are recorded and do not change until another Analyze Tablestatement is run against the table. You can use these statistics to monitor space usage ofthe 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 shouldnot analyze any of the tables owned by SYS. The general form of the Analyze Tablecommand is shown below.

    Analyze Table tablename < Compute|Estimate > Statistics

    < Sample n < Percent|Rows > >

    If you Computethe statistics, all of the rows in the table are analyzed. This can be timeconsuming for tables containing a large number of rows. To save time, you can Estimatethe statistics based upon a random sample of rows selected by Oracle. You can choosethe size of the sample used to Estimate Statistics by specifying the number of rows or thepercent 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 inthe table will be analyzed. Figure 9 below shows that initially no statistics exist in ElainesCUSTOMER table. After Elaine analyzes the table, the statistics can be viewed.

  • 8/3/2019 Table Spaces and Users

    21/30

    Chapter 6 Tablespaces and Users Page 21

    Figure 9

    Computing Statistics

    In Figure10, the DEPARTMENT and EMP tables are analyzed from Jerrys account usingEstimate Statistics. The DEPARTMENT table is analyzed using the default sample sizeand 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 Figure11.

  • 8/3/2019 Table Spaces and Users

    22/30

    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 thatcontain the tables data. Normally the structure of a block is validated when the block isread into the database buffer cache. The command below validates the structure of theEMP table.

    SQL> Analyze Table EMP Validate Structure;

    Table analyzed.

    You can validate the structure of the table and its associated objects such as indexes withthe 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 thetablespace where the table resides and the owner of the table. Two new parameters arealso included, InitTrans and MaxTrans. The value for InitTrans represents the spacereserved to log one user who is accessing the tables data. The value from MaxTransrepresents 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 rarelychanged. The results of the query below show the tables in the database not owned bySYS 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');

  • 8/3/2019 Table Spaces and Users

    23/30

    Chapter 6 Tablespaces and Users Page 23

    TABLE TABLESPACE PCT PCT INI MAX INITIAL NEXT MIN MAX PCT

    OWNER NAME NAME FREE USED TRANS TRANS EXTENT EXTENT EXTENTS EXTENTS INCREASE

    ------- ----------- ---------- ---- ----- ---- ----- -------- ------ ------- ------- --------OUTLN OL$ SYSTEM 10 40 1 255 16384 16384 1 505 50

    OUTLN OL$HINTS SYSTEM 10 40 1 255 16384 16384 1 505 50

    JERRY EMP DATA 10 40 1 255 32768 32768 1 121 50

    JERRY DEPARTMENT DATA2 10 40 1 255 49152 32768 1 2147483645 0

    ELAINE CUSTOMER 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 USERNAME BYTES MAX_BYTES BLOCKS MAX_BLOCKS

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

    DATA ELAINE 131072 5242880 16 640INDEXES ELAINE 65536 -1 8 -1

    DATA2 ELAINE 0 10485760 0 1280

    TEMP ELAINE 0 -1 0 -1

    DATA GEORGE 0 5242880 0 640

    TEMP GEORGE 0 -1 0 -1

    INDEXES JERRY 65536 -1 8 -1

    DATA JERRY 98304 5242880 12 640

    DATA2 JERRY 98304 10485760 12 1280

    TEMP JERRY 0 -1 0 -1

    DATA KRAMER 0 5242880 0 640

    TEMP SUPERMAN 0 -1 0 -1

    DATA SUPERMAN 0 5242880 0 640

    13 rows selected.

    The first two columns identify a unique tablespace, user combination. For example, thefirst four rows identify Elaines allocation on the four tablespaces she can access. TheBytes 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 firstrow, Elaine has used 131,072 bytes of her 5,242,880 byte (5M) quotaon the DATAtablespace. The 1 under Max_Bytes means the quota is unlimited. The second row ofthe results shows that Elaines quota on the INDEXES tablespace is unlimited. The lasttwo columns convert the bytes used and quota in bytes to database blocks. With adb_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 totalnumber of bytes used is computed for each tablespace. The first row of the results belowshows that objects residing in the DATA tablespace have used 229,376 bytes.

  • 8/3/2019 Table Spaces and Users

    24/30

    Chapter 6 Tablespaces and Users Page 24

    SQL> Select Tablespace_Name,

    2 SUM(Bytes) As Bytesused3 SUM(Blocks) As Blocksused

    4 From DBA_TS_Quotas

    5 Group By Tablespace_Name;

    TABLESPACE_NAME BYTESUSED BLOCKSUSED

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

    DATA 229376 28

    DATA2 98304 12

    INDEXES 131072 16

    TEMP 0 0

    4 rows selected.

    Monitoring Space Usage

    Recalling that the DBA_Free_Space view described earlier in this Chapter providesinformation 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 joinsDBA_Free_Space and DBA_TS_Quotas to calculate the percent of available space usedin each tablespace. From the results of the query below, only 2.23 percent of the space inthe DATA tablespace is being used.

    SQL> Select a.Tablespace_Name,

    2 SUM(a.Bytes) As BytesFree,

    3 SUM(b.Bytes) As BytesUsed,

    4 SUM(b.Bytes)/(SUM(a.Bytes)+SUM(b.Bytes)) * 100

    5 As "Percent Used"

    6 From DBA_Free_Space a, DBA_TS_Quotas b

    7 Where a.Tablespace_Name = b.Tablespace_Name

    8 Group By a.Tablespace_Name;

    TABLESPACE_NAME BYTESFREE BYTESUSED Percent Used

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

    DATA 30228480 688128 2.2257552

    DATA2 12124160 196608 1.5957447

    INDEXES 8093696 262144 3.1372549

    TEMP 41910272 0 0

    Data Dictionary Space Usage Views

    DBA_Segments View

    The results of the query below provide specific information on the segments. Thesegments residing on the SYSTEM and TOOLS tablespaces have been filtered out of theresults for readability and because we are not interested in the SYSTEM segments at thispoint. The first row of the results shows that Elaine has created a Table namedCUSTOMER in the DATA tablespace. The Elaines CUSTOMER table currently has twoextents attached to it and it is allowed to grow to 121 extents.

  • 8/3/2019 Table Spaces and Users

    25/30

    Chapter 6 Tablespaces and Users Page 25

    The DBA_Segments view is one of the most useful data dictionary views because itprovides access to all of the segments on the database. If we need to know informationregarding both Tables and Indexes, this view can provide us with that information.

    SQL> Select Owner, Segment_Type, Tablespace_Name, Segment_Name,

    Extents, Max_Extents

    1 From DBA_Segments2 Where Tablespace_Name NOT IN ('SYSTEM',TOOLS)

    3 Order By Owner;

    OWNER SEGMENT_TYPE TABLESPACE_NAME SEGMENT_NAME EXTENTS MAX_EXTENTS

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

    ELAINE TABLE DATA CUSTOMER 2 121

    ELAINE INDEX DATA SYS_C001043 2 121

    ELAINE INDEX INDEXES CUST_REGION_IDX 2 121

    JERRY TABLE DATA EMP 1 121

    JERRY TABLE DATA2 DEPARTMENT 2 2147483645

    JERRY INDEX DATA SYS_C001033 2 121

    JERRY INDEX INDEXES EMP_DEPT_IDX 2 121

    JERRY INDEX DATA2 SYS_C001034 1 2147483645

    SYS ROLLBACK RBS RBS0 8 4096

    9 rows selected.

    The following view from DBA_Segments provides some of the same information as theprevious view concerning space usage, but adds some additional elements. The querybelow shows each tablespace and the bytes the segments occupy; however it alsoprovides information on the segments stored in each tablespace. The first row of theresults below indicates that the DATA tablespace contains four segments. The SYSTEMtablespace contains the 445 segments that make up the data dictionary. The RBStablespace 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 useralong 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;

  • 8/3/2019 Table Spaces and Users

    26/30

    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 segmentand 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 2940928INDEX 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. TheDEPARTMENT table was created in the DATA2 tablespace using locally managedextents of a uniform 32K extent size. DEPARTMENT was created with an Initialstorageparameter of 48K. To satisfy the Initial extent requirement, two uniform extents must beallocated. When using locally managed extents, the uniform extent size cannot bechanged by changing the storage parameters.

    SQL> Select Owner, Segment_Name, Segment_Type,

    2 Tablespace_Name, Extent_Id, Bytes

    3 From DBA_Extents Where Tablespace_Name != SYSTEM

    4 Order By Owner, Segment_Name;

    SEGMENT_ TABLESPACE EXTENT

    OWNER SEGMENT_NAME TYPE _NAME _ID BYTES BLOCKS

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

    ELAINE CUSTOMER TABLE DATA 0 32768 4

    ELAINE CUSTOMER TABLE DATA 1 32768 4

    ELAINE CUST_REGION_IDX INDEX INDEXES 0 32768 4

    ELAINE CUST_REGION_IDX INDEX INDEXES 1 32768 4

    ELAINE SYS_C001043 INDEX DATA 1 32768 4

    ELAINE SYS_C001043 INDEX DATA 0 32768 4

  • 8/3/2019 Table Spaces and Users

    27/30

    Chapter 6 Tablespaces and Users Page 27

    JERRY EMP TABLE DATA 0 32768 4

    JERRY SYS_C001054 INDEX DATA 1 32768 4

    JERRY DEPARTMENT TABLE DATA2 0 32768 4

    JERRY DEPARTMENT TABLE DATA2 1 32768 4

    JERRY EMP_DEPT_IDX INDEX INDEXES 0 32768 4

    JERRY EMP_DEPT_IDX INDEX INDEXES 1 32768 4

    JERRY SYS_C001055 INDEX DATA2 0 32768 4

    JERRY SYS_C001054 INDEX DATA 0 32768 4

    SYS RBS0 ROLLBACK RBS 0 524288 64

    SYS RBS0 ROLLBACK RBS 1 524288 64

    SYS RBS0 ROLLBACK RBS 2 524288 64

    SYS RBS0 ROLLBACK RBS 3 524288 64

    SYS RBS0 ROLLBACK RBS 4 524288 64

    SYS RBS0 ROLLBACK RBS 5 524288 64

    SYS RBS0 ROLLBACK RBS 6 524288 64

    SYS RBS0 ROLLBACK RBS 7 524288 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 theCUSTOMER and DEPARTMENT tables have each acquired two extents and the filewhere each extent is located.

    SQL> Select Owner, Segment_Name, Extent_ID, Segment_Type,File_Name

    2 From DBA_Extents a, DBA_Data_Files b

    3 Where a.File_ID = b.File_ID

    4 And a.Tablespace_Name != 'SYSTEM'

    5 Order By Owner, Segment_Type;

    EXTENT SEGMENT

    OWNER SEGMENT_NAME _ID _TYPE FILE_NAME

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

    ELAINE CUSTOMER 1 TABLE /oraclec/data/DB08/disk1/data_01.dbf

    ELAINE CUSTOMER 0 TABLE /oraclec/data/DB08/disk1/data_01.dbf

    ELAINE SYS_C001043 1 INDEX /oraclec/data/DB08/disk1/data_01.dbf

    ELAINE SYS_C001043 0 INDEX /oraclec/data/DB08/disk1/data_01.dbf

    ELAINE CUST_REGION_IDX 1 INDEX /oraclec/data/DB08/disk3/indexes_01.dbf

    ELAINE CUST_REGION_IDX 0 INDEX /oraclec/data/DB08/disk4/indexes_02.dbf

    JERRY EMP 0 TABLE /oraclec/data/DB08/disk1/data_01.dbf

    JERRY DEPARTMENT 0 TABLE /oraclec/data/DB08/disk1/data2_01.dbf

    JERRY DEPARTMENT 1 TABLE /oraclec/data/DB08/disk2/data2_02.dbf

    JERRY EMP_DEPT_IDX 0 INDEX /oraclec/data/DB08/disk3/indexes_01.dbf

    JERRY EMP_DEPT_IDX 1 INDEX /oraclec/data/DB08/disk4/indexes_02.dbf

    JERRY SYS_C001054 0 INDEX /oraclec/data/DB08/disk1/data_01.dbf

    JERRY SYS_C001054 1 INDEX /oraclec/data/DB08/disk1/data_02.dbf

    JERRY SYS_C001055 0 INDEX /oraclec/data/DB08/disk1/data2_01.dbf

    SYS RBS0 0 ROLLBACK /oraclec/data/DB08/disk1/rbs01.dbf

    SYS RBS0 1 ROLLBACK /oraclec/data/DB08/disk1/rbs01.dbf

    SYS RBS0 2 ROLLBACK /oraclec/data/DB08/disk1/rbs01.dbf

    SYS RBS0 3 ROLLBACK /oraclec/data/DB08/disk1/rbs01.dbf

    SYS RBS0 4 ROLLBACK /oraclec/data/DB08/disk1/rbs01.dbf

    SYS RBS0 5 ROLLBACK /oraclec/data/DB08/disk1/rbs01.dbf

    SYS RBS0 6 ROLLBACK /oraclec/data/DB08/disk1/rbs01.dbf

    SYS RBS0 7 ROLLBACK /oraclec/data/DB08/disk1/rbs01.dbf

    22 rows selected.

  • 8/3/2019 Table Spaces and Users

    28/30

    Chapter 6 Tablespaces and Users Page 28

    DBA_Objects View

    The DBA_Objects view retrieves information on all objects in the database. In addition tosegments, 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 tableas shown below.

    SQL> Create View EMP_DEPT_SAL As

    2 Select Dept,

    3 COUNT(*) As NDepts,

    4 SUM(SALARY) As TotalSal,

    5 AVG(SALARY) As AvgSal

    6 From EMP

    7 Group By Dept;

    View created.

    The following query against the DBA_Objects view returns information on the objects thatusers have created. The owner, name and type of object are displayed along with thedate on which the object was created. The Last_DDL_Time is the date the last DDLstatement was issued against the object. The view below shows the tables and indexesthat were displayed in the prior views in this section. The highlighted rows show the SQLview 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 OBJECT_NAME OBJECT_TYPE CREATED LAST DDL_TIME

    ------- ---------------- ----------- ---------- -------------JERRY DEPARTMENT TABLE 06-JUN-03 06-JUN-03

    JERRY EMP TABLE 06-JUN-03 06-JUN-03

    JERRY EMP_DEPT_IDX INDEX 06-JUN-03 06-JUN-03

    JERRY EMP_DEPT_SAL VIEW 06-JUN-03 06-JUN-03

    JERRY EMP_SALARY VIEW 01-JUN-03 01-JUN-03

    JERRY SYS_C00996 INDEX 06-JUN-03 06-JUN-03

    JERRY SYS_C00997 INDEX 06-JUN-03 06-JUN-03

    ELAINE CUSTOMER TABLE 06-JUN-03 06-JUN-03

    ELAINE CUST_REGION_IDX INDEX 06-JUN-03 06-JUN-03

    ELAINE SYS_C001001 INDEX 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 Tableto add columns or modify existing columns was discussed in Chapter 2. The Alter Tablecommand may also be used to add or modify integrity constraints as explained in Chapter9.

  • 8/3/2019 Table Spaces and Users

    29/30

    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 tableas 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 modifiedbecause of the new values.

    Deallocating Unused Extents

    If many rows are deleted from a table that has acquired several extents, the extents arenot automatically deallocated from the table. Once extents have been allocated, theymust 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 deallocateextents but require that the table be no smaller that a specified size. The statement belowwill 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 extentbeing added to the CUSTOMER table. The extent will be sized according to the rulesdescribed 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 inwhich the file should be located. In the example below, an extent is added to the EMPtable with a size of 16K and is placed in the data_02.dbffile.

  • 8/3/2019 Table Spaces and Users

    30/30

    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 fromthe database. The Drop Table command cannot be rolled back so be sure you want toremove the table before issuing the command. The EMP table is dropped from Jerrysaccount 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 theconstraint 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 commandrun from the SYSTEM account will remove Elaines CUSTOMER table.

    SQL> Drop Table Elaine.CUSTOMER;

    Table dropped.