Truncate data and if insert new row column increment from 1 - oracle

I have table with two rows which one ID with auto increment and there are much row last number ID is 89. And then I truncate data/row in the table. And then I insert row again.
But number ID from 90 not from 1 (one). If in mysql if I truncate data in table auto increment start from 1 (one) again. So how in oracle I want to ID autoincrement from one again. Thanx.
Below step when I create table:
// create table;
CREATE TABLE tes (
id NUMBER NULL,
ip_address varchar2(25) NOT NULL
PRIMARY KEY (id)
);
// and create increment;
CREATE SEQUENCE tes_sequence START WITH 1 INCREMENT BY 1;
// and create trigger;
CREATE OR REPLACE TRIGGER tes_trigger
BEFORE INSERT
ON tes
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT tes_sequence.nextval INTO :NEW.ID FROM dual;
END;

Oracle sequence is a separate object and is not connected with table. If you need to start sequence after truncating a table you need to alter the sequence. Have a look here: How do I reset a sequence in Oracle?

Related

How to increment the value of the unique constraint column value in ORACLE

How to increment the value of the unique constraint column value in ORACLE, in the select statement.
For example, in a table 'BILLING_TABLE' - column BLNG_Sk is the unique key (Autoincremented).
So while inserting a new record into the BILLING_TABLE, for the column BLNG_SK we need to give the value (Which is the increment by 1 from the present max value.)
For example, if BLNG_SK max value is 12321.
new record should be 12322.
how to achieve this in Oracle?
Oracle has a SEQUENCE object which provides the functionality you require.
You create one using the CREATE SEQUENCE SQL statement.
The Oracle documentation provides all the required information and the documentation is available via Oracle's Web site.
Assuming you are on Oracle 12.1 or later, define it as an identity column and do not pass any value when inserting:
create table testtable
( test_id number generated always as identity
constraint testtable_pk primary key
, othercol varchar2(10) );
insert into testtable (othercol) values ('Demo');
select * from testtable;
TEST_ID OTHERCOL
---------- ----------
1 Demo
insert into testtable (othercol) values ('Demo #2');
select * from testtable;
TEST_ID OTHERCOL
---------- ----------
1 Demo
2 Demo #2
Try creating a sequence and a trigger. This is the case when you provide the value manually.
CREATE SEQUENCE dept_seq START WITH 12322;
Trigger definition:
CREATE OR REPLACE TRIGGER dept_bir
BEFORE INSERT ON BILLING_TABLE
FOR EACH ROW
BEGIN
SELECT dept_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
/

Difference between Oracle sequence last number and max id inserted

I'm working on a application used by multiple users. They can insert, delete and update rows in the database which fires triggers what write into log tables. The problem is that the difference between the maximum id inserted in the log table and the last number generated by the sequences used by the trigger is changing continously. Sometimes an primary key exception is generated because the the sequence generate values that are already inserted in the table.
CREATE OR REPLACE
TRIGGER EVALUACION.TCALIF_DEL
AFTER DELETE ON EVALUACION.CALIFICACIONES FOR EACH ROW
BEGIN
INSERT INTO LOG_CALIFICACIONES (ID_BITACORA, ID_EVALUACION, ANIO, CALIFICACION, OBSERVACION, USUARIO, FECHA_HORA, TIPO_OPERACION_BD)
SELECT SEQ_CALIF.NEXTVAL, :OLD.ID_EVALUACION, :OLD.ANIO, :OLD.CALIFICACION, :OLD.OBSERVACION, :OLD.USUARIO, :OLD.FECHA_HORA, 'D' FROM DUAL;
END;
CREATE OR REPLACE
TRIGGER EVALUACION.TCALIF_INS
AFTER INSERT ON EVALUACION.CALIFICACIONES FOR EACH ROW
BEGIN
INSERT INTO LOG_CALIFICACIONES (ID_BITACORA, ID_EVALUACION, ANIO, CALIFICACION, OBSERVACION, USUARIO, FECHA_HORA, TIPO_OPERACION_BD)
SELECT SEQ_CALIF.NEXTVAL, :OLD.ID_EVALUACION, :OLD.ANIO, :OLD.CALIFICACION,:OLD.OBSERVACION, :OLD.USUARIO, :OLD.FECHA_HORA, 'I' FROM DUAL;
END;
CREATE OR REPLACE
TRIGGER EVALUACION2010.TCALIF_UPD
AFTER UPDATE ON EVALUACION.CALIFICACIONES FOR EACH ROW
BEGIN
INSERT INTO LOG_CALIFICACIONES (ID_BITACORA, ID_EVALUACION, ANIO, CALIFICACION, OBSERVACION, USUARIO, FECHA_HORA, TIPO_OPERACION_BD)
SELECT SEQ_CALIF.NEXTVAL, :OLD.ID_EVALUACION, :OLD.ANIO, :OLD.CALIFICACION, :OLD.OBSERVACION, :OLD.USUARIO, :OLD.FECHA_HORA, 'U' FROM DUAL; END;
The code for the sequence is:
CREATE SEQUENCE "EVALUACION"."SEQ_CALIF"
MINVALUE 1
MAXVALUE 999999999999999999999999999
INCREMENT BY 1
START WITH 11114992
CACHE 20
NOORDER
NOCYCLE
NOPARTITION;

