ORA-00904: Invalid identifier, I am creating a table but this error appears, could anyone help me?

Asked

Viewed 191 times

-1

CREATE TABLE CARRO(
   ID_CARRO VARCHAR(20),
   MODELO VARCHAR2(10),
   COR VARCHAR2(10),
   PLACA VARCHAR2(10),
   CONSTRAINT PK_CARRO PRIMARY KEY (ID_MARCA, MODELO),
   CONSTRAINT FK_CARRO_REF_MARCA FOREIGN KEY (ID_MARCA)
   REFERENCES MARCA(ID_MARCA),
);
  • 1

    mysql? this is an oracle error, edit the correct tag

1 answer

1

Your query has two errors, I will comment here:

CREATE TABLE CARRO(
   ID_CARRO VARCHAR(20),
   MODELO VARCHAR2(10),
   COR VARCHAR2(10),
   PLACA VARCHAR2(10),
   ID_MARCA INT, <-- Falta esse campo, usado abaixo para criar a FK. Usei INT no exemplo, veja o tipo correto
   CONSTRAINT PK_CARRO PRIMARY KEY (ID_MARCA, MODELO),
   CONSTRAINT FK_CARRO_REF_MARCA FOREIGN KEY (ID_MARCA)
   REFERENCES MARCA(ID_MARCA)  <-- aqui há uma vírgula inválida, remova pois não há mais atributos a serem definidos.
);
  • FOREIGN KEY (ID_MARCA) here is creating a FK from a field that has not been defined, need to add earlier in the field list;
  • REFERENCES MARCA(ID_MARCA), here is an unnecessary comma, because there is nothing more to define in the table, as the following is the )
  • I did as your example and still gave error, ORA-00904: "ID_MARCA": invalid identifier

  • removed the comments right? see here working http://sqlfiddle.com/#! 4/aad08

  • I already have a table created as a tag, how do I include this?

  • @Ricardopunctual I believe that the PK of the Car table should be PRIMARY KEY (ID_CARRO, MODEL)

  • @Ricardopereira, in the example I created the table "mark" just to validate the Foreign key, in your case you don’t. You need to put the same type as the column "ID_MARCA" of your table "tag" in the column "ID_MARCA" of this new table, I put "INT" as example, confirm pf

  • @Danizavtz I agree with you, but since I don’t know his modeling, I’m just showing you how to solve the token error problem, or "invalid identifier"

  • I switched and gave this error ORA-00905: keyword not found

  • @Ricardopunctual http://sqlfiddle.com/#! 4/aad08/3 where I was wrong??

  • compare with my example, to use sqlfiddle, the creation commands need to be on the left-hand side, but still have the most comma FOREIGN KEY (ID_MARCA),

  • I got it, thanks for your help. Hugs

  • good, if the question helped, do not forget to accept :)

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.