Ho can i define a integer auto increment with oracle 11g?This is my code with mysql user_id int(6) not null auto_increment primary key
how can i have this line with oracle?Because i've already the same database in mysql now i want to build the same structure with oracle
Asked
Active
Viewed 2,147 times
0
-
Possible duplicate of [Auto Increment for Oracle](http://stackoverflow.com/questions/9733085/auto-increment-for-oracle) – Feb 02 '17 at 01:03
-
for oracle 12c+ follow this https://stackoverflow.com/a/11296469/10531665 – Susobhan Das Oct 23 '20 at 08:15
1 Answers
1
You can achieve this with a sequence.
CREATE SEQUENCE seq_user;
The above will auto increment by 1, and start at 1.
To insert values using this sequence, you can do the following (for example):
INSERT INTO table_name (user_id) VALUES (seq_user.NEXTVAL);
To automate this process, you could reference the sequence in a trigger on the table, that adds this value on an insert automatically:
CREATE OR REPLACE TRIGGER user_trg
BEFORE INSERT ON table_name
FOR EACH ROW
DECLARE
BEGIN
IF(inserting)
THEN
:NEW.USER_ID := seq_user.NEXTVAL;
END IF;
END;

mmartin2409
- 54
- 3