我有一个表歌曲,其中包含艺术家姓名和流派作为外键,但它们不是艺术家和流派表中的主键。我希望将数据输入歌曲表并自动填充到其他两个表中,除非我的逻辑错误。所以这是歌表:
CREATE TABLE IF NOT EXISTS `project`.`song` (
`idsong` INT(11) NOT NULL AUTO_INCREMENT ,
`artistname` VARCHAR(45) NULL DEFAULT NULL ,
`title` VARCHAR(45) NULL DEFAULT NULL ,
`genre` VARCHAR(15) NULL DEFAULT NULL ,
`year` INT(4) NULL DEFAULT NULL ,
`lyrics` TEXT NULL DEFAULT NULL ,
`url` VARCHAR(45) NULL DEFAULT NULL ,
PRIMARY KEY (`idsong`) ,
INDEX `title` (`title` ASC) ,
INDEX `genre_idx` (`genre` ASC) ,
INDEX `artistname_idx` (`artistname` ASC) ,
CONSTRAINT `genre`
FOREIGN KEY (`genre` )
REFERENCES `project`.`genres` (`genre` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `artistname`
FOREIGN KEY (`artistname` )
REFERENCES `project`.`artist` (`artistname` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
和另外两个表:
CREATE TABLE IF NOT EXISTS `project`.`genres` (
`genre` VARCHAR(15) NOT NULL ,
PRIMARY KEY (`genre`) )
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `project`.`artist` (
`idartist` INT(11) NOT NULL AUTO_INCREMENT ,
`artistname` VARCHAR(45) NULL DEFAULT NULL ,
`bio` TEXT NULL DEFAULT NULL ,
PRIMARY KEY (`idartist`) ,
INDEX `artistname` (`artistname` ASC) )
ENGINE = InnoDB
这是我的查询不起作用:
INSERT INTO song (artist, title, genre, year, lyrics, url)
VALUES
('Jason Aldean', 'Night Train', 'Country', '2012', 'This is the song lyrics', '9ADpkg1dkDU');
有什么想法有什么问题吗?我是新手,所以我确定是用户:)