Error 1064 in Mysql Workbench

Asked

Viewed 35 times

0

I wrote the following code for the college database class:

CREATE DATABASE estoque;
USE estoque;

CREATE TABLE produtos( /*o erro ocorre aqui nesse parêntese*/
    INT codP,
    CHAR (20) nomeP,
    FLOAT precoP,
    PRIMARY KEY (codP)
    FOREIGN KEY (id)
);
CREATE TABLE fornecedores( /*e nesse parêntese*/
    INT id,
    CHAR (40) nomeF,
    PRIMARY KEY (id),
    FOREIGN KEY (codP)
);

However, there are syntax errors where I commented. And even searching manuals on websites or in the documentation itself, they’re written the same way I wrote them, so I don’t know how to fix them and there’s no way to give origin to that code.

  • Review your FOREIGN KEY clauses. Which field is referencing which field in which table?

1 answer

0

1 - Invert the variable and its format like this 2 - The external reference needs to define what it connects to in the table and cannot be created to a table that has not yet been created. you will need to define it after creating the second table.

CREATE DATABASE estoque;
USE estoque;

CREATE TABLE produtos(
  codP INT,
  nomeP CHAR (20),
  precoP FLOAT,
  PRIMARY KEY (codP)
);
CREATE TABLE fornecedores( /*e nesse parêntese*/
  id INT,
  nomeF CHAR (40) ,
  PRIMARY KEY (id),
  FOREIGN KEY (id) reference produtos(codP)
);
ALTER TABLE produtos
ADD FOREIGN KEY (codP) REFERENCES fornecedores(id);
  • I do not know what is intended to be implemented with this definition of tables but if, by chance, it is an N:N relationship, which would be normal, this implantation will not work. It works in the case of a 1:1 relationship, a supplier provides a single product and a product is supplied by a single supplier, in which case there is no reason to divide it into two tables.

  • I only did what he asked, but according.

Browser other questions tagged

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