SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: UNIQUE Column

  1. #1
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    UNIQUE Column

    I am looking for some help in the clarification of the UNIQUE key. For example, say I created a table and appended UNIQUE (email, activation, code) to the end of the CREATE statement. When inserting data, does the unique fields create a combined unique key or are they separate? In other words does an insert look for a row with all three unique fields to be the same or will it be unsuccessful if just one of the fields is a duplicate?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    the combination of column values has to be unique
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So to accomplish individual UNIQUE, it means that I have to do SELECT queries prior to make sure the values are not there?

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    you never have to do SELECTs to ensure uniqueness

    what is it you want to do?

    what was the purpose of declaring a 3-column composite UNIQUE key?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was trying to understand how they work in concept. If I had a field for email and then a field for a salt key, I would want to make sure the email is UNIQUE and that the salt can only be in the database once as well. I guess if I created the salt with the email among other things, I shouldn't have to worry about that I suppose and can just have email as UNIQUE.

    If the email is a UNIQUE field, does that mean with my coding language I shouldn't perform a select query, just try to insert the email and then check if the insert query succeeded to know if the email exists already or not?

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by orange_floyd View Post
    If the email is a UNIQUE field, does that mean with my coding language I shouldn't perform a select query, just try to insert the email and then check if the insert query succeeded to know if the email exists already or not?
    yes
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •