0

I've been trying to add AUTO_INCREMENT on a created table , but the ALTER table query is not working

My Table: My Table Description

Query and Error: enter image description here

And also

ALTER TABLE professor ADD sno INT IDENTITY;

is not working

Abhishek Kumar
  • 165
  • 1
  • 17

1 Answers1

1

Can you please try:

alter table professor add sno integer generated by default on null as identity;

EDIT: Sorry, the OP is asking for 11g, as the code above works after 12.

Then you need to use sequence. Please see below:

ALTER TABLE professor ADD sno INT;
CREATE SEQUENCE dept_seq START WITH 1;

And you need to set a trigger for your sequence like:

CREATE OR REPLACE TRIGGER dept_bir 
BEFORE INSERT ON departments 
FOR EACH ROW

BEGIN
  SELECT dept_seq.NEXTVAL
  INTO   :new.id
  FROM   dual;
END;

Reference: How to create id with AUTO_INCREMENT on Oracle?

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82