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?
1 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
-
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.– windy401Commented 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.)– stefanCommented Oct 31, 2017 at 20:53