SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Portugal
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question #1217 -Cannot delete or update a parent row: a foreign key constraint fails

    Hello!
    My name's Eduardo and i'm reading the book "Build Your Own Database Driven
    Website using PHP & MySQL".
    Well, actually, i'm reading the sample, but anyway, i'm doing a mysq database and I made some tables and filled in some information just fo testing, but when I try to delete the data from the tables,or when I try tdrop the tables, nothing happens, and I get the following message:"#1217 -
    Cannot delete or update a parent row: a foreign key constraint fails". Could
    somebody tell me what does this means?
    Thank you

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    It means that you are trying to cancel a row from one table, that contains a value present in at least one row in another table, and the foreign key constraint is there to avoid this from happening.

    For example, let's say you have a user table and a usertype table:

    USER
    UserId UserName UserTypeId
    1 Joe 1

    USERTYPE
    UserTypeId Description
    1 Administator
    2 User

    If UserTypeId is a foreign key for the USER table, pointing to the UserTypeId of the USERTYPE table, then right now you can't delete the row with UserTypeId 1 in the USERTYPE table. This prevents you from deleting usertypes that are still used for some users.

    You should first delete all the users with UserType 1, before you can delete the UserType itself.

    If this isn't clear, then you can read the manual:
    http://dev.mysql.com/doc/refman/5.1/...nstraints.html

  3. #3
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Portugal
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much guido 2004!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •