#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2016
    Posts
    1
    Rep Power
    0

    I can't insert data into my table!


    I'm trying to insert this code into my Albums table on my MySQL database

    Code:
    INSERT INTO `Albums` (`Albumid`, `Name`, `Numberoftracks`, `Artistid`,     ]
    `Genre`) VALUES (1, "Innuendo", 12, "Queen", "Rock");
    But every time I try to I keep getting this error.

    Code:
    1452 - Cannot add or update a child row: a foreign key constraint fails 
    (`b4014107_db1/Albums`, CONSTRAINT `Albums_ibfk_1` FOREIGN KEY (`Artistid`) 
    REFERENCES `Artist` (`Artistid`))
    I know it's something to do with my foreign key within the table, but I need to manually enter the foreign key because it's not auto-incremented.

    Here's the tables code. .

    Code:
     CREATE TABLE `Albums` (  `Albumid` int(6) NOT NULL,  `Name` varchar(50) NOT 
    NULL,  `Numberoftracks` int(11) NOT NULL,  `Artistid` int(6) NOT NULL,  
    `Genre` varchar(50) NOT NULL,  PRIMARY KEY  (`Albumid`),  KEY `Artistid` 
    (`Artistid`),  CONSTRAINT `Albums_ibfk_1` FOREIGN KEY (`Artistid`)   
    REFERENCES `Artist` (`Artistid`)) ENGINE=InnoDB DEFAULT CHARSET=latin1
    How do I fix this? I need to input data into the table.
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,585
    Rep Power
    4287
    you have artistid as INTEGER, so you can't insert an artist name into it

    why do you say your artist table isn't auto_incremented?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

IMN logo majestic logo threadwatch logo seochat tools logo