Friday, November 23, 2012

You should move the database Audit Trail to a Different Tablespace

You may want to move the database audit trail tables to a different tablespace if the SYSTEM tablespace is too busy

Run the DBMS_AUDIT_MGMT.SET_AUDIT_TRAIL_LOCATION PL/SQL procedure to specify the name of the destination tablespace and move it to that tablespace.

BEGIN
 DBMS_AUDIT_MGMT.SET_AUDIT_TRAIL_LOCATION(
  AUDIT_TRAIL_TYPE            => DBMS_AUDIT_MGMT.AUDIT_TRAIL_AUD_STD, 
  AUDIT_TRAIL_LOCATION_VALUE  => 'AUD_AUX');
END;

Friday, November 9, 2012

A way to remove the duplicate records

First you need to query the database to find all duplicate rows, and then you need to run a statement to delete one of each duplicate record that is found

DECLARE
  CURSOR emp_cur IS
  SELECT *
  FROM   employees
  ORDER BY employee_id;
  emp_count         number := 0;
  total_count         number := 0;
BEGIN
  DBMS_OUTPUT.PUT_LINE('You will see each duplicated employee listed more ');
  DBMS_OUTPUT.PUT_LINE('than once in the list below.  This will allow you to ');
  DBMS_OUTPUT.PUT_LINE('review the list and ensure that indeed...there are more ');
  DBMS_OUTPUT.PUT_LINE('than one of these employee records in the table.');
  DBMS_OUTPUT.PUT_LINE('Duplicated Employees: ');
-- Loop through each player in the table
  FOR emp_rec IN  emp_cur LOOP
-- Select the number of records in the table that have the same ID as the current record
     SELECT count(*)
     INTO emp_count
     FROM employees
     WHERE employee_id = emp_rec.employee_id; 

-- If the count is greater than one then a duplicate has been found, so print it out.
     IF emp_count > 1 THEN
        DBMS_OUTPUT.PUT_LINE(emp_rec.employee_id || ' - ' || emp_rec.first_name ||
                   ' ' || emp_rec.last_name || ' - ' || emp_count);
                  total_count := total_count + 1;
     END IF;

  END LOOP;
END;



Next, you need to delete the duplicated rows that have been found. The following DELETE statement
will ensure that one of the duplicates is removed:

DELETE FROM employees A WHERE ROWID > (
SELECT min(rowid) FROM employees B
WHERE A.employee_id = B.employee_id);

When to use SYS_CONTEXT function

The SYS_CONTEXT function allows you to define a namespace and then place parameters
within it so that they can be retrieved for use at a later time. The general syntax for the use of
SYS_CONTEXT is as follows:

SYS_CONTEXT('namespace','parameter'[,length])

A namespace can be any valid SQL identifier, and it must be created using the CREATE_CONTEXT
statement. The parameter must be a string or evaluate to a string, and it must be set using the
DBMS_SESSION.SET_CONTEXT procedure. The call to SYS_CONTEXT with a valid namespace and parameter will result in the return of a value that has a VARCHAR2 datatype.
The USERENV namespace is automatically available for use because it is a built-in namespace
provided by Oracle. The USERENV namespace contains session information for the current user.

query the database for the user’s information

If you want to obtain environment and session information such as the name and IP address of the
current user so that the values can be stored into local variables for logging purposes you can make use of the SYS_CONTEXT built-in function to query the database for the user’s information. Once you
have obtained the information, then store it into a local variable. At that point, you can do whatever
you’d like with it, such as save it in a logging table.

DECLARE
  username      varchar2(100);
  ip_address    varchar2(100); 


BEGIN
  SELECT SYS_CONTEXT('USERENV','SESSION_USER'), SYS_CONTEXT('USERENV','IP_ADDRESS')
  INTO username, ip_address
  FROM DUAL;

  DBMS_OUTPUT.PUT_LINE('The connected user is: ' || username || ', and the IP address
 is ' ||
                                                      ip_address);
END;