I3306 – Database II Chapter 4 - Oracle XE 11.2 Administration References https://docs.oracle.com/cd/E17781_01/server.112/e18804/toc.htm https://docs.oracle.com/cd/E11882_01/server.112/e22490/dp_overview.htm#SUTIL100 Chapter Outline • • • • • • • • Starting Up and Shutting Down Connecting to the Database Managing Database Memory Managing Database Storage Managing Users and Security Data Dictionary Monitoring the Database Viewing Database Version and Globalization Information • Exporting and Importing Metadata and Data • Baking Up and Recovering Dr. Michel Nabaa Oracle XE 11.2 Administration 2 Starting Up • Oracle Database XE starts up automatically immediately after installation and after each system restart. Thus, there is no need to start up the database unless you previously shut it down. • To startup Oracle XE – Click Start, Programs (or All Programs), Oracle Database 11g Express Edition, and then select Start Database. Or – At the operating system prompt, enter SQLPLUS / AS SYSDBA SQL> STARTUP • To exit the SQL command line – SQL> EXIT Dr. Michel Nabaa Oracle XE 11.2 Administration 3 Shutting Down • Click Start, Programs (or All Programs), Oracle Database 11g Express Edition, and then select Stop Database. Or • At the operating system prompt, enter – SQLPLUS / AS SYSDBA – SQL> SHUTDOWN IMMEDIATE Dr. Michel Nabaa Oracle XE 11.2 Administration 4 Connecting to the Database • 1. Locally • Using the SQL Command Line – Click Start, point to Programs (or All Programs), point to Oracle Database 11g Express Edition, and then select Run SQL Command Line. SQL> CONNECT username/password Or • At the operating system prompt, enter – SQLPLUS SQL> CONNECT username/password • Using SQL Developer – Create Database Connections Dr. Michel Nabaa Oracle XE 11.2 Administration 5 Connecting to the Database • 2. Remotely • At the operating system prompt, enter – SQLPLUS SQL> Connect username/password@[//]host[:port][/service_name] // is optional – host is host name or IP address of the computer running Oracle XE – port (optional) TCP port number where Oracle Net listener is listening. • If not specified, the default port number 1521 is assumed. – service_name (optional) for Oracle Database XE, service name is XE. Dr. Michel Nabaa Oracle XE 11.2 Administration 6 Managing Database Memory • Memory allocation Overview • Automatic Memory Management • SGA and PGA sizes Dr. Michel Nabaa Oracle XE 11.2 Administration 7 Memory allocation Overview Dr. Michel Nabaa Oracle XE 11.2 Administration 8 Automatic Memory Management (AMM) • Automatic Memory Management in Oracle Database XE usually provides the best memory configuration based on your resources and workload. • It dynamically exchanges memory between SGA and PGA as needed to meet processing demands and tunes the sizes of the individual SGA components and the sizes of the individual PGAs. • Automatic Memory Management cannot be disabled. Dr. Michel Nabaa Oracle XE 11.2 Administration 9 SGA and PGA sizes (1) • Default sizes for SGA and PGA are set upon installation. • The maximum amount of memory allowed for the SGA and PGA Aggregate is 1 gigabyte (GB). • Starting Oracle 11g, AMM is enabled and memory_target parameter is set. • If memory_target is set to non zero value and : – sga_target and pga_aggregate_target are set to 0, then 60% of memory mentioned in memory_target is allocated to SGA and the remaining 40% is kept for PGA. – sga_target and pga_aggregate_target are set to non-zero values, then these values will be considered minimum values. Dr. Michel Nabaa Oracle XE 11.2 Administration 10 SGA and PGA sizes (2) • The only circumstances under which you should need to change SGA and PGA Aggregate sizes are the following: – You add physical memory to the computer running Oracle Database XE and want to allocate more to the database. – You received an error due to insufficient memory. • in SGA ORA-04031: unable to allocate n bytes of shared memory ORA-00379: no free buffers available in buffer pool... • in PGA ORA-04030: out of process memory when trying to allocate n bytes Dr. Michel Nabaa Oracle XE 11.2 Administration 11 Changing SGA and PGA sizes (1) • In SQL Developer, click View, then DBA to display the DBA navigator. • In the DBA navigator, expand the SYSTEM connection, expand Database Configuration, and click Initialization Parameters. • Note the current values for pga_aggregate_target and sga_target, and calculate the desired new values. Assume that the desired new values are 140 megabytes for pga_aggregate_target and 400 megabytes for sga_target. • In the Connections navigator, select the SYSTEM connection; and if a SQL Worksheet is not already open for that connection, rightclick SYSTEM and select Open SQL Worksheet. Dr. Michel Nabaa Oracle XE 11.2 Administration 12 Changing SGA and PGA sizes (2) • Enter the following command lines: – ALTER SYSTEM SET pga_aggregate_target = 140 M; – ALTER SYSTEM SET sga_target = 400 M; • Run Script icon to execute these statements. • At the next convenient time, shut down and restart the database to enable the SGA size changes to take effect. • SHOW SGA; Do not set any memory-related parameters unless you fully understand the consequences. Dr. Michel Nabaa Oracle XE 11.2 Administration 13 Managing Database Storage • • • • • The Database Storage Structures Monitoring Storage Space Usage Compacting Storage Viewing redo Log Files Managing the Flash recovery Area Dr. Michel Nabaa Oracle XE 11.2 Administration 14 The Database Storage Structures Dr. Michel Nabaa Oracle XE 11.2 Administration 15 Monitoring Storage Space Usage (1) • Oracle Database XE 11.2 is limited to 11GB of user data • To check and monitor the storage space used 1. In SQL Developer, click the Reports navigator tab, and expand the hierarchy as follows: All Reports, then Data Dictionary Reports, then Database Administration, then Storage. 2. Under Storage, click Free Space, then select a database connection for a privileged user such as SYSTEM Dr. Michel Nabaa Oracle XE 11.2 Administration 16 Monitoring Storage Space Usage (2) • If you notice that free space is becoming low 1. For each schema: – Log in as the schema owner. – Drop (delete) unused database objects. – Purge the recycle bin. 2. Compact storage. Dr. Michel Nabaa Oracle XE 11.2 Administration 17 The Recycle Bin • The recycle bin is a data dictionary table containing information about dropped objects. SELECT Value FROM V$parameter WHERE Name = 'recyclebin'; ALTER SYSTEM SET recyclebin = ON; Or ALTER SESSION SET recyclebin = ON; • In SQL Developer, in the Connections navigator, you can – view the contents of the recycle bin , – restore dropped objects, and – purge objects from the recycle bin. Dr. Michel Nabaa Oracle XE 11.2 Administration 18 Compact Storage • Objects with fragmented free space result in much wasted space. • The preferred way to defragment and reclaim this space is to perform an online segment shrink. • Use Segment Advisor to identify segments that would benefit from online segment shrink. • If the Segment Advisor determines that an object has a significant amount of free space, it recommends online segment shrink. ALTER TABLE employees ENABLE ROW MOVEMENT; ALTER TABLE employees SHRINK SPACE COMPACT; Dr. Michel Nabaa Oracle XE 11.2 Administration 19 Viewing Redo Log Files 1. In SQL Developer, click View, then DBA. 2. Expand a SYSTEM connection in the DBA navigator; then expand Storage and select Redo Log Groups. 3. Select any redo log group to see its location, type, and whether it was created in the flash recovery area. Dr. Michel Nabaa Oracle XE 11.2 Administration 20 Managing the Flash Recovery Area(1) Monitoring flash recovery area available space • To determine the current available space in the flash recovery area, as a system user run the SQL query: SELECT NAME, TO_CHAR(SPACE_LIMIT, '999,999,999,999') AS SPACE_LIMIT, TO_CHAR(SPACE_LIMIT - SPACE_USED + SPACE_RECLAIMABLE, '999,999,999,999') AS SPACE_AVAILABLE, ROUND((SPACE_USED SPACE_RECLAIMABLE)/SPACE_LIMIT * 100, 1) AS PERCENT_FULL FROM V$RECOVERY_FILE_DEST; • Name indicates the current flash recovery area location. • SPACE_LIMIT indicates the current flash recovery area maximum size. • SPACE_AVAILABLE indicates the space available for storing new backups and archived redo logs. • PERCENT_FULL indicates the current percentage of flash recovery area space used for backups and archived redo logs. Dr. Michel Nabaa Oracle XE 11.2 Administration 21 Managing the Flash Recovery Area(2) Setting the flash recovery area location • To change the flash recovery area location, connect as sysdba and : – Run the SQL command: ALTER SYSTEM SET DB_RECOVERY_FILE_DEST = 'new_path‘; – Run PL/SQL script movelogs.sql that moves the online redo log files to the new flash recovery area location and drops the log files from the old location. On the drive where Oracle XE is installed, movelogs.sql is in the directory : C:\oraclexe\app\oracle\product\11.2.0\server\rdbms\admin Dr. Michel Nabaa Oracle XE 11.2 Administration 22 Managing the Flash Recovery Area(2) • Do not manually delete the contents of the old flash recovery area using operating system utilities. • Make the backups in the old flash recovery area location obsolete by backing up your database twice after you change the location of the flash recovery area. • Only the two most recent backups and accompanying archived redo logs are retained. Dr. Michel Nabaa Oracle XE 11.2 Administration 23 Managing the Flash Recovery Area(3) Changing the flash recovery area size • To change the flash recovery area size, connect as sysdba and run the SQL command: ALTER SYSTEM SET DB_RECOVERY_FILE_DEST_SIZE = new_size; where new_size is of the format nK (kilobytes), nM (megabytes) or nG (gigabytes). Dr. Michel Nabaa Oracle XE 11.2 Administration 24 Managing Users and Security • • • • • • • • • About User Accounts User privileges and roles Administrative Accounts and Privileges The SYS and SYSTEM Users The SYSDBA system Privilege Logging in as an Administrator Changing Administrative User Passwords Managing Database Users Predefined User Accounts Dr. Michel Nabaa Oracle XE 11.2 Administration 25 About User Accounts • A user account is identified by a user name and defines the user's attributes, including the following: – – – – Password for database authentication Privileges and roles Default tablespace for database objects Default temporary tablespace for query processing workspace • When you create a user, you are also implicitly creating a schema for that user. A schema is a logical container for the database objects (such as tables, views, triggers, and so on) that the user creates. • When you drop (delete) a user, you must either first drop all the user's schema objects, or use the cascade feature. Dr. Michel Nabaa Oracle XE 11.2 Administration 26 User privileges and roles • Two main types of user privileges: – System privileges are privileges given to users to allow them to perform certain functions that deal with managing the database and the server, for example CREATE USER, CREATE TABLE, CREATE SESSION, … – Object privileges allow users to perform certain actions upon certain database objects, where database objects are things like tables, stored procedures, indexes, etc …, for example DELETE and/or SELECT from a particular table • GRANT statement is used to grant privileges • REVOKE statement is used to revoke privileges Dr. Michel Nabaa Oracle XE 11.2 Administration 27 User privileges and roles • A role is a named group of related privileges • System and object privileges are granted to roles and roles are granted to users. • Predefined roles in Oracle XE – DBA role enables a user to perform most administrative functions, ex creating users and granting privileges; creating and granting roles; creating and dropping schema objects in other users' schemas; and more. It grants all system privileges. It is by default granted to user SYSTEM. You should be very cautious about assigning the DBA role to any other database users. – Use of the CONNECT and RESOURCE roles is discouraged. • Instead, grant only those privileges that the specific user will need. Dr. Michel Nabaa Oracle XE 11.2 Administration 28 The SYSTEM and SYS Users (1) • Administrative user accounts, automatically created when you install Oracle Database XE. They are both created with the password that you supplied upon installation. • SYSTEM : – Automatically granted the DBA role – Used to perform all administrative functions other than starting up and shutting down the database. – Never use the SYSTEM schema to store tables of interest to non-administrative users. Dr. Michel Nabaa Oracle XE 11.2 Administration 29 The SYS and SYSTEM Users (2) • SYS : – automatically granted the DBA role – Owns the base tables and views for the database data dictionary – the default schema when you connect as SYSDBA – Tables in the SYS schema are manipulated only by the database. – No one should create any tables in the schema of user SYS. – Database users should not connect to the Oracle database using the SYS account. – User SYSTEM is preferred for all administrative tasks Dr. Michel Nabaa Oracle XE 11.2 Administration 30 The SYSDBA System Privilege • SYSDBA is a system privilege assigned only to user SYS to perform high-level administrative tasks such as starting up and shutting down the database. • To connect to the database with the SYSDBA privilege from the SQL Command Line: • SQL > connect sys/password as sysdba password is the password for the SYS user account. Dr. Michel Nabaa Oracle XE 11.2 Administration 31 Logging In as an Administrator • Three ways to log in to Oracle Database XE to perform administrative tasks: – Log in as user SYSTEM – Log in as a user who has been granted the DBA role – Log in and connect to the database as SYSDBA SQL> CONNECT SYS/<password> AS SYSDBA; Dr. Michel Nabaa Oracle XE 11.2 Administration 32 Changing Administrative User Passwords • To change the password for user SYS or SYSTEM Using the SQL Command Line – Connect to the database as SYSDBA – Enter one of the following commands: • ALTER USER SYS IDENTIFIED BY newpassword; • ALTER USER SYSTEM IDENTIFIED BY newpassword; Dr. Michel Nabaa Oracle XE 11.2 Administration 33 Managing Database Users • • • • • Creating users Altering users Locking and unlocking user accounts Expiring a user password Dropping users Dr. Michel Nabaa Oracle XE 11.2 Administration 34 Managing Database Users • To perform the managing database users operations in the SQL Developer Connections navigator 1. Open a connection to the SYSTEM user. 2. In the nodes under this SYSTEM connection, expand Other Users. – To create a new database user, right-click the Other Users node. – To perform an action on a database user, right-click that user in the hierarchy and select the appropriate command (Edit User or Drop User). Dr. Michel Nabaa Oracle XE 11.2 Administration 35 Predefined User Accounts Dr. Michel Nabaa Oracle XE 11.2 Administration 36 Data Dictionary • Set of read-only tables and views containing information about the database including schemas, users, privileges, and even auditing data. – ‘Metadata’ information is stored inside the database – Can be read and queried just like any standard table Dr. Michel Nabaa Oracle XE 11.2 Administration 37 Data Dictionary • More than 1200 data dictionary views in 11g – Static views ex USER_TABLES – Dynamic views ex V$PARAMETER – Global dynamic views ex GV$PARAMETER – Undocumented views ex X$KSPPI Dr. Michel Nabaa Oracle XE 11.2 Administration 38 Static Views • Contain data about objects, privileges, users, etc • Contents change with DDL operations • Three types – USER_ • Contains only information about current user objects • Ex USER_TABLES – ALL_ • Contains only information about current user objects and objects he is privileged to use • Ex ALL_TABLES – DBA_ • Contains all objects, use restricted to DBA • Ex DBA_TABLES Dr. Michel Nabaa Oracle XE 11.2 Administration 39 Common Static DBA Views Dr. Michel Nabaa Oracle XE 11.2 Administration 40 Dynamic Performance Views • They are continuously updated while a database is open and in use • Their contents relate primarily to performance. • Common dynamic views: Dr. Michel Nabaa Oracle XE 11.2 Administration 41 Miscellaneous Views • SESSION_PRIVS – Active session privileges • DICTIONARY – Names and descriptions of all data dictionary views available to the current user • DICT_COLUMNS – Column descriptions for views available to the current user • TABLE_PRIVILEGES – Object privileges for the current user Dr. Michel Nabaa Oracle XE 11.2 Administration 42 Monitoring the Database • As an administrator, you can monitor the activities of the database and its users. – Monitoring Sessions – Monitoring the Top SQL Statements – Monitoring Long Operations Dr. Michel Nabaa Oracle XE 11.2 Administration 43 Monitoring Sessions • SQL Developer enables you to monitor the current database sessions: – to determine the users who are currently logged in to the database – to determine what applications the users are running. – To kill a session Dr. Michel Nabaa Oracle XE 11.2 Administration 44 Viewing Sessions 1. In SQL Developer, click Tools, then Monitor Sessions. 2. In the Select Connection dialog box, select a connection to SYSTEM (or another account with full DBA privileges). A Sessions tab is displayed. 3. (Optional) Right-click in any row in the display, and explore the options available as shown in the context menu commands, which include Trace Session, Kill Session, and Find/Highlight (to search for rows in the grid that contain a specified text string). Dr. Michel Nabaa Oracle XE 11.2 Administration 45 Monitoring the Top SQL Statements (1) • The "top" SQL statements are the SQL statements that: – are executed most often – use more system resources than other SQL statements, or – use system resources more frequently than other SQL statements. • Viewing the top SQL statements enables you to focus your SQL tuning efforts on the statements that can have the most impact on database performance. Dr. Michel Nabaa Oracle XE 11.2 Administration 46 Monitoring the Top SQL Statements (2) • To monitor the top SQL statements: 1. In SQL Developer, click the Reports navigator tab, and expand the hierarchy as follows: All Reports, then Data Dictionary Reports, then Database Administration, then Top SQL. 2. Under Top SQL, select any of the listed "Top SQL by“ reports: Buffer Gets, Buffer Gets/Rows Proc, CPU, Disk Reads, Executions, or Waits. Dr. Michel Nabaa Oracle XE 11.2 Administration 47 Killing a Session • Killing a Session logs it off and disconnects the user running the session. • Reasons to kill a session : 1. The session is not responding. 2. You want to perform an administrative function that requires all users to log off first, but the user is not available to end his or her session. • To kill a session 1. 2. 3. In SQL Developer, click Tools, then Monitor Sessions. In the Select Connection dialog box, select a connection to SYSTEM (or another account with full DBA privileges). Right-click in the row for the session to be terminated, and select Kill Session. Dr. Michel Nabaa Oracle XE 11.2 Administration 48 Monitoring Long Operations • To monitor long operations: 1. In SQL Developer, click the Reports navigator tab, and expand the hierarchy as follows: All Reports, then Data Dictionary Reports, then Database Administration, then Sessions. 2. Under Sessions, select Active Sessions. 3. If you are asked to select a connection, select one for SYS AS SYSDBA. 4. Check the UP_TIME value for each listed session, and note any that you consider to be longer than desired or expected. Dr. Michel Nabaa Oracle XE 11.2 Administration 49 Viewing Database Globalization Information • To view database version information: 1. In SQL Developer, click the Reports tab on the left, near the Connections navigator. (If this tab is not visible, click View, then Reports.) 2. In the Reports navigator, expand Data Dictionary Reports. 3. Under Data Dictionary Reports, expand About Your Database. 4. Under About Your Database, click National Language Support Parameters. Dr. Michel Nabaa Oracle XE 11.2 Administration 50 Exporting & Importing Metadata and Data • You can export and import metadata (database object definitions), data, or both metadata and data in two ways: 1. Using SQL Developer for Exporting and Importing data 2. Using Other Tools for Exporting and Importing data • For more convenience, you may want to use SQL Developer for export and import operations Dr. Michel Nabaa Oracle XE 11.2 Administration 51 Exporting & Importing Metadata and Data • Using SQL Developer for Exporting and Importing data – Example: Exporting Metadata and Data for a Table – Example: Importing Metadata and Data Using a Script File – Example: Exporting Data to a Microsoft Excel File – Example: Importing Data from a Microsoft Excel File https://docs.oracle.com/cd/E17781_01/server.112/ e18804/impexp.htm#ADMQS258 Dr. Michel Nabaa Oracle XE 11.2 Administration 52 Exporting & Importing Metadata and Data • In SQL Developer, click Tools, then Database Export. Dr. Michel Nabaa Oracle XE 11.2 Administration 53 Backing Up and Recovering • Backing Up and Restoring the Database • Viewing and Restoring Historical Data with Flashback Query • Recovering Dropped Tables Dr. Michel Nabaa Oracle XE 11.2 Administration 54 Backing Up and Restoring the Database • About Backing Up and Restoring the Database • Enabling ARCHIVELOG Mode for Media Failure Protection • Backing Up the Database • Scheduling Automatic Backups • Restoring and Recovering the Database Dr. Michel Nabaa Oracle XE 11.2 Administration 55 About Backing Up and Restoring the Database • Based on protecting the physical files that make up the database • Oracle Database XE uses the Recovery Manager (RMAN) utility • Oracle Database XE includes backup and restore scripts to perform a full backup and restore of the entire database, and store backup files in the flash recovery area. • Automatic Management of Backup Storage Dr. Michel Nabaa Oracle XE 11.2 Administration 56 Viewing the Current ARCHIVELOG Mode Setting 1. Using the SQL Command Line, log in to the database and connect as SYSDBA 2. Enter the following command: SELECT log_mode FROM v$database; 3. The LOG_MODE value will be either ARCHIVELOG (that is, on) or NOARCHIVELOG (that is, off). Dr. Michel Nabaa Oracle XE 11.2 Administration 57 Turning on ARCHIVELOG Mode 1. 2. Using the SQL Command Line, log in to the database and connect as SYSDBA. At the SQL Command Line prompt, enter : SHUTDOWN IMMEDIATE 3. At the SQL Command Line prompt, enter : STARTUP MOUNT 4. 5. SQL> ALTER DATABASE ARCHIVELOG; SQL > ALTER DATABASE OPEN; The database is now running with the new ARCHIVELOG mode setting. Change the size of the flash recovery area to at least 15 gigabytes to allow for the extra space required for archived log files. ALTER SYSTEM SET DB_RECOVERY_FILE_DEST_SIZE = 20G; 6. 7. To display space limit and used space SELECT * FROM V$RECOVERY_FILE_DEST; Dr. Michel Nabaa Oracle XE 11.2 Administration 58 Backing Up the Database (1) • In ARCHIVELOG mode, the provided backup script performs online backups of a database. • In NOARCHIVELOG mode, backups are performed offline. • Click Start, point to Programs (or All Programs), point to Oracle Database 11g Express Edition, and then select Backup Database. Dr. Michel Nabaa Oracle XE 11.2 Administration 59 Backing Up the Database (2) • Logs containing the output from the last two backups in Windows are stored in the locations : – C:\oraclexe\app\oracle\product\11.2.0\server\database\O XE_BACKUP_CURRENT.LOG. – C:\oraclexe\app\oracle\product\11.2.0\server\database\O XE_BACKUP_PREVIOUS.LOG. • The database backup is stored in the directory – C:\oraclexe\app\oracle\oradata\XE • To schedule automatic backups, use any task scheduling software to run the supplied backup script : – C:\oraclexe\app\oracle\product\11.2.0\server\bin\Back up.bat Dr. Michel Nabaa Oracle XE 11.2 Administration 60 Restoring and Recovering the Database (1) • In ARCHIVELOG mode, the restore script restores the backed up database files, and then uses the online and archived redo log files to recover the database to the state it was in before the software or media failure occurred. • In NOARCHIVELOG mode, the restore script restores the database to its state at the last backup. Any transactions that took place after the last backup are lost. • Restore script is lunched by running the Restore Database command. • Click Start, point to Programs (or All Programs), point to Oracle Database 11g Express Edition, and then select Restore Database. Dr. Michel Nabaa Oracle XE 11.2 Administration 61 Restoring and Recovering the Database (2) • Restoring and Recovering the Database are based on the following assumptions: – A software failure, media (disk) failure, or operator error caused the loss or corruption of one or more database files, rendering the database unusable. – In the flash recovery area, the backup sets and, if in ARCHIVELOG mode, archived logs, are intact and available. – If in ARCHIVELOG mode, the online redo logs are intact and available. If not, the database is restored to the point of the last transaction included in an archived log. – The Oracle Database XE host computer and operating system are operational. – The Oracle Database XE installed software (binaries) are intact and operational. Dr. Michel Nabaa Oracle XE 11.2 Administration 62 Viewing and Restoring Historical Data with Flashback Query • The Flashback Query feature of Oracle Database XE enables you to retrieve data at a point in time in the past from a table or materialized view. You can then reconstruct lost data that was deleted or changed by accident. • The clause AS OF added to the SELECT statement specifies either a time or a System Change Number (SCN). The query then uses the committed data from the corresponding time. • SCN is a stamp that defines a committed version of a database at a point in time. Dr. Michel Nabaa Oracle XE 11.2 Administration 63 Example: Recovering Data with Flashback Query • The example retrieves the state of the record for Hanna at 9:30 a.m., April 4, 2017 and restores Hanna's information to the employees table. SELECT * FROM employees AS OF TIMESTAMP TO_TIMESTAMP ('2017-04-04 09:30:00', 'YYYY-MM-DD HH:MI:SS') WHERE last_name = ‘Hanna'; INSERT INTO employees (SELECT * FROM employees AS OF TIMESTAMP TO_TIMESTAMP('2017-04-04 09:30:00', 'YYYY-MM-DD HH:MI:SS') WHERE last_name = ‘Hanna'); Dr. Michel Nabaa Oracle XE 11.2 Administration 64