Trigger required to insert data [duplicate]

This question already has answers here:
Using sequential values for the primary key in an INSERT query
(2 answers)
Closed 9 years ago.
I am trying to load a column with unique sequence number each time a row of data is insrerted in the table.How can this be achieved?
You can create a Sequence, and then use the sequence nextval in your insert statements for the column which you want to have sequential incremented value.
CREATE SEQUENCE seq
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
CACHE 10;
INSERT INTO tab VALUES (seq.nextval, col1, col2, col3);
there is nothing like "auto_increment" or "identity" in Oracle,
but if you want auto increment in your column value you can use Sequence for the this.
after creating sequence you can use After Insert Trigger to insert identical value.
here is trigger example...
CREATE OR REPLACE TRIGGER dep_ins_trig
BEFORE INSERT ON <table_name>
FOR EACH ROW
BEGIN
SELECT dept_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
/
This is achieved by Trigger and Sequence when you want serialized number that anyone can easily read/remember/understand. But if you don't want to manage ID Column (like emp_id) by this way, and value of this column is not much considerable, you can use SYS_GUID() at Table Creation to get Auto Increment like this.
CREATE TABLE <table_name>
(emp_id RAW(16) DEFAULT SYS_GUID() PRIMARY KEY,
name VARCHAR2(30));
Now your emp_id column will accept "globally unique identifier value".
you can insert value in table by ignoring emp_id column like this.
INSERT INTO <table_name> (name) VALUES ('name value');
So, it will insert unique value to your emp_id Column.

Using sequential values for the primary key in an INSERT query

How can I write an insert query for an Oracle database which has a sequential primary key so that the insert statement automatically takes the next number in the sequence?
INSERT INTO LD_USER_ROLE(USER_ROLE_ID,INS_USER,INS_DATE, USERNAME)
VALUES (100, 'sp22',to_date('2003/05/03 21:02:44','yyyy/mm/dd hh24:mi:ss'),'JOHN BARRY', )
In the above statement I have hardcoded the value of 100 for the key 'USER_ROLE_ID' but I'd like to alter this as explained in the first paragraph.
Why don't you just create a trigger for your sequence like this:
Sequence:
CREATE SEQUENCE LD_USER_ROLE_SEQ
INCREMENT BY 1 START WITH 1 NOMAXVALUE NOMINVALUE NOCYCLE NOCACHE NOORDER
Trigger:
CREATE TRIGGER LD_USER_ROLE_INSERT BEFORE INSERT ON LD_USER_ROLE
REFERENCING NEW AS NEW OLD AS OLD FOR EACH ROW
BEGIN
SELECT LD_USER_ROLE_SEQ.NEXTVAL INTO :NEW.USER_ROLE_ID FROM DUAL;
END;
The trigger will automatically get the next value/id on every insert (like auto_increment in mysql).
Apart from using a trigger, you can use a sequence directly in the insert statement:
CREATE SEQUENCE LD_USER_ROLE_SEQ;
INSERT INTO LD_USER_ROLE
(USER_ROLE_ID,INS_USER,INS_DATE, USERNAME)
VALUES
(ld_user_role_seq.nextval, 'sp22',to_date('2003/05/03 21:02:44','yyyy/mm/dd hh24:mi:ss'),'JOHN BARRY', )

Oracle database table insertion

I have two tables:
create table Number( num number(5));
create table Entry(id number(3), name varchar(50));
How can I increment the num field of Number table in Oracle whenever I insert something in the Entry table?
You should use a SEQUENCE instead. The "Number" table is an inherently bad idea, because when two sessions are inserting rows concurrently, each session only sees the uncommited value in the Number table.
This is what you should do instead:
create sequence entrySeq;
create table Entry(id number(3), name varchar(50));
create trigger tr_entry before insert on Entry for each row
begin
select entrySeq.nextval into :new.number from dual;
end;
/
Do you want number.num to continually represent the number of rows iin the Entry table? If so you could just define it as a view:
create view number_view
as
select count(*) from Entry
create sequence entrySeq;
create table Entry(id number(3), name varchar(50));
insert into Entry value (entrySeq.nextval, 'MyName');
(You don't need a trigger).
A sequence returns a unique and increasing number value but Oracle doesn't guarantuee that it is gapless. When sometimes transactions are rollbacked the values of column id will contain gaps.

Resources