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

    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Alter Table Constraint Foreign Key...


    I'm adding a foreign key to an existing, populated column in a table and was curious about what the practical difference is between these two syntaxes:

    ALTER TABLE review ADD FOREIGN KEY (profile_id) REFERENCES profile (id)

    and

    ALTER TABLE review ADD CONSTRAINT (profile_id_fkey) FOREIGN KEY (profile_id) REFERENCES profile (id)

    I've seen them both used in online references. I've always used the first form when creating foreign keys in new tables but I can't find an explanation of when to use that form and when to use the CONSTRAINT form. Are they the same?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,926
    Rep Power
    1045
    Hi,

    read the manual
    CONSTRAINT constraint_name

    An optional name for a column or table constraint. If the constraint is violated, the constraint name is present in error messages, so constraint names like col must be positive can be used to communicate helpful constraint information to client applications. (Double-quotes are needed to specify constraint names that contain spaces.) If a constraint name is not specified, the system generates a name.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0
    I did read that. So that's all that CONSTRAINT does in this context? Produce a more detailed error message?
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,926
    Rep Power
    1045
    Originally Posted by steveinbklyn
    I did read that.
    Well, then why did you say you couldn't find an explanation and asked if they're the same? Obviously they are not.



    Originally Posted by steveinbklyn
    So that's all that CONSTRAINT does in this context? Produce a more detailed error message?
    Yes. And when you choose your own name, of course it's also more comfortable to edit the constraint later.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0
    Originally Posted by Jacques1
    Well, then why did you say you couldn't find an explanation and asked if they're the same? Obviously they are not.
    Have a nice, or better, day.

IMN logo majestic logo threadwatch logo seochat tools logo