www.webdeveloper.com
Results 1 to 5 of 5

Thread: Table without a primary key.

  1. #1
    Join Date
    Aug 2009
    Location
    Ottawa, On
    Posts
    12

    Table without a primary key.

    Hey,

    I'm currently scripting this database from MsSQL to MySQL and have come across this table that has no primary key. Now this is just out of curiosity, I don't have a lot of SQL experience but from what I have been thought: Every table needs a primary key.

    I guess my question is: Would there be any time or situation where one would need to create a table without a primary key?

  2. #2
    Join Date
    Mar 2008
    Posts
    124
    Quote Originally Posted by GreenMushroom16 View Post
    Hey,

    I'm currently scripting this database from MsSQL to MySQL and have come across this table that has no primary key. Now this is just out of curiosity, I don't have a lot of SQL experience but from what I have been thought: Every table needs a primary key.

    I guess my question is: Would there be any time or situation where one would need to create a table without a primary key?
    You can create tables in MySQL without a primary key. You normally only do this when a table is acting as a 'go-between' of two tables and a unique identifier is not required or useful. For example, if a person adds a property to their favourites, then there could be a table which has the following fields - personIDfk, propertyIDfk, date_added - which has the effect of linking the person with the property and no primary key is required or useful.

    HTH

  3. #3
    Join Date
    Aug 2009
    Location
    Ottawa, On
    Posts
    12
    That makes sense, although the table I have come across holds these fields:

    • PCName
    • AppPath
    • Active

    I guess it's used to locate available applications, my guess is this table would need a key if they ever need to add another table giving properties to this one. Maybe they'll never need to but I just think it would be good practice to add a key anyways.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,427
    Note that a primary key can be across mutliple columns, so if in this example some combination of 2 or 3 columns should never be duplicated, you might make that combination the primary key (PCName and AppPath, perhaps?).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    As stated, you don't need a PK and/or your PK can span columns. However, best practice is to use an auto_inc PK (even if you're using UUIDs) on every table. This is particularly important in web development, particularly because most of your tables will/ought be InnoDB.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles