SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SQL Primary Key?

    Hey all im creating the table using PHP:

    PHP Code:
    $query $db->query("CREATE TABLE users (uName varchar(40), email varchar(40), password varchar(40), primary key (uName, email))"); 
    That works, except say i add a user with uName = Wacko and stuff.

    When i add another user with uName = Wacko it still adds it lol why is this? I thought because i have set uName as a primary key then this would produce an error?

    Any help would be great

    Thanks in advance, Nick!
    Win A FREE iPhone!

    Tired from working? Funny Things! Laugh the stress away
    Some Funny Jokes | Funny Videos

  2. #2
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    Bowen Mountain, NSW
    Posts
    490
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NickPaul View Post
    Hey all im creating the table using PHP:

    PHP Code:
    $query $db->query("CREATE TABLE users (uName varchar(40), email varchar(40), password varchar(40), primary key (uName, email))"); 
    That works, except say i add a user with uName = Wacko and stuff.

    When i add another user with uName = Wacko it still adds it lol why is this? I thought because i have set uName as a primary key then this would produce an error?

    Any help would be great

    Thanks in advance, Nick!
    Within you code you have:
    PHP Code:
    primary key (uNameemail
    I'm not 100% certain, this may need confirmation from a more senior php developer but you have listed 2 fields for primary, then it will only deny you the entry if uName AND email are the same.
    Regards,
    BJ Duncan

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by BJ Duncan View Post
    Within you code you have:
    PHP Code:
    primary key (uNameemail
    I'm not 100% certain, this may need confirmation from a more senior php developer but you have listed 2 fields for primary, then it will only deny you the entry if uName AND email are the same.
    Exactly.
    If you want uname to be unique, and email to be unique too, then you should define one as primary key, and the other as a unique key.

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haha wow you guys are quick Yup worked a treat!!!

    Thanks heaps, Nick
    Win A FREE iPhone!

    Tired from working? Funny Things! Laugh the stress away
    Some Funny Jokes | Funny Videos


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
  •