MySQL Syntax Error After Adding FOREIGN KEY REFERENCE

database-designMySQL

I'm getting the shown error in the code below, having copied the foreign key line stuff from the tutorial shown in the second image. Even after looking at this and other tutorials throughout the day I'm still not seeing where the problem is. Could someone please point out what's wrong?

enter image description here

enter image description here

Best Answer

Try:

...
poster int references user(userid)
...

E.g.

mysql> create table user ( uname varchar(15) primary key ) ;
Query OK, 0 rows affected (0.01 sec)


mysql> create table note (
    ->   title int primary key
    -> , poster int references user(userid)
    -> );
Query OK, 0 rows affected (0.00 sec)

Or ...

alter table note
add constraint user_fk
foreign key (poster) references user(uname);

E.g.

mysql> create table user ( uname varchar(15) primary key ) ;
Query OK, 0 rows affected (0.00 sec)

mysql> create table note (
    ->   title int primary key
    -> , poster varchar(15)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> alter table note
    -> add constraint user_fk
    -> foreign key (poster) references user(uname); 
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0