Sunday, June 30, 2013

Setting default boot time out in Solaris 10 and later

To locate the menu.lst file, type:


# /sbin/bootadm list-menu
The location and contents of the file are displayed.

The location for the active GRUB menu is: /boot/grub/menu.lst
default 0
timeout 10
0 Solaris
1 Solaris failsafe
2 second_disk
3 second_disk failsafe

edit this file in  /boot/grub/menu.lst.


To edit time out from 10 sec to 1 Sec


login as: root
Using keyboard-interactive authentication.
Password:
Last login: Sun Jun 30 14:56:50 2013 from 192.168.1.18
Sun Microsystems Inc.   SunOS 5.10      Generic January 2005
You have new mail.
ROOT@ORASUN02$ /sbin/bootadm list-menu
The location for the active GRUB menu is: /boot/grub/menu.lst
default 0
timeout 1
0 Solaris 10 10/09 s10x_u8wos_08a X86
1 Solaris failsafe
ROOT@ORASUN02$


Cheers
Rajani


Friday, June 28, 2013

Diffrence between %type and %rowtype

-- %TYPE is used to declare a field with the same type as
-- that of a specified table's column:

DECLARE
   v_EmpName  emp.ename%TYPE;
BEGIN
   SELECT ename INTO v_EmpName FROM emp WHERE ROWNUM = 1;
   DBMS_OUTPUT.PUT_LINE('Name = ' || v_EmpName);
END;
/



-- %ROWTYPE is used to declare a record with the same types as
-- found in the specified database table, view or cursor:

DECLARE
  v_emp emp%ROWTYPE;
BEGIN
  v_emp.empno := 10;
  v_emp.ename := 'Rajani';
   v_emp.dept := 'IT';
 
END;
/
 

Thursday, June 20, 2013

How to display logname @ hostname in command prompt for Solaris 10

Hi Friends,

By default Solaris prompts you with $ for individual users and # for root user.

But if you want to modify your login terminal , please follow below steps.

You can either edit it though VI editor else use winscp to edit it remotely .

The advantage of WINSCP over File Zilla is it opens the file in ANSI / UNIX mode and modifies remotely without locally downloading.

If we want to set it locally for individual users:

Logged in as fred user :

/export/home/fred

# This is the .profile file of default home directory of user fred.
# This is the default standard profile provided to a user.
# They are expected to edit it to meet their own needs.

#MAIL=/usr/mail/${LOGNAME:?}
export PS1="$(logname)@$(hostname) $" 

--------------------------------------------------------------------------------------------------------------------
If we want to set it Globally for every user including the root.

Logged in as root user 

/etc/profile 

# This is the /etc/profile loggen in as root user to change the login prompt.
#iDent "@(#)profile 1.19 01/03/13 SMI" /* SVr4.0 1.3 */

# The profile that all logins get before using their own .profile.
trap ""  2 3
export LOGNAME PATH

if [ "$TERM" = "" ]
then
if /bin/i386
then
TERM=sun-color
else
TERM=sun
fi
export TERM
fi

# Login and -su shells get /etc/profile services.
# -rsh is given its environment in its .profile.

case "$0" in
-sh | -ksh | -jsh | -bash)

if [ ! -f .hushlogin ]
then
/usr/sbin/quota
# Allow the user to break the Message-Of-The-Day only.
trap "trap '' 2"  2
/bin/cat -s /etc/motd
trap "" 2

/bin/mail -E
case $? in
0) 
echo "You have new mail."
  ;;
2) 
echo "You have mail."
  ;;
esac
fi
esac

umask 022
trap  2 3
# Added below lines with logged in as root user to modify the login terminal for root and  individual users. 
#
if [ "$LOGNAME" = "root" ]
then
PS1="ROOT@`uname -n`$ " ; export PS1
else
PS1="$LOGNAME@`uname -n`$ " ; export PS1
fi

Wednesday, June 19, 2013

How can you call a form on a form

Hi,

We can call a form in 3 different ways :

1.Call Form
2.New Form
3.Open Form

The communication between forms can be done in the following 3 ways
let say Form A calls FormB

