SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Unique & PRIMARY- what's the difference?

  1. #1
    SitePoint Zealot Jafar's Avatar
    Join Date
    Jun 2005
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unique & PRIMARY- what's the difference?

    Hi,
    I want to know what's the difference between Unique and primary key in mysql?
    for example what's difference between using Unique for Username Field (in user table ) or primary?
    Thanks
    regards

  2. #2
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,513
    Mentioned
    35 Post(s)
    Tagged
    1 Thread(s)
    in mysql? nothing
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    691
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Columns in a unique constraint may be null, which is not allowed for primary key columns.

  4. #4
    reads the ********* Crier silver trophybronze trophy SitePoint Award Recipient longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also, the auto_increment feature requires a primary index.

  5. #5
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    More than one field in a table may be unique for a record. One can be the primary key, for the other you'd use the unique constraint.
    membership_no, name, address, telno, locker_no
    membership_no is unique and the primary key
    locker_no is also unique in the table, as no-one shares lockers. (assumes old members who used the locker last year are deleted)

  6. #6
    SitePoint Zealot Jafar's Avatar
    Join Date
    Jun 2005
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thnx

    Thanks All,
    very useful tips.

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
  •