Google+
Showing posts with label 9i. Show all posts
Showing posts with label 9i. Show all posts

Thursday, July 21, 2011

OTM - Introduction


OTM is a oracle transportation management system which track your shipping status , reduces the cost of transportation , by which you can get

status how much your shipment is paid or how much your shipment is delayed at a time.

OTM is a well defined GUI based transportation management system. It contain help section tell about all issues regrading how to navigate OTM.

It contain following basic components :-


  1. Currency Section
  2. Rate Section
  3. International Rate
  4. TimeZone Section
  5. Schedule Section
  6. Order Management Section
  7. Airport Section
  8. Port Section
  9. Distance Time Section



OTM is very strong optimization engine . When you go to plan a shipment there are lot of stuff work in background :-

  1. Finding Rates
  2. Finding Airport available to pick up load .
  3. Finding Resources available to move shipment source to destination within deadline
  4. How much transportation cost .
  5. How much time is required to complete shipment.





Note:-Please comment and reply me.

Wednesday, June 22, 2011

MW Podcast 003 - Explore the OTM UI: Map Feature



Note:-Please comment and reply me.

Tuesday, June 7, 2011

Replacing/Altering Views


Replacing/Altering Views

To alter the definition of a view, you must replace the view using one of the following methods:
  • A view can be dropped and then re-created. When a view is dropped, all grants of corresponding view privileges are revoked from roles and users. After the view is re-created, necessary privileges must be regranted.
  • A view can be replaced by redefining it with a CREATE VIEW statement that contains the OR REPLACE option. This option replaces the current definition of a view, but preserves the present security authorizations.
For example, assume that you create the ACCOUNTS_STAFF view, as given in a previous example. You also grant several object privileges to roles and other users. However, now you realize that you must redefine the ACCOUNTS_STAFF view to correct the department number specified in the WHERE clause of the defining query, because it should have been 30. To preserve the grants of object privileges that you have made, you can replace the current version of the ACCOUNTS_STAFF view with the following statement:

CREATE OR REPLACE VIEW Accounts_staff AS
    SELECT Empno, Ename, Deptno
    FROM Emp
    WHERE Deptno = 30
    WITH CHECK OPTION CONSTRAINT ica_Accounts_cnst;
Replacing a view has the following effects:
  • Replacing a view replaces the view's definition in the data dictionary. All underlying objects referenced by the view are not affected.
  • If previously defined but not included in the new view definition, then the constraint associated with the WITH CHECK OPTION for a view's definition is dropped.
  • All views and PL/SQL program units dependent on a replaced view become invalid.
With some restrictions, rows can be inserted into, updated in, or deleted from a base table using a view. The following statement inserts a new row into the EMP table using the ACCOUNTS_STAFFview:
INSERT INTO Accounts_staff
    VALUES (199, 'ABID', 30);

Restrictions on DML operations for views use the following criteria in the order listed:
  1. If a view is defined by a query that contains SET or DISTINCT operators, a GROUP BY clause, or a group function, then rows cannot be inserted into, updated in, or deleted from the base tables using the view.
  1. If a view is defined with WITH CHECK OPTION, then a row cannot be inserted into, or updated in, the base table (using the view), if the view cannot select the row from the base table.
  1. If a NOT NULL column that does not have a DEFAULT clause is omitted from the view, then a row cannot be inserted into the base table using the view.
  1. If the view was created by using an expression, such as DECODE(deptno, 10, "SALES", ...), then rows cannot be inserted into or updated in the base table using the view.
The constraint created by WITH CHECK OPTION of the ACCOUNTS_STAFF view only allows rows that have a department number of 10 to be inserted into, or updated in, the EMP table. Alternatively, assume that the ACCOUNTS_STAFF view is defined by the following statement (that is, excluding the DEPTNO column):
CREATE VIEW Accounts_staff AS
    SELECT Empno, Ename
    FROM Emp
    WHERE Deptno = 10
    WITH CHECK OPTION CONSTRAINT ica_Accounts_cnst;

Considering this view definition, you can update the EMPNO or ENAME fields of existing records, but you cannot insert rows into the EMP table through the ACCOUNTS_STAFF view because the view does not let you alter the DEPTNO field. If you had defined a DEFAULT value of 10 on the DEPTNO field, then you could perform inserts.
If you don’t want any DML operations to be performed on views, create them WITH READ ONLY option. Then no DML operations are allowed on views.

Monday, June 6, 2011

Views


Views are known as logical tables. They represent the data of one of more tables. A view derives its data from the tables on which it is based. These tables are called base tables. Views can be based on actual tables or another view also.

Whatever DML operations you performed on a view they actually affect the base table of the view. You can treat views same as any other table. You can Query, Insert, Update and delete from views, just as any other table.

Views are very powerful and handy since they can be treated just like any other table but do not occupy the space of a table.

The following sections explain how to create, replace, and drop views using SQL commands.

Creating Views


Suppose we have EMP and DEPT table. To see the empno, ename, sal, deptno, department name and location we have to give a join query like this.

