AUTI INCREMENT in an oracle table

1

Good I want to create a table in an oracle scheme called CA:

CREATE TABLE  PDT_CONCEPTOS(
ID INT(8) NOT NULL,
NOMBRE VARCHAR(250) NOT NULL,
CODIGO_PDT VARCHAR(8) NOT NULL,
CODIGO_DB VARCHAR(100) NOT NULL,  
DESCRIPCION VARCHAR(250)NOT NULL,  
PERTENECE VARCHAR(150)NOT NULL,  
DB_TABLA VARCHAR(100)NOT NULL,  
PRIMARY KEY(ID)  
 );

What I want is for my ID to be self-increasing and to be believed in the schema CA I hope your help thanks

    
asked by Dalstron 28.12.2018 в 22:45
source

1 answer

1

You can create a sequence in this way, in which you indicate the name that in this case will be id that starts at 1 and each turn increases by 1:

CREATE SEQUENCE id
START WITH 1
INCREMENT BY 1;

Later we have a table similar to the following, where as you can see the column of PRIMARY KEY is called the same as the sequence that we created above

CREATE TABLE persons(
    id INT NOT NULL,
    first_name VARCHAR2(50) NOT NULL,
    last_name VARCHAR2(50) NOT NULL,
    PRIMARY KEY(id)
);

Now we proceed to perform an insertion in the following way, where to invoke the sequence, it is done as follows: id.NEXTVAL which is the name of the sequence point and the method NEXTVAL

INSERT INTO persons(id, first_name, last_name) VALUES (id.NEXTVAL, 'alfreds', 'peaces');
    
answered by 29.12.2018 в 00:07