0

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

1 Answer 1

1

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
2
  • Hmm adding it after like "alter table note add constraint user_fk foreign key (poster) references user(uname);" ended up working. Bit annoying to do separately but glad I can now.
    – windy401
    Commented Oct 31, 2017 at 15:56
  • 1
    @windy401 Good! The advantage of this "long form" is: you are giving the constraint a name. (Sometimes this can be helpful when you want to list the constraints or if you need to remove one of them.)
    – stefan
    Commented Oct 31, 2017 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.