1.Using New_Form: this Terminates Form A and activates Form B

The NewForm will terminates your current active Form A and releases the memory which is used by Form A and Then activates the called Form B.

2.Using Call_Form:
This suspends the Form A and activates Form B.When you close Form B then Form A will active again.
User cant able to navigate between Forms A and B.

3.Using Open Form:
It opens the indicated form B.
User can be able to navigate between forms A and B.
This is used in multi form applications.

The above is just brief on "Functionality".You have to research more when you are implementing.

Cheers
Rajani

Thursday, June 13, 2013

How to generate a spool CSV file with Current Date

PROMPT
PROMPT *** my_script.sql *** START ***
PROMPT
set lines 420
set trimspool on
setpages 0
set head offset
termout offset
feedback offset
echo off
col dcol new_value mydate noprint
select to_char(sysdate,'YYYY-MM-DD') dcol from dual;
spool C:\&mydate._MY_SPOOL_FILENAME.csv

select 'INIT,'|| e.employee_id ||','|| e.department_id||','||d.manager_id||','|| d.department_id
from employees e, departments d
where e.department_id = d.department_id
order by 1
/
spool off
set echo on
set termout on
PROMPT
PROMPT *** my_script.sql *** END ***
PROMPT --Explanation:
/* The first query puts the date in the substitution variable "mydate". There is no visible output from this query because of the NOPRINT option in the COLUMN command. In the SPOOL command the first period (.) indicates the end of the variable name and is not included in the resulting string. If "mydate" contained "2013-05-14" from the first query then the spool file name would be "2013-05-14_MY_SPOOL_FILENAME.csv ". */

-- The Spool path C:\ means in 'C' drive for windows and
-- for Unix : Spool &mydate._MY_SPOOL_FILENAME.csv (file will be spooled in working directory)

Hope this clears all your Doubt.

Cheers
Rajani

Database Schema Objects

Schema Objects:

  1. Tables
  2. Views
  3. Synonyms
  4. Functions
  5. Procedures
  6. Packages
  7. Triggers
  8. Indexes
  9. Constraints
  10. Sequences
  11. Java
  12. DB Links
  13. Users
  14. Jobs
  15. Types
  16. Queue Tables
  17. Queues 
  18. Directories
  19. Policy Groups
  20. Policies
  21. Rollback Segments
  22. Tablespaces
  23. Libraries
  24. Materailized Views
  25. Favourites
  26. Materialized View Logs
  27. Dimensions
  28. Sys privs
  29. Clusters
  30. Refresh Groups
  31. Recyclebin
  32. Sched. Chains
  33. Sched.Programs
  34. Sched.Schedules
  35. Sched. Jobs
  36. Sched. Job Classes
  37. Sched. windows
  38. Sched. Window Groups
  39. Invalid Objects
  40. Contexts
  41. Transformations
  42. Audit Policies

Wednesday, June 12, 2013

Definer and Invoker Rights for stored routines in Oracle

Definer rights

A routine stored in the database by default, is executed with the definer rights (owner of the routine), depending on the user who calls it. This is a good way of having the required code perform process logic in one place. It gives better control, preventing direct access to objects that belong to another user, which might result in security issues.

For example, table emp_new  belongs to schema scott. User scott creates a procedure raise_sal  allowing for updates of emp_new table. User HR  is granted execute privileges on the procedure. Now user HR  cannot access the table as no privileges have been granted, but can call the procedure to do the required process logic for updating the table.

Invoker Rights

Invoker rights is a new model for resolving references to database elements in a PL/SQL program unit. From Oracle 8i onwards, we can decide if a program unit should run with the authority of the definer or of the invoker. This means that multiple schemas, accessing only those elements belonging to the invoker, can share the same piece of code.

For example, let's take the above case. The table, emp_new , is created in HR schema  also. Each of the schema will now own the same set of objects but different data, as they are being used for different purposes. Since the called procedure, raise_sal  is owned by SCOTT,  the ideal solution in Oracle 8 and earlier releases, was to compile it in user HR also, so that it will use the objects thereof.

