That's because you used wrong syntax for alter table
.
Here's correct one:
SQL> create table test_so
2 (name varchar2(20));
Table created.
SQL> alter table test_so add id int generated always as identity primary key;
Table altered.
SQL>
[EDIT]
As you commented that such a statement won't work, it appears that your Oracle database version doesn't support identity columns. In that case, you'll have to do that with a database trigger. Here's how:
SQL> create table test_so
2 (name varchar2(20));
Table created.
SQL> alter table test_so add id int;
Table altered.
SQL> create sequence seq_so;
Sequence created.
SQL> create or replace trigger trg_bi_test
2 before insert on test_so
3 for each row
4 when (new.id is null)
5 begin
6 :new.id := seq_so.nextval;
7 end;
8 /
Trigger created.
Testing:
SQL> insert into test_so (name) values ('Littlefoot');
1 row created.
SQL> select * from test_so;
NAME ID
-------------------- ----------
Littlefoot 1
SQL>