Mysql
 sql >> Base de données >  >> RDS >> Mysql

MYSQL - Impossible de créer une clé externe

Vous devez avoir une colonne dans les keywords table pour contenir la clé étrangère.

Comme ça

CREATE TABLE `jobs` (
      `title` text NOT NULL,
      `type` text NOT NULL,
      `location` text NOT NULL,
      `salary` int(11) NOT NULL,
      `description` text NOT NULL,
      `date` date NOT NULL,
      `job_id` int(11) NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (`job_id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;

CREATE TABLE `keywords` (
    `keyword_id` int(11) NOT NULL AUTO_INCREMENT,
    `keyword` text NOT NULL,
    `job_id` int(11) NOT NULL,              #<- new column
    PRIMARY KEY(`keyword_id`),
    FOREIGN KEY (job_id) REFERENCES jobs(job_id)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;