0

这是我的代码

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有关?!

有什么帮助吗?

4

1 回答 1

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;
于 2013-11-04T23:14:24.677 回答