简体   繁体   中英

foreign key not creating in table

first table:

create table login(
  id int auto_increment, 
  user varchar(20), 
  pass varchar(20), 
  primary key(id)
);

first table is created with primary key..

second table:

create table check(
  cid int, 
  rid int, 
  usname varchar(20), 
  word varchar(20), 
  FOREIGN KEY(rid) REFERENCES login(id)
);

Error:

ERROR 1064 (42000): 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 'check
(cid int, rid int, usname varchar(20), word varchar(20), FOREIGN KEY(rid) R' at
line 1

The problem isn't the foreign key. check is a reserved word in MySQL. So you need to choose another name or escape it:

create table `check`(
    cid int,
    rid int,
    usname varchar(20),
    word varchar(20),
    FOREIGN KEY(rid) REFERENCES login(id)
);

Ironically, although it is a reserved word, it is not actually used. Alas, I do wish that MySQL supported check constraints.

By the way, here is a SQL Fiddle.

check is a key word in MySQL ; either surround it with backticks, `, or choose a different name.

Change the name of your table check . Check is a reserved word in MySQL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM