Reputation: 1233
So I have looked it up for the past 2 days almost and I can't seem to figure out what is it that mysql doesn't let me add a foreign key. When I run the code for the second table that includes the foreign key I get the following error:
#1072 - Key column 'album' doesn't exist in table
I'm pretty sure there isn't any syntax error in my code as I have revised it few times now.
I have seen the same question before in stakOVF but the issues with those questions were very obvious syntax errors, however not of the solutions in those questions were relevant to my problem, none solved my issue.
So here is the code I am running and for which the error above is returned. Thanks in advance.
CREATE TABLE ‘Album’(
‘id’ INT AUTO_INCREMENT PRIMARY KEY ,
‘name’ VARCHAR( 35 ) NOT NULL
) ENGINE = InnoDB;
The above code runs with no problem, but when I run the code below the error comes up
CREATE TABLE ‘Picture’(
‘id_pk’ INT AUTO_INCREMENT PRIMARY KEY ,
‘album’ INT,
‘pictureURL’ VARCHAR( 270 ) NOT NULL ,
‘name’ VARCHAR( 35 ) NOT NULL ,
CONSTRAINT album_fk FOREIGN KEY ( album ) REFERENCES Album( id )
) ENGINE = InnoDB;
I have fiddled with the CONSTRAINT line and I had it also in the following form FOREIGN KEY (album) REFERENCES Album(id), that is without the constraint prepended.
THANK YOU ALL, after all there was a syntax error an like some said it is to do with the funny quotes, removing them, works like a charm. Many Thanks!
Upvotes: 0
Views: 233
Reputation: 94642
Can I suggest you get hold of MysQL Workbench. Its free and it's nice easy dislogs guide you through this sort of process.
This works for me, see what it does for you. I just changed the field name album
to albumId
to make the column usage more obvious.
CREATE TABLE `Picture` (
`id` INT NOT NULL AUTO_INCREMENT ,
`albumId` INT NULL ,
`pictureURL` VARCHAR(270) NOT NULL ,
`name` VARCHAR(35) NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `album_fk_idx` (`albumId` ASC) ,
CONSTRAINT `album_fk` FOREIGN KEY (`albumId` ) REFERENCES `album` (`id` ) ON DELETE NO ACTION ON UPDATE NO ACTION)
ENGINE = InnoDB;
Upvotes: 1
Reputation: 3237
First you don't need any quote mark because any reserved word is used.
I tried to execute these sql queries without the quotes and it worked like a charm.
CREATE TABLE Album(
id INT AUTO_INCREMENT PRIMARY KEY ,
name VARCHAR( 35 ) NOT NULL
) ENGINE = InnoDB;
CREATE TABLE Picture(
id_pk INT AUTO_INCREMENT PRIMARY KEY ,
album INT,
pictureURL VARCHAR( 270 ) NOT NULL ,
name VARCHAR( 35 ) NOT NULL ,
CONSTRAINT album_fk FOREIGN KEY ( album ) REFERENCES Album( id )
) ENGINE = InnoDB;
The CREATE TABLE ‘Album’ creates a table with ‘Album’ name instead of Album, with ‘id’ and ‘name’ fields instead of id and name and so on
Upvotes: 3
Reputation: 11712
You seem to use interesting quotes, that become part of the name. Remove them and you should be fine
Upvotes: 1
Reputation: 475
I think you should index the column 'album' before declaring it as a foreign key Please check if this works
CREATE TABLE ‘Picture’(
‘id_pk’ INT AUTO_INCREMENT PRIMARY KEY ,
‘album’ INT,
‘pictureURL’ VARCHAR( 270 ) NOT NULL ,
‘name’ VARCHAR( 35 ) NOT NULL ,
INDEX (album),
CONSTRAINT album_fk FOREIGN KEY ( album ) REFERENCES Album( id )
) ENGINE = InnoDB;
Upvotes: 2
Reputation: 404
As far as I know, foreign keys should be made indexes to actually make them foreign keys.
I have found this example in mysql dev documentation (http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html)
CREATE TABLE parent (
id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (
id INT,
parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id)
REFERENCES parent(id)
ON DELETE CASCADE
) ENGINE=INNODB;
I am not sure about it, but you might try to modify your code accordingly.
Upvotes: 1