I create table in oracle and I want add auto increment for my primary key
CREATE TABLE "TEST_1"."PERSON"
("ID" NUMBER NOT NULL ENABLE,
"FNAME" VARCHAR2(20 BYTE),
"LNAME" VARCHAR2(20 BYTE),
CONSTRAINT "PERSON_PK" PRIMARY KEY ("ID"));
Using Oracle sql develope when I want alter ID to get Auto Increment for primary key I get error ORA-02262: ORA-932
I have two raw in table
ALTER TABLE PERSON
MODIFY (ID DEFAULT SYS_GUID() );
Best Answer
(Note: my answer and examples are using Oracle 11g)
Reason for the issue/error
The Oracle error
ORA-02262
is thrown when there are inconsistent data types. In your case, when creating the table you specify that the ID column is of typeNUMBER
:The
SYS_GUID()
function in Oracle "generates and returns a globally unique identifier (RAW
value) made up of 16 bytes). The documentation then states that the 16-byteRAW
value can be represented by a 32-character hexadecimal representation, orVARCHAR2(32)
.For this reason, when
SYS_GUID()
is used as the default value of a column in Oracle, the result is often stored as a 32-byteVARCHAR
orVARCHAR2
:Solution(s)
If altering the data type of the column is a suitable solution, the code below will successfully alter the table to store
SYS_GUID()
values as identifiers:If you must have a numeric value as the ID in your table, there is an excellent answer here: