Search code examples
mysqlsqlmysql-error-1064

ERROR 1064: is there a mysql create table to insert a foreign key? I get a syntax error


when I am creating a table and inserting a foreign key, I get this error "ERROR 1064 (4200)" of syntax, I don't know if I am writing it wrong or if I need something else to add it to the table.

Code:

CREATE TABLE orders (id_orders INT NOT NULL, date DATE NOT NULL, id_client INT FOREIGN KEY REFERENCES client(id_client));

I expected the data output to be correct and not give me an error. since I am connecting a primary key with a foreign key.


Solution

  • The create statement should be like this

    CREATE TABLE orders (
      id_orders INT NOT NULL
    , date DATE NOT NULL
    , id_client INT
    , CONSTRAINT fk_client FOREIGN KEY (id_client)
      REFERENCES client(id_client) );