How to make an autoincrementable field in oracle 11g?

3

The table is as follows.

create table ventas(
    id_venta number(5) primary key,
    id_articulo number(5) references articulos,
    id_empleado number(6) references empleado, 
    id_cliente  number(5) references cliente, 
    cantidad number(10) not null,
    precio number(8) not null,
    total number(8) not null,
    fecha_venta date
); 
    
asked by David Alfonso 30.12.2017 в 00:36
source

1 answer

3

To solve this, you just need a trigger and using a sequence like the one presented here.

create sequence Auidventa
start with 1
increment by 1  -- aqui lo hago es que determino apartir de que nuevo voy
                -- a iniciar y cuando va incrementar
order;

-- Creacion de trigger
create or replace trigger autoidventa
before insert on ventas
for each row
begin
  select Auidventa.nextval into :new.id_venta from dual;
end;
/

Now only that it gives to realize the common insertion in the table advantages but eye does not specify the field id_venta, that is to say it does not insert there since that the autoincrementable field.

    
answered by 30.12.2017 / 00:36
source