Friday, April 14, 2017

How to Define an Auto Increment Primary Key in Oracle



 Create the table

 create table honnikery (
      id number primary key,
      name varchar2(100)
    );

Create sequence
 create sequence honni_id_seq;

create a trigger that uses the sequence to populate the primary key
create trigger trg_honni_id
      before insert on honnikery
      for each row
    begin
      select honni_id_seq.nextval
        into :new.id
        from dual;
    end;



insert into honnikery( name ) values ('honnikery Prabhakar');
insert into honnikery( name ) values ('Biswa');

Wednesday, April 5, 2017

How does one get the time difference between two date columns SQL

How does one get the time difference between two date columns
 in hours , minutes and seconds



SELECT floor((D1-D2)*24)
      || ' HOURS ' ||
          mod(floor((D1-D2)*24*60),60)
        || ' MINUTES ' ||
        mod(floor((D1-D2)*24*60*60),60)
         || ' SECS ' time_difference
    FROM honnikery;



TIME_DIFF
--------------------------------------------------------------------------------
1 HOURS 0 MINUTES 0 SECS
1 HOURS 10 MINUTES 0 SECS
10 HOURS 1 MINUTES 10 SECS