Search code examples
mysqlsyntaxforeign-keysmysql-error-1064

MySQL table not creating error #1064


Here is my sql:

CREATE TABLE companyprinciple {
userid INT, 
FOREIGN KEY (userid) REFERENCES user(id) ON DELETE CASCADE
}ENGINE=INNODB;

and here is the error that I am getting:

#1064 - You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near '{
userid INT, 
FOREIGN KEY (userid) REFERENCES user(id) ON DELETE CASCADE
}ENG' at line 1

unfortunately most of my experience in db programming is in postgreSQL, and the foreign keys work a lot better there it seems.

EDIT: alright so the first problem was that my eyes were closed and the { } should be ( ) but now i am getting a new error

#1005 - Can't create table 'ourhoursdb.companyprinciple' (errno: 150)

here is the full ddl.sql (... is other columns, this table is creating fine though)

CREATE SCHEMA OurHoursDB;
USE OurHoursDB;

CREATE TABLE User (
id SERIAL NOT NULL PRIMARY KEY,
...

) ENGINE=INNODB;

CREATE TABLE companyprinciple (
userid INT, 
FOREIGN KEY (userid) REFERENCES user(id) ON DELETE CASCADE
)ENGINE=INNODB;

Solution

  • Use round parenthesis pair (). NOT the curly ones {}.

    Also make sure you have created user table with an id column with exact same type first.