With Oracle 8i, there is no need for this duplication of code. A single compiled program unit can be made to use SCOTT schema's  objects when invoked by SCOTT  and HR schema's  objects when invoked by HR. This way, we have the option of creating a code repository in one place and sharing it with various production users. The owner of the routine must grant EXECUTE privilege to other users.

To enable code to run with Invoker rights, an AUTHID clause needs to be used before the IS or AS keyword in the routine header. The AUTHID clause tells Oracle whether the routine is to be run with the invoker rights (CURRENT_USER), or with the Owner rights (DEFINER). If you do not specify this clause, Oracle by default assumes it to be AUTHID DEFINER.

Restriction in using Invoker rights

1. When compiling a new routine, direct privileges are only considered to resolve any external references. Grants through roles are ignored. The same applies when executing a routine created with invoker rights.

2. AUTHID is specified in the header of a program unit. The same cannot be specified for individual programs or methods within a package or object type.

3. Definer rights will always be used to resolve any external references when compiling a new routine.

4. Maintain extra caution on privileges being assigned to a different user. If the wrong privileges are assigned, a routine with invoker rights may have a mind of its own! Such issues would be difficult to debug. So ensure that the grants are perfectly in place.

5. For an invoker rights routine referred in a view or a database trigger, the owner of these objects is always considered as the invoker, and not the user triggering it.

Invoker rights is a powerful option, to be used with caution. To reduce code maintenance, this option should be thought of in the design stage, based on the need to share code across schemas with a similar setup.

Let us consider the below example.


Screnario 1:

SCOTT  : table :  emp_new

SCOTT  : Procedure : Raise_sal

Category :  Authid Definer

Grant execute on  SCOTT.raise_sal to HR;

HR SCHEMA : emp_new table not present

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

Screnario 2:

SCOTT  : table :  emp_new

SCOTT SCHEMA : Procedure  : Raise_sal

Category :  Authid Definer

Grant execute on  SCOTT.raise_sal to HR;

HR SCHEMA : emp_new table present but with diffent data set

Result :

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Screnario 3:

SCOTT  : table :  emp_new

SCOTT SCHEMA : Procedure : Raise_sal

Category :  Authid current_user

Grant execute on  SCOTT.raise_sal to HR;

HR SCHEMA : emp_new table not present

Result:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Screnario 4:

SCOTT  : table :  emp_new

SCOTT SCHEMA : Procedure : Raise_sal

Category :  Authid current_user

Grant execute on  SCOTT.raise_sal to HR;

HR SCHEMA :  emp_new table present but with diffent data sets.


Result:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

SCOTT  : table :  emp_new

SCOTT  : Procedure : Raise_sal

Category :  Authid Definer

Grant Execute to HR;

HR SCHEMA : emp_new table not present


Logged in as Scott user:

CREATE TABLE SCOTT.EMP_NEW
(
  EMPNO      NUMBER(4),
  ENAME      VARCHAR2(10 BYTE),
  JOB        VARCHAR2(9 BYTE),
  MGR        NUMBER(4),
  HIREDATE   DATE,
  SAL        NUMBER(7,2),
  COMM       NUMBER(7,2),
  DEPTNO     NUMBER(2),
  TEST_DATE  DATE
);

