MySQL语法错误 - 无法创建表

时间:2013-11-04 23:12:09

标签: php mysql syntax syntax-error

这是我的代码

CREATE TABLE IF NOT EXISTS items
( 
id INT NOT NULL AUTO_INCREMENT, 
name varchar(256) , 
description TEXT,
price INT , 
images TEXT,
views INT ,
hidden TEXT,
purchases INT,
PRIMARY KEY (id) 
);

CREATE TABLE IF NOT EXISTS methods 
( 
method_id INT NOT NULL AUTO_INCREMENT, 
method varchar(256),
username varchar(256),
password varchar(256),
PRIMARY KEY (method_id)
);

CREATE TABLE IF NOT EXISTS payments
( 
payment_id INT NOT NULL AUTO_INCREMENT, 
item_id INT NOT NULL, 
method varchar(256),
display INT,
PRIMARY KEY (payment_id) ,
FOREIGN KEY (item_id) REFERENCES items (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (method) REFERENCES methods (method) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

前两个表生成正常,但第三个“付款”给我错误代码150!哪个应与FK有关?!

任何帮助?

1 个答案:

答案 0 :(得分:1)

方法表中的主键是method_id而不是method。数据类型为INT而非VARCHAR(256)

你需要:

CREATE TABLE IF NOT EXISTS payments
( 
  payment_id INT NOT NULL AUTO_INCREMENT, 
  item_id INT NOT NULL, 
  method_id int,  -- this is different
  display INT,
  PRIMARY KEY (payment_id) ,
  FOREIGN KEY (item_id) REFERENCES items (id) ON DELETE CASCADE ON UPDATE CASCADE,

  -- and this line is different
  FOREIGN KEY (method_id) REFERENCES methods (method_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;