select e.empno,e.ename,e.sal,e.deptno,d.dname,d.loc
        From emp e, dept d where e.deptno=d.deptno;

So everytime we want to see emp details and department names where they are working we have to give a long join query. Instead of giving this join query again and again, we can create a view on these table by using a CREATE VIEW command given below

create view emp_det as select e.empno,
e.ename,e.sal,e.deptno,d.dname,d.loc
        from emp e, dept d where e.deptno=d.deptno;

Now to see the employee details and department names we don’t have to give a join query, we can just type the following simple query.

select * from emp_det;

This will show same result as you have type the long join query.  Now you can treat this EMP_DET view same as  any other table.
For example, suppose all the employee working in Department No. 10 belongs to accounts department and most of the time you deal with these people. So every time you  have to give a DML or Select statement you have to give a WHERE condition like .....WHERE DEPTNO=10. To avoid this, you can create a view as given below

CREATE VIEW accounts_staff AS
    SELECT Empno, Ename, Deptno
    FROM Emp
    WHERE Deptno = 10
    WITH CHECK OPTION CONSTRAINT ica_Accounts_cnst;
 
Now to see the account people you don’t have to give a query with where condition you can just type the following query.
 
Select * from accounts_staff;
Select sum(sal) from accounst_staff;
Select max(sal) from accounts_staff; 
 
As you can see how views make  things easier.
The query that defines the ACCOUNTS_STAFF view references only rows in department 10. Furthermore, WITH CHECK OPTION creates the view with the constraint that INSERT and UPDATEstatements issued against the view are not allowed to create or result in rows that the view cannot select.

Considering the example above, the following INSERT statement successfully inserts a row into the EMP table through the ACCOUNTS_STAFF view:

INSERT INTO Accounts_staff VALUES (110, 'ASHI', 10);
However, the following INSERT statement is rolled back and returns an error because it attempts to insert a row for department number 30, which could not be selected using theACCOUNTS_STAFF view:

INSERT INTO Accounts_staff VALUES (111, 'SAMI', 30);

Wednesday, December 29, 2010

Nth Highest Salary Query in Oracle 9i/10g/11g/11i

Problem: Show the list of all employees from the employee table having second highest Salary or Nth highest salary in the organization.

Solution: Well we can achieve the result by so many ways but in this post I would like to do it by using co-related queries.
Query to get the list of employees getting 2nd highest salary (Set the value of @SalaryPosition to get Nth highest salary):




SELECT * FROM Employee E1  WHERE @SalaryPosition = ( SELECT COUNT(DISTINCT E2.Salary) FROM Employee E2 WHERE E2.Salary >= E1.Salary )



Co-Related Query:  In short, it’s a type of nested sub query where the inner query is referenced to some value of the outer query. And the inner query is executed for each record in the outer query making it slowL.
     In our query, to get the 2nd highest salary, works in the same way too. Let’s dive a little deeper.

Sample data used in employee table:


slNo           empname               salary


101            Ram                      5000
102            Abhishek              7500
101            kumar                   5000
104            prasad                  6570
102            Jumla                    7500
101           Harkesh                12000
101           John                      4000

So we have taken two aliases for the employee table i.e. E1 and E2.(Here N=2)









Corollary: Let’s take the first record in the table E1 where the salary is 5000 and assume it as the second highest salary. But how will you know if it is actually the second highest salary or not. If 5000 is the second highest salary, then the distinct count of all the salaries from table E2 which are greater than or equal to 5000 must be 2.





The first record from E1 (salary = 5000) will be compared to all the records in E2 (salary).


Table E1                                         Table E2                                                  Distinct count where
(considering the first record only)   (distinct salary)                                           salary is >= 5000

5000                                               4000                                                                   4
                                                       5000
                                                       6570
                                                       7500
                                                      12000

Hence 5000 is not the highest salary as we have 4 more salaries greater than equal to 5000. But it is concluded that 5000 is at the fourth position.

     Now let’s take the second salary from table E1 and do the same comparison as in the earlier case.



The second salary is 7500 and as per rule the count of distinct salaries greater than equal to 7500 must be 2.

   Table E1                                         Table E2                                                Distinct count where
(considering the second record only)  (distinct salary)                                       salary is >= 5000


7500                                                  4000                                                                 2
                                                          5000
                                                         6570
                                                         7500
                                                         12000


Here in this case our condition in the where clause of outer query is satisfying.

Note:-Please comment and reply me.

Friday, October 8, 2010

A NOCYCLE sequence

 A NOCYCLE sequence

SQL> -- A NOCYCLE sequence
SQL>
SQL>
SQL> CREATE SEQUENCE StudentNumSeq
  2 INCREMENT BY 50000
  3 START WITH 50000
  4 MAXVALUE 99999
  5 NOCACHE
  6 NOCYCLE;

Sequence created.

A CYCLE sequence

 A CYCLE sequence

SQL> -- A CYCLE sequence
SQL>
SQL>
SQL> CREATE SEQUENCE StudentNumSeq
  2 INCREMENT BY 50000
  3 START WITH 50000
  4 MAXVALUE 99999
  5 NOCACHE
  6 CYCLE;

