Search code examples
sqloracle-databaseoracle11galter-table

How to fix "ORA-01735: Invalid ALTER TABLE option" error in Oracle11g


I'm coding a SQL script but, when I compile and try execute with SQL*PLUS, this show mentioned error at beginning of the issue. I'm seeing other related issues but I can't find the solution.

This is for a local server in my Windows 10' computer, running Oracle 11g with SQL*PLUS and XAMPP Server. I've tried on another computer with similar specificatios except specific details.

ALTER TABLE ACTORES(
ADD CONSTRAINT pk_actores PRIMARY KEY (Codigo)
);
COMMIT;
ALTER TABLE PERSONAJES(
  ADD CONSTRAINT pk_personajes PRIMARY KEY (Codigo),
  ADD CONSTRAINT fk1_personajes FOREIGN KEY (CodigoActor) REFERENCES ACTORES (Codigo),
  ADD CONSTRAINT fk2_personajes FOREIGN KEY (CodigoSuperior) REFERENCES PERSONAJES (Codigo)
);
COMMIT;
ALTER TABLE NAVES(
  ADD CONSTRAINT pk_naves PRIMARY KEY (Codigo)
);
COMMIT;

Solution

  • Too many parenthesis aren't healthy.

    SQL> create table actores (codigo number);
    
    Table created.
    
    SQL> alter table actores (add constraint pk_actores primary key (codigo));
    alter table actores (add constraint pk_actores primary key (codigo))
                        *
    ERROR at line 1:
    ORA-01735: invalid ALTER TABLE option
    
    
    SQL> alter table actores add constraint pk_actores primary key (codigo);
    
    Table altered.
    
    SQL>
    

    Another table:

    SQL> create table personajes (codigo number, codigoactor number, codigosuperior number);
    
    Table created.
    
    SQL> alter table personajes add constraint pk_personajes primary key (codigo);
    
    Table altered.
    
    SQL> alter table personajes add constraint fk1_personajes foreign key (codigoactor)
      2    references actores (codigo);
    
    Table altered.
    
    SQL> alter table personajes add constraint fk2_personajes foreign key (codigosuperior)
      2    references personajes (codigo);
    
    Table altered.
    
    SQL>
    

    Or, alternatively, several constraints in the same ALTER TABLE:

    SQL> drop table personajes;
    
    Table dropped.
    
    SQL> create table personajes (codigo number, codigoactor number, codigosuperior number);
    
    Table created.
    
    SQL> alter table personajes add
      2    (constraint pk_personajes primary key (codigo),
      3     constraint fk1_personajes foreign key (codigoactor)
      4       references actores (codigo),
      5     constraint fk2_personajes foreign key (codigosuperior)
      6       references personajes (codigo)
      7    );
    
    Table altered.
    
    SQL>
    

    Do the same for the rest of your tables/constraints.

    P.S. Oh, yes - you don't have to COMMIT; ALTER TABLE is DDL and implicitly commits any changes.