Question

I have a table articles and I want to create another table summaries that is a child of articles table.

I've used the following sql to create summaries table but however mysql keeps throwing an error #1072 - Key column 'article_id' doesn't exist in table

CREATE TABLE summaries(
summary_id INT NOT NULL AUTO_INCREMENT,
summary TEXT,
PRIMARY KEY ( summary_id),
FOREIGN KEY (article_id) REFERENCES articles(article_id)
)ENGINE=INNODB;
Was it helpful?

Solution

you dont have article_id in your table summaries, you may mean this:

   FOREIGN KEY (summary_id) REFERENCES articles(article_id)

if you want make summary_id as foreign key to article_id in articles table.

But make sure they have same structure properties.

EDIT:

or create a column article_id in summaries table and make it foreign key

  CREATE TABLE summaries(
  summary_id INT NOT NULL AUTO_INCREMENT,
  summary TEXT,
  article_id INT,
  PRIMARY KEY ( summary_id),
  FOREIGN KEY (article_id) REFERENCES articles(article_id)
  )ENGINE=INNODB;

EDIt:

 INSERT INTO `summaries` (summary,article_id)
 VALUES ($yoursummryVar , (SELECT article_id 
 FROM articles
  WHERE ........)
 )

i didnt test it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top