Sequence created.

SQL>
SQL>
SQL> select studentNumSeq.nextVal from dual;

  NEXTVAL
----------
  50000

SQL> select studentNumSeq.nextVal from dual;

  NEXTVAL
----------
  1

SQL> select studentNumSeq.nextVal from dual;

  NEXTVAL
----------
  50001

SQL>
SQL>
SQL> drop sequence studentNumSeq;



Note:-Please comment and reply me.

Wednesday, September 8, 2010

Merge Statement Example in Orcale



merge [ hints ] into table-name-1 | view-name-1 [ alias-1 ]
using table-name-2 | view-name-2 | subquery [ alias-2 ]
on ( condition )
[ merge-update-clause ] [ merge-insert-clause ] [ error-logging-clause ];

 

 create table table_dest (
  id number primary key,
  txt varchar2(20)
);

insert into table_dest values (1,'one');
insert into table_dest values (3,'three');
insert into table_dest values (5,'five');

commit;

create table table_source (
  id number primary key,
  txt varchar2(20)
);

Monday, September 6, 2010

Modifying a Sequence

Modifying a Sequence

• You must be the owner or have the ALTER privilege for the sequence.

• Only future sequence numbers are affected.

• The sequence must be dropped and re-created to restart the sequence at a different number.

• Some validation is performed.

 
 
Note:-Please comment and reply me.

Saturday, August 14, 2010

NOCYCLE sequence

SQL> -- A NOCYCLE sequence
SQL>
SQL>
SQL> CREATE SEQUENCE StudentNumSeq
  2 INCREMENT BY 50000
  3 START WITH 50000
  4 MAXVALUE 99999
  5 NOCACHE
  6 NOCYCLE;

Sequence created.

SQL>
SQL>
SQL> select studentNumSeq.nextVal from dual;

  NEXTVAL
----------
  50000

SQL> select studentNumSeq.nextVal from dual;
select studentNumSeq.nextVal from dual
  *
ERROR at line 1:
ORA-08004: sequence STUDENTNUMSEQ.NEXTVAL exceeds MAXVALUE and cannot be instantiated


SQL> select studentNumSeq.nextVal from dual;
select studentNumSeq.nextVal from dual
*
ERROR at line 1:
ORA-08004: sequence STUDENTNUMSEQ.NEXTVAL exceeds MAXVALUE and cannot be instantiated


SQL>
SQL>
SQL> drop sequence studentNumSeq;

Sequence dropped.


Note:-Please comment and reply me.

Creating a trigger based on two tables

SQL>
SQL>
SQL> CREATE TABLE myTable1 (a INTEGER, b CHAR(10));

Table created.

SQL>
SQL> CREATE TABLE myTable2 (c CHAR(10), d INTEGER);

Table created.

SQL>
SQL>
SQL> CREATE TRIGGER trig1
  2 AFTER INSERT ON myTable1
  3 REFERENCING NEW AS newRow
  4 FOR EACH ROW
  5 WHEN (newRow.a <= 10)
  6 BEGIN
  7 INSERT INTO myTable2 VALUES(:newRow.b, :newRow.a);
  8 END trig1;
  9

Trigger created.

SQL>
SQL> insert into myTable1 values(1,'a');

1 row created.

SQL> insert into myTable1 values(2,'b');

1 row created.

SQL>
SQL> select * from myTable1;

  A B
---------- ----------
  1 a
  2 b

SQL> select * from myTable2;

C D
---------- ----------
a 1
b 2

SQL>
SQL> drop table myTable1;

Table dropped.

SQL> drop table myTable2;

Table dropped.

Note:-Please comment and reply me.

Friday, August 6, 2010

Sequence Example 1


SQL> CREATE SEQUENCE supplier_seq
  2 MINVALUE 1
  3 MAXVALUE 999999999999999999999999999
  4 START WITH 1
  5 INCREMENT BY 1
  6 CACHE 20;

Sequence created.

SQL>
SQL> select supplier_seq.nextval from dual;

  NEXTVAL
----------
  1

SQL> select supplier_seq.nextval from dual;

  NEXTVAL
----------
  2

SQL> select supplier_seq.nextval from dual;

  NEXTVAL
----------
  3

SQL> select supplier_seq.nextval from dual;

  NEXTVAL
----------
  4

SQL>
SQL> drop sequence supplier_seq;

Sequence dropped.



Note:- Please comment and reply me.

If id is null, use the value from sequence

If id is null, use the value from sequence
 
SQL> create table myTable(
  2 id number primary key,
  3 blob_content blob
  4 )
  5 /

Table created.

SQL>
SQL> create sequence myTable_seq
  2 /

Sequence created.

SQL>
SQL> create or replace trigger biu_myTable
  2 before insert or update on myTable
  3 for each row
  4 begin
  5 if :new.id is null then
  6 select myTable_seq.nextval into :new.id from dual;
  7 end if;
  8 end;
  9 /

Trigger created.

SQL> drop table myTable;

Table dropped.

SQL> drop sequence myTable_seq;

Sequence dropped.




Note:-Please comment and reply me.