Oracle 12c – Use Table enhancements

You can now make columns within a table invisible and general access will only display visible tables.  The following queries will not display invisible columns:

  1. Select * from
  2. Describe command
  3. %ROWTYPE declaration in PL/SQL
  4. Describe on OCI

 

Invisible columns will be displayed if they are explicitly called by column name and you can insert data by explicitly naming the column, but if insert does not list column names you cannot insert into invisible columns.  Columns can be made invisible during table creation, adding columns, or modifying them.  Virtual columns can be invisible also.  You can use an invisible column as a partition key.

 

Invisible columns have the following restrictions.

  1. External, cluster, and temporary tables cannot have invisible columns.
  2. Attributes of user-defined types cannot be invisible.

 

 

Example of Column invisibility using SCOTT.EMP table.

 

 

  1. Logon to SQLPLUS as the user SCOTT

 

[oracle@linux2 ~]$ sqlplus scott/password

SQL*Plus: Release 12.1.0.2.0 Production on Thu Dec 29 14:14:06 2016

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Last Successful login time: Thu Dec 29 2016 13:47:09 -05:00

 

Connected to:

Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 – 64bit Production

With the Partitioning, OLAP, Advanced Analytics, Real Application Testing

and Unified Auditing options

 

SQL>

 

  1. select table_name, column_name, hidden_column from USER_TAB_COLS.

 

SQL>  column table_name format a10

SQL> column column_name  format a10

SQL> select table_name, column_name, hidden_column from user_tab_cols where table_name=’EMP’;

 

TABLE_NAME COLUMN_NAM HID

———- ———- —

EMP        EMPNO      NO

EMP        ENAME      NO

EMP        JOB        NO

EMP        MGR        NO

EMP        HIREDATE   NO

EMP        SAL        NO

EMP        COMM       NO

EMP        DEPTNO     NO

 

8 rows selected.

 

SQL>

 

 

NOTE:  None of the columns are hidden.

 

  1. Now select the first three rows from table SCOTT.EMP.

 

SQL> set linesize 200

SQL>  select * from scott.emp where rownum<4;

 

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO

———- ———- ——— ———- ——— ———- ———- ———-

      7369 SMITH      CLERK           7902 17-DEC-80        800                    20

      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30

      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30

 

SQL>

 

  1. Now alter the table SCOTT.EMP and make column HIRDATE, SAL, COMM invisible.

 

alter table scott.emp modify hiredate invisible;

alter table scott.emp modify sal invisible;

alter table scott.emp modify comm invisible;

 

SQL> alter table scott.emp modify hiredate invisible;

alter table scott.emp modify sal invisible;

alter table scott.emp modify comm invisible;

 

Table altered.

SQL>

Table altered.

SQL>

Table altered.

SQL>

 

  1. Select the first 3 rows of table SCOTT.EMP.

 

SQL>  select * from scott.emp where rownum<4;

 

     EMPNO ENAME      JOB              MGR     DEPTNO

———- ———- ——— ———- ———-

      7369 SMITH      CLERK           7902         20

      7499 ALLEN      SALESMAN        7698         30

      7521 WARD       SALESMAN        7698         30

SQL>

 

NOTE:  You no longer see the columns HIREDATE, SAL, or COMM

 

 

  1. Now desc the table SCOTT.EMP.

 

SQL> desc scott.emp;

 Name                                Null?                        Type

 ————                           ——–                   ——————————————-

 EMPNO                             NOT NULL             NUMBER(4)

 ENAME                                                          VARCHAR2(10)

 JOB                                                                 VARCHAR2(9)

 MGR                                                               NUMBER(4)

 DEPTNO                                                         NUMBER(2)

SQL>

 

NOTE:  You no longer see the columns HIREDATE, SAL, or COMM

 

 

 

  1. Now select the first 3 values but explicitly request the columns, NOTE they are still there but need to be explicitly queried.

 

SQL> select EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO from scott.emp where rownum<4;

 

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO

———- ———- ——— ———- ——— ———- ———- ———-

      7369 SMITH      CLERK           7902 17-DEC-80        800                    20

      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30

      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30

SQL>

 

  1. Additionally the hidden_columns value in user_tab_cols will also be updated accordingly.

 

SQL> column table_name format a10

column column_name  format a10

select table_name, column_name, hidden_column from user_tab_cols where table_name=’EMP’;

SQL> SQL>

TABLE_NAME COLUMN_NAM HID

———- ———- —

EMP        EMPNO      NO

EMP        ENAME      NO