Insert into SCOTT.EMP_NEW
   (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
 Values
   (7369, 'SMITH', 'CLERK', 7902, TO_DATE('12/17/1980 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
    1870, 20);
Insert into SCOTT.EMP_NEW
   (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
 Values
   (7499, 'ALLEN', 'SALESMAN', 7698, TO_DATE('02/20/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
    1760, 300, 30);
Insert into SCOTT.EMP_NEW
   (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
 Values
   (7521, 'WARD', 'SALESMAN', 7698, TO_DATE('02/22/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
    1375, 500, 30);
Insert into SCOTT.EMP_NEW
   (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
 Values
   (7566, 'JONESSS', 'MANAGER', 7839, TO_DATE('04/02/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
    3272.5, 20);
COMMIT;


CREATE OR REPLACE PROCEDURE SCOTT.raise_sal
AUTHID DEFINER
IS
BEGIN
   UPDATE emp_new
      SET sal = sal * 1.10
    WHERE empno IN (SELECT empno FROM emp_new);
END raise_sal;
/

Grant execute on  raise_sal to HR;

SQL> select * from emp_new;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO TEST_DATE
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------
      7369 SMITH      CLERK                7902 17-DEC-80          1870                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81          1760        300        30
      7521 WARD       SALESMAN        7698 22-FEB-81         1375         500         30
      7566 JONESSS    MANAGER         7839 02-APR-81       3272.5                    20

Then Logged in as HR user

SQL> conn hr
Enter password: **
Connected.
SQL> select * from emp_new;
select * from emp_new
              *
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> execute scott.raise_sal;

PL/SQL procedure successfully completed.

SQL> commit;

Commit complete.

SQL> conn scott                      Swap to scott user to see the changes in scott schema : Here HR user doen't have any access directly to the emp_new table in scott schema
                                               but by calling the procedure it can update the data on emp_new table of scott Schema.
                                             
Enter password: *****
Connected.
SQL> select * from emp_new;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO TEST_DATE
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------
      7369 SMITH      CLERK           7902 17-DEC-80           2057                        20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1936         300         30
      7521 WARD       SALESMAN        7698 22-FEB-81     1512.5        500         30
      7566 JONESSS    MANAGER         7839 02-APR-81    3599.75                    20

SQL>


Result of Scenario1:
Can update the table in grantee schema (i.e SCOTT)  irrespective of DML privilege on the grantee table (i.e in emp_new of SCOTT schema) .


Scenario 2:

SCOTT  : table :  emp_new

SCOTT SCHEMA : Procedure  : Raise_sal

Category :  Authid Definer

Grant execute on  SCOTT.raise_sal to HR;

HR SCHEMA : emp_new table present but with diffent data set



Logged in as HR user.

CREATE TABLE HR.EMP_NEW
(
  EMPNO      NUMBER(4),
  ENAME      VARCHAR2(10 BYTE),
  JOB        VARCHAR2(9 BYTE),
  MGR        NUMBER(4),
  HIREDATE   DATE,
  SAL        NUMBER(7,2),
  COMM       NUMBER(7,2),
  DEPTNO     NUMBER(2),
  TEST_DATE  DATE
);


Insert into HR.EMP_NEW
   (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
 Values
   (7654, 'MARTIN', 'SALESMAN', 7698, TO_DATE('09/28/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
    1375, 1400, 30);
Insert into HR.EMP_NEW
   (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
 Values
   (7698, 'BLAKE', 'MANAGER', 7839, TO_DATE('05/01/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
    3135, 30);
Insert into HR.EMP_NEW
   (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
 Values
   (7782, 'CLARK', 'MANAGER', 7839, TO_DATE('06/09/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
    2695, 10);
    COMMIT;



SQL> show user
USER is "HR"
SQL> select * from emp_new;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO TEST_DATE
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1375       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81        3135                       30
      7782 CLARK      MANAGER         7839 09-JUN-81        2695                       10


Connect as SCOTT

SQL> show user
USER is "SCOTT"
SQL> select * from emp_new;

     EMPNO ENAME      JOB              MGR     HIREDATE         SAL       COMM     DEPTNO TEST_DATE
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------
      7369 SMITH       CLERK                7902        17-DEC-80       1870                           20
      7499 ALLEN       SALESMAN        7698        20-FEB-81       1760        300             30
      7521 WARD        SALESMAN        7698        22-FEB-81      1375        500            30
      7566 JONESSS    MANAGER         7839       02-APR-81       3272.5                      20


SQL> conn hr
Enter password: **
Connected.
SQL> exec scott.raise_sal;

PL/SQL procedure successfully completed.

SQL> commit;

Commit complete.

SQL> select * from emp_new;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO TEST_DATE
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1375       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       3135                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2695                    10



SQL> conn scott
Enter password: *****
Connected.
SQL> select * from emp_new;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO TEST_DATE
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------
      7369 SMITH      CLERK           7902 17-DEC-80            2057                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1936        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81     1512.5        500         30
      7566 JONESSS    MANAGER         7839 02-APR-81    3599.75                    20



Result of  Scenario2 : It will  update  the table of scott schema only.


Screnario 3:

SCOTT  : table :  emp_new

SCOTT SCHEMA : Procedure : Raise_sal

Category :  Authid current_user

Grant execute on  SCOTT.raise_sal to HR;

HR SCHEMA : emp_new table not present




drop table hr.emp_new;

drop procedure scott.raise_sal;


CREATE OR REPLACE PROCEDURE scott.raise_sal authid current_user
IS
BEGIN
UPDATE emp_new
SET sal = sal * 1.10
WHERE empno IN (select empno FROM emp_new );
END raise_sal;
/

Grant execute on  raise_sal to HR;


SQL> show user
USER is "HR"
SQL> select * from emp_new;
select * from emp_new
              *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> execute scott.raise_sal;
BEGIN scott.raise_sal; END;

*
ERROR at line 1:
ORA-00942: table or view does not exist
ORA-06512: at "SCOTT.RAISE_SAL", line 4
ORA-06512: at line 1


Result of  Scenario 3  : emp_new table is not present in HR schema now . The procedure can't update the table emp_new in SCOTT shema  irrespective of execute priviledge .

Screnario 4:  emp_new table present in HR schema but with different data.

SCOTT  : table :  emp_new

SCOTT SCHEMA : Procedure : Raise_sal

Category :  Authid current_user

Grant execute on  SCOTT.raise_sal to HR;

HR SCHEMA :  emp_new table present but with diffent data sets.



SQL> conn hr
Enter password: **
Connected.
SQL> CREATE TABLE HR.EMP_NEW
  2  (
  3    EMPNO      NUMBER(4),
  4    ENAME      VARCHAR2(10 BYTE),
  5    JOB        VARCHAR2(9 BYTE),
  6    MGR        NUMBER(4),
  7    HIREDATE   DATE,
  8    SAL        NUMBER(7,2),
  9    COMM       NUMBER(7,2),
 10    DEPTNO     NUMBER(2),
 11    TEST_DATE  DATE
 12  );

Table created.

SQL>
SQL>
SQL> Insert into HR.EMP_NEW
  2     (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
  3   Values
  4     (7654, 'MARTIN', 'SALESMAN', 7698, TO_DATE('09/28/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),

  5      1375, 1400, 30);

1 row created.

SQL> Insert into HR.EMP_NEW
  2     (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
  3   Values
  4     (7698, 'BLAKE', 'MANAGER', 7839, TO_DATE('05/01/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
  5      3135, 30);

1 row created.

SQL> Insert into HR.EMP_NEW
  2     (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
  3   Values
  4     (7782, 'CLARK', 'MANAGER', 7839, TO_DATE('06/09/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
  5      2695, 10);

1 row created.

SQL>     COMMIT;

Commit complete.

SQL> select * from emp_new;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO TEST_DATE
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1375       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       3135                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2695                    10


SQL> conn scott
Enter password: *****
Connected.
SQL> select * from emp_new;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO TEST_DATE
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------
      7369 SMITH      CLERK           7902 17-DEC-80           2057                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1936        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81     1512.5        500         30
      7566 JONESSS    MANAGER         7839 02-APR-81    3599.75                    20

SQL> conn hr
Enter password: **
Connected.
SQL> execute scott.raise_sal;

PL/SQL procedure successfully completed.

SQL> commit;

Commit complete.

SQL> select * from emp_new;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO TEST_DATE
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------
      7654 MARTIN     SALESMAN        7698 28-SEP-81     1512.5       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81     3448.5                    30
      7782 CLARK      MANAGER         7839 09-JUN-81     2964.5                    10

SQL> conn scott
Enter password: *****
Connected.
SQL> select * from emp_new;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO TEST_DATE
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------
      7369 SMITH      CLERK           7902 17-DEC-80            2057                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1936        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81     1512.5        500         30
      7566 JONESSS    MANAGER         7839 02-APR-81    3599.75                    20


Result of Scenario 4: It has updated the emp_new table data of HR schema by referencing the raise_sal procedure of SCOTT schema. The emp_new table data of SCOTT schema remained unaffcted here.

Hope it clears all your doubt.


Cheers
Rajani

Friday, June 7, 2013

Crontab – Quick Reference

Setting up cron jobs in Unix and Solaris
cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs in unix , solaris.  Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.
This document covers following aspects of Unix cron jobs
1. Crontab Restrictions
2. Crontab Commands
3. Crontab file – syntax
4. Crontab Example
5. Crontab Environment
6. Disable Email
7. Generate log file for crontab activity
1. Crontab Restrictions
You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use
crontab if your name does not appear in the file /usr/lib/cron/cron.deny.
If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.
2. Crontab Commands
export EDITOR=vi ;to specify a editor to open crontab file.
crontab -e    Edit your crontab file, or create one if it doesn’t already exist.
crontab -l      Display your crontab file.
crontab -r      Remove your crontab file.
crontab -v      Display the last time you edited your crontab file. (This option is only available on a few systems.)
3. Crontab file
Crontab syntax :
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.






*     *       *           command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)



* in the value field above means all legal values as in braces for that column.
The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).
Notes
A. ) Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.
B.) The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .
4. Crontab Example
A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.
30     18     *     *     *         rm /home/someuser/tmp/*
Changing the parameter values as below will cause this command to run at different time




schedule below :
min
hour
day/month
month
day/week
Execution time
30
0
1
1,6,12
*
– 00:30 Hrs  on 1st of Jan, June & Dec.
0
20
*
10
1-5
–8.00 PM every weekday (Mon-Fri) only in Oct.
0
0
1,10,15
*
*
– midnight on 1st ,10th & 15th of month
5,10
0
10
*
1
– At 12.05,12.10 every Monday & on 10th of every month
:

Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.
5. Crontab Environment
cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh).
cron supplies a default environment for every shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh
Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry.
6. Disable Email
By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .
>/dev/null 2>&1
7. Generate log file
To collect the cron execution execution log in a file :
30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log

Q: What is the difference between Exist and In Operator?

The main difference would be performance. 

If the conditions are correct the results would be the same, but the queries would be executed in a different way by the database engine. When the appropriate index exists, the EXISTS approach usually performs better. You could use the autotrace option in SQL*Plus or SQL Command Line to see the execution plan for your queries.In my point of view, EXISTS is better because it requires to specify a join condition, which can invoke an INDEX scan.

However, IN is often better if the results of the sub query are very small. Usually to run the query that returns the smaller set of results first.

SQL Statements that use the SQL EXIST Condition are very inefficient since the sub-query is RE-RUN for EVERY row in the outer query's table.

The EXISTS operator tests for existence of rows in the result set of the subquery.

. If a subquery row value is found :

- The condition is flagged TRUE

- The search does not continue in the inner query.


. If a subquery row  value is not found :

- The condition is flagged FALSE

- The search continues in the inner query.


-EXISTS operator is frequently used in Correlated sub queries to test whether a value retrieved by the outer query exists in the result set of the values retrieved by the inner query.


If the subquery returns at least one row , the operator returns TRUE. If the value does not exits , it returns FALSE. 

Accordingly, NOT EXISTS tests whether a value retrived by the outer query is not part of the results set of the values retrived by the inner query.


Q: Find employees who have at least one person reporting to them.

SELECT employee_id,last_name, job_id, department_id
FROM employees outer
WHERE EXISTS    
                       (SELECT 'X' 
                        FROM employees
                      WHERE manager_id= outer.employee_id);



The EXISTS operator ensures that the search in the inner query doesn't continue when at least one match is found for the manager and employee_id condition:

WHERE manager_id =outer.employee_id;

Note that the inner SELECT query doesn't need to return a specific value , so a constant can be selected.
From a performance stand point , it is faster to select a constant than a column.

NOTE : Having employee_id in the SELECT clause of the inner query causes a table scan for that column.

Replacing it with a literal 'X' or any constant improves performance.

This is more efficient than using in operator.



A IN construct can be used as an alternative for a EXISTS operator , as shown below.

SELECT employee_id,last_name, job_id, department_id
FROM employees 
WHERE employee_id  IN 
                                    (SELECT manager_id 
                                      FROM employees
                                     WHERE manager_id IS NOT NULL);


Cheers
Rajani

Basic VI Commands:

To Start vi
To use vi on a file, type in vi filename. If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.

vi filename  : edit filename starting at line 1

vi -r filename : recover filename that was being edited when system crashed


To Exit vi:

To save and quit:    ESC : x Enter       : quit vi, writing out modified file to file named in original invocation
                               ESC :wq Enter     : quit vi, writing out modified file to file named in original invocation


To unsave and quit : ESC :q!             : quit vi even though latest changes have not been saved for this vi call



Moving the Cursor:


ESC + j                  : move cursor down one line

ESC + k                  : move cursor up one line

ESC + h                  : move cursor left one character

ESC + l                  : move cursor right one character

ESC + 0                  : move cursor to start of current line (the one with the cursor)

ESC + $                  : move cursor to end of current line

ESC + w                  : move cursor to beginning of next word

ESC + b                  : move cursor back to beginning of preceding word

ESC : 0 or ESC 1G   : move cursor to first line in file

ESC : n or ESC nG   : move cursor to line n

ESC : $ or ESC G   : move cursor to last line in file


Screen Manipulation:

The following commands allow the vi editor screen (or window) to move up or down several lines and to be refreshed.


CTRL+ f : move forward one screen

CTRL+ b : move backward one screen

CTRL+ d : move down (forward) one half screen

CTRL+ u : move up (back) one half screen

Adding, Changing, and Deleting Text:

ESC+ u  : UNDO WHATEVER YOU JUST DID; a simple toggle

Inserting or Adding Text :

The following commands allow you to insert and add text. Each of these commands puts the vi editor into insert mode; thus, the <Esc> key must be pressed to terminate the entry of text and to put the vi editor back into command mode.

i              : insert text before cursor, until <Esc> hit

I in caps  : insert text at beginning of current line, until <Esc> hit

a             : append text after cursor, until <Esc> hit

A             : append text to end of current line, until <Esc> hit

o             : open and put text in a new line below current line, until <Esc> hit

O            : open and put text in a new line above current line, until <Esc> hit

Changing Text:


r     : replace single character under cursor (no <Esc> needed)

R    : replace characters, starting with current cursor position, until <Esc> hit

cw : change the current word with new text, starting with the character under cursor, until <Esc> hit

cNw : change N words beginning with character under cursor, until <Esc> hit;   e.g., c5w changes 5 words

C   : change (replace) the characters in the current line, until <Esc> hit

cc  : change (replace) the entire current line, stopping when <Esc> is hit


Deleting Text :

ESC + x  : delete single character under cursor

Nx         : delete N characters, starting with character under cursor

dw        : delete the single word beginning with character under cursor

dNw     : delete N words beginning with character under cursor;   e.g., d5w deletes 5 words

ESC +dd : delete entire current line

dND        :delete N lines, beginning with the current line;   e.g., 5dd deletes 5 lines

Cutting and Pasting Text:

ESC+yy    : copy (yank, cut) the current line into the buffer

Esc +Nyy  : copy (yank, cut) the next N lines, including the current line, into the buffer

p             : put (paste) the line(s) in the buffer into the text after the current line



Searching Text :


/string : search forward for occurrence of string in text

?string : search backward for occurrence of string in text

 n       : move to next occurrence of search string

N       :move to next occurrence of search string in opposite direction


Determining Line Numbers :

Being able to determine the line number of the current line or the total number of lines in the file being edited is sometimes useful.

:.= returns line number of current line at bottom of screen

:= returns the total number of lines at bottom of screen

Ctrl+g   : provides the current line number, along with the total number of lines,in the file at the bottom of the screen.


Saving and Reading Files:

These commands permit you to input and output files other than the named file with which you are currently working.


:r filename<Return> : read file named filename and insert after current line (the line with cursor)

:w<Return>           :write current contents to file named in original vi call

:w newfile<Return> : write current contents to a new file named newfile

:12,35w smallfile<Return> : write the contents of the lines numbered 12 through 35 to a new file named smallfile

:w! prevfile<Return>  : write current contents over a pre-existing file named prevfile