MYSQL - Impossible to create an external key

前端 未结 1 1815
离开以前
离开以前 2021-01-29 10:31

I am a self taught CS and I am really novice at mySQL. I created a table called \"jobs\". I would like to create a new table keywords with 3 columns:

1条回答
  •  我在风中等你
    2021-01-29 11:05

    You need to have a column in the keywords table to hold the foreign key.

    Like this

    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;
    

    0 讨论(0)
提交回复
热议问题