Search code examples
sqloracle-databasealter

Alter Scripts in SQL - ORA 01735


I've tried searching this topic, and my searches led me to this format, which is still throwing an error. When I execute my script, I basically get a load of ORA-01735 errors for all my later statements. I had it done out differently, but googling led me to this format, which still doesn't work. Any tips?

CREATE TABLE table7
(
column1 int NOT NULL,
column2 int NOT NULL,
column3 int NOT NULL
)
/

ALTER TABLE table7

ADD(    pk1 PRIMARY KEY(column1),
    fk1 FOREIGN KEY(column2) REFERENCES Table1(column2),
    fk2 FOREIGN KEY(column3) REFERENCES Service(column3)
)
/

Solution

  • ADD should surround each column definition. You don't wrap a single ADD around 3 new columns.

    See: http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_3001.htm#i2183462

    For Primary Key and Foreign Key constraints you need the CONSTRAINT keyword. See: http://docs.oracle.com/javadb/10.3.3.0/ref/rrefsqlj81859.html Section on "adding constraints".

    EDIT: This was the only thing that worked on the fiddle I tried:

    ALTER TABLE table7
    ADD (
          CONSTRAINT pk1 PRIMARY KEY (column1),
          CONSTRAINT fk1 Foreign Key (column2) REFERENCES Table1 (column2),
          CONSTRAINT fk2 Foreign Key (column3) REFERENCES Service (column3)
        )
    

    Here's the fiddle: http://sqlfiddle.com/#!4/9d2a3