将新列添加到表作为外键

时间:2015-10-08 00:43:48

标签: mysql sql

我很难尝试在我的sql表中添加一个列,该列将成为另一个表的外键。我收到以下错误:

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 ''description_id' INT,
ADD FOREIGN KEY (description_id)
REFERENCES description(de' at line 2

从我的询问:

ALTER TABLE images
ADD COLUMN 'description_id' INT,
ADD FOREIGN KEY (description_id)
REFERENCES description(description_id)

这是我的表,我试图将列添加到:

CREATE TABLE `images` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `pattern` varchar(225) DEFAULT NULL,
  `color` varchar(225) DEFAULT NULL,
  `imageUrl` varchar(225) DEFAULT NULL,
  `imageSource` varchar(225) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=latin1;

以下是正在连接的表:

CREATE TABLE `description` (
  `description_id` int(11) NOT NULL,
  `color` varchar(255) DEFAULT NULL,
  `pattern` varchar(255) DEFAULT NULL,
  `body` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`description_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

1 个答案:

答案 0 :(得分:0)

你不能改变表添加,添加。尝试:

ALTER TABLE images
ADD COLUMN 'description_id' INT
REFERENCES description(description_id)