EMP        JOB        NO

EMP        MGR        NO

EMP        HIREDATE   YES

EMP        SAL        YES

EMP        COMM       YES

EMP        DEPTNO     NO

8 rows selected.

SQL>

 

  1. This completes using invisible columns in Oracle 12c.

 

Larry Catt

OCP

Oracle 12c – Explain the fundamentals of Database architecture

Database vs Database Instance:

Database – Set of files residing on OS layer that store data, do not require database instance.

Database Instance – Set of memory structures that manage database files.  An instance includes shared memory area and set of background processes. Instances can exist independent of database files.

 

 

Basic vs RAC Configuration:

Basic – In Oracle 12c Basic configuration, you have one database instance per database.

RAC – In an Oracle 12c RAC configuration you have multiple instances per database which allows for improved scalability, performance and fault tolerance.

 

Primary Database vs Data Guard Database:

Primary Database – Is the database which performs all actual processing and storage of the RDBMS for active application processes.  A primary database can be related to zero or many standby databases.  Data Guard Database – Data Guard Databases are standby databases which draw changes from a primary database.  These standby databases can be: physical (byte for byte copies, kept in sync by applying primary database redo logs) or logical (where they are kept in sync by SQL statements by Oracle Streams).

 

 

Memory Structures of Oracle Instance:

System Global Area (SGA) – shared memory structures that contain data and control info for single instance.  SGA is shared by all server and background processes. Allocation and deallocation of space is performed in units call granules.   You can view components in view V$SGASTAT.  Examples include cached data blocks and shared SQL areas.

Program Global Area (PGA) – PGA is not shared memory.  It contains data and control information for single oracle process.   One PGA is created at start of server process and background process.  Total size of Instance PGA is total space of all PGA.

User Global Area (UGA) – Memory associated to just a single user session.

Software Code Areas – Memory area to store code being executed or can be executed.

 

 

Parts of SGA:

  1. Database Buffer Cache – Contains data blocks read from data files. All users share the buffer cache and it optimizes physical I/O by keeping frequently accessed blocks in memory.  It uses Least Recently Used (LRU) to determine what blocks to keep.
  2. Redo Log Buffer – Circular buffer that contains all changes by DML and DDL and allows for recovery. Redo use continuous space in buffer and LGWR process writes them to disk.
  3. Shared Pool – Caches program data required by server to include SQL, PL/.SQL code, system parameters, data dictionary info. Every SQL executed requires access to shared pool.
  4. Large Pool – optional memory area, used for large memory allocation which are too big for shared pool, such as UGA, Oracle XA interface, and RMAN.
  5. Java Pool – Stores session specific Java code and data within JVM.
  6. Steams Pool – used by Oracle Streams. Stores buffered queue messages and stream capture and apply process.  It begins with size of 0 and grows dynamically as needed.
  7. Fixed SGA – Internal housekeeping area, contains general information about database and instance that other background processes need. Size of fixed SGA is set by Oracle database and can’t be changed.

 

 

 

Larry Catt

OCP

Create PDB in silent mode

In Oracle 12c RDBMS you can use the Database Configuration Assistant to create a PDB from shell scripts or command line in an existing CDB.    You can provide all of the necessary parameters for the PDB creation through a response file or at the command prompt.   In this example we will provide all required command line option to create new PDB called pdb1 in the CDB1 container.

 

  1. Logon to your Oracle server as the Oracle software owner.

 

[root@linux2 ~]# su – oracle

Last login: Wed Oct  5 15:10:16 EDT 2016 on pts/2

[oracle@linux2 ~]$

 

  1. Define the following parameter with values:
    1. -sourceDB = the name of the CDB database you wish to create your PDB in.
    2. -pdbName = the name of the new PDB database.
    3. -pdbAdminPassword = password for the PDB admin user.

 

  1. You will have to also supply the tags: –silent and  -createPluggableDatabase

 

  1. Update the below DBCA command with the values defined in step 2.

 

dbca -silent -createPluggableDatabase -sourceDB cdb1 -pdbName pdb1 -pdbAdminPassword password

 

  1. Example execution.

 

[oracle@linux2 admin]$ dbca -silent -createPluggableDatabase -sourceDB cdb1 -pdbName pdb1 -pdbAdminPassword password

Creating Pluggable Database

4% complete                                                                                                                                         

12% complete

21% complete

38% complete

85% complete

Completing Pluggable Database Creation

100% complete

Look at the log file “/opt/app/oracle/cfgtoollogs/dbca/cdb1/pdb1/cdb1.log” for further details.

[oracle@linux2 admin]$

 

  1. View the status of the listener process to validate that the new PDB has been picked up by the listener with the command: lsnrctl status

 

[oracle@linux2 admin]$ lsnrctl status

 

LSNRCTL for Linux: Version 12.1.0.2.0 – Production on 20-OCT-2016 10:21:44

 

Copyright (c) 1991, 2014, Oracle.  All rights reserved.

 

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.30.15.75)(PORT=1521)))

STATUS of the LISTENER

————————

Alias                     LISTENER

Version                   TNSLSNR for Linux: Version 12.1.0.2.0 – Production

Start Date                19-OCT-2016 14:06:54

Uptime                    0 days 20 hr. 14 min. 50 sec

Trace Level               off

Security                  ON: Local OS Authentication

SNMP                      OFF

Listener Parameter File   /opt/app/oracle/product/12.1.0.2/db_1/network/admin/listener.ora

Listener Log File         /opt/app/oracle/diag/tnslsnr/linux2/listener/alert/log.xml

Listening Endpoints Summary…

  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.30.15.75)(PORT=1521)))

Services Summary…

Service “cdb1” has 2 instance(s).

  Instance “cdb1”, status UNKNOWN, has 1 handler(s) for this service…

  Instance “cdb1”, status READY, has 1 handler(s) for this service…

Service “cdb1XDB” has 1 instance(s).

  Instance “cdb1”, status READY, has 1 handler(s) for this service…

Service “pdb1” has 1 instance(s).

  Instance “cdb1”, status READY, has 1 handler(s) for this service…

The command completed successfully

[oracle@linux2 admin]$

 

 

  1. Update the tnsnames.ora file with the reference to newly created PDB1 with the command:

 

echo -e “PDB1 =\n” \

”  (DESCRIPTION =\n” \

”    (ADDRESS_LIST =\n” \

”      (ADDRESS = (PROTOCOL = TCP)(HOST = “`hostname -i`”)(PORT = 1521))\n” \

”    )\n” \

”    (CONNECT_DATA =\n” \

”      (SERVER = DEDICATED)\n”  \

”      (SERVICE_NAME = pdb1)\n” \

”    )\n” \

”  )\n” >> $ORACLE_HOME/network/admin/tnsnames.ora

 

Example execution:

 

[oracle@linux2 admin]$ echo -e “PDB1 =\n” \

> ”  (DESCRIPTION =\n” \

> ”    (ADDRESS_LIST =\n” \

> ”      (ADDRESS = (PROTOCOL = TCP)(HOST = “`hostname -i`”)(PORT = 1521))\n” \

> ”    )\n” \

> ”    (CONNECT_DATA =\n” \

”      (SERVER = DEDICATED)\n”  \

”      (SERVICE_NAME = pdb1)\n” \

”    )\n” \

”  )\n” >> $ORACLE_HOME/network/admin/tnsnames.ora> ”      (SERVER = DEDICATED)\n”  \

> ”      (SERVICE_NAME = pdb1)\n” \

> ”    )\n” \

> ”  )\n” >> $ORACLE_HOME/network/admin/tnsnames.ora

[oracle@linux2 admin]$

 

 

  1. Test connection to new PDB with the command: tnsping pdb1

 

[oracle@linux2 admin]$ tnsping pdb1

 

TNS Ping Utility for Linux: Version 12.1.0.2.0 – Production on 20-OCT-2016 10:32:32

 

Copyright (c) 1997, 2014, Oracle.  All rights reserved.

 

Used parameter files:

 

 

Used TNSNAMES adapter to resolve the alias

Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.30.15.75)(PORT = 1521))) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = pdb1)))

OK (0 msec)

[oracle@linux2 admin]$

 

 

  1. This completes the creation of new PDB in silent mode.

 

Larry Catt

OCP

Oracle 12c – Real-Time Database Operation Monitoring

Oracle 12c provides the ability to monitor PL/SQL operations within the database in real-time.  Database operations are either simple or composite.   Simple operations are single SQL or PL/SQL procedure/function and Composite operations are activity between two points in time.  A session doing multiple transactions, a given session can only be involved in one composite operation at a time.  This article outlines the basics involved in real-time database operation monitoring.

 

 

  1. Real time SQL monitoring was introduced in 11g but only supported simple operations,12c supports composite operation monitoring, which allows you the ability to monitor a logical grouping of actions to support a particular business requirement.
  2. Real-time Monitoring starts automatically when SQL statements run in parallel or consume more than 5 seconds of CPU or I/O time.
  3. Real-Time monitoring can be viewed via Cloud Control on Monitor SQL Execution page which is the preferred method, allowing you to drill down. It can also be monitored via the data dictionary views, or DBMS_SQL_MONITOR package.
  4. Data Dictionary views:
    1. V$SQL_MONITOR
    2. V$SQL_MONITOR_SESSTAT
    3. V$SQL_PLAN_MONITOR

 

  1. DBMS_SQL_MONITOR package provides the following functions and procedure
    1. REPORT_SQL_MONITOR – gives detailed report.
    2. BEGIN_OPERATION – starts monitoring a session’s performance.
    3. END_OPERATION – ends monitoring a session’s performance

 

  1. SQL Monitoring is automatic when STATISTICS_LEVEL is set to TYPICAL or ALL and Oracle will begin monitoring long running queries automatically.
  2. The init parameter CONTROL_MANAGEMENT_PACK_ACCESS must be set to default of DIAGNOSTIC+TUNING to use this feature.
  3. Hints of MONITOR and NO_MONITOR can be used to force or stop monitoring.

 

SELECT /*+MONITOR*/ first_name from emp;

 

  1. Use the BEGIN_OPERATION and END_OPERATION function of DBMS_SQL_MONITOR package to monitor entire session.

 

 

Larry Catt

OCP

Oracle 12c – Create a default permanent tablespace

You can create a default permanent tablespace for newly created users to avoid them having the SYSTEM tablespace as the default, which is never recommended.  Both the CREATE DATABASE and ALTER DATABASE commands support the DEFAULT TABLESPACE clause.  Command that sets the default tablespace to ‘USERS’ tablespace for all users is:

 

Example:

 

ALTER DATABASE DEFAULT TABLESPACE users;

 

SQL> ALTER DATABASE DEFAULT TABLESPACE users;

Database altered.

SQL>

 

If you are not sure what the default tablespace is for newly created users, you can view the  DATABASE_PROPERTIES to shows the database default tablespace with a property_name of DEFAULT_TEMP_TABLESPACE and DEFAULT_PERMANENT_TABLESPACE.

 

Example:

 

SELECT property_name, property_value FROM database_properties where property_name like ‘%TABLESPACE’;

 

SQL> SELECT property_name, property_value FROM database_properties where property_name like ‘%TABLESPACE’;

PROPERTY_NAME

——————————————————————————–

PROPERTY_VALUE

——————————————————————————–

DEFAULT_TEMP_TABLESPACE

TEMP

DEFAULT_PERMANENT_TABLESPACE

USERS

SQL>

 

Once a default permanent tablespace is defined, all objects for users will be created there unless they have their own default tablespace defined or the create statement defines a different tablespace.

 

 

Larry Catt

OCP

Oracle 12c – Perform daily administration tasks

Oracle 12c does not eliminate the need for daily administrative tasks of a database administrator, however it does have many enhancement that can aid in the identification of problems/sub-par performance.    Additionally, there exist two camps on monitoring the health of a database:  One wanting to automate all monitoring tasks and let the database tell you when something is wrong; and One that wants tasks to remain manual where you keep a closer contact with database structures and are able to determine trends.  Regardless of how you perform your monitoring, the following list of daily DBA tasks is a good general guideline.   NOTE:   This list is not limited to just error checking, a DBA job covers both error correction and error avoidance.

 

  1. Check for any database errors in alert and trace.
  2. Check for any networking errors in listener alert. (other network connectivity logs)
  3. Check for errors in Operating System log files for database servers.
  4. Check for errors in Storage Area Network log files.
  5. Check Space allocation for database storage. (database tablespaces)
  6. Check for Space availability at Operating System Layer.
  7. Check any Application Layer error logs.
  8. Installing, Upgrading, and Patching Oracle Database server software (OS) and application tools. (Test installing, upgrading, and Patching before production implementations)
  9. Allocation of system storage and planning future storage.
  10. Creating primary database storage structure (tablespaces)
  11. Create primary database objects (tables, views, indexes)
  12. Planning backup and recovery procedure.
  13. Consult oracle technical support
  14. Ensure compliance with Oracle license agreement.
  15. Modify database structures.
  16. Managing users and maintain security.
  17. Controlling and Monitoring user activity and privileges.
  18. Monitor and optimize performance.
  19. Maintain archive tape backup/recovery devices (other type of backup device).
  20. Backup and restore database. (including test recovery at least twice a year)

 

Larry Catt

OCP