SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Primary Key Name

  1. #1
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Primary Key Name

    Hi,

    I have a question about how to name the primary row id key in my MySQL tables. Is it good practice to use the same naming convention for the primary row id field in every table e.g. "row_id", or should i name them all uniquely e.g. in the users table call it "users_row_id" and in the products table call it "products_row_id"?

    I would assume that using the same name in each table is the best way to go but i'm just worried that when i do a join query that it will be a bit more awkward to distinguish between each tables row_id field.

    Thanks,
    Ronan

  2. #2
    SitePoint Mentor silver trophybronze trophy
    SitePoint Award Recipient Mikl's Avatar
    Join Date
    Dec 2011
    Location
    Edinburgh, Scotland
    Posts
    1,020
    Mentioned
    37 Post(s)
    Tagged
    0 Thread(s)
    There's no definitive answer to this question. Different database designers and developers will all have their own views.

    My personal choice is to go for the same column name in every table. The name I use is ID, but it could just as well be something else.

    Using the same name in every table can save a lot of effort - and risk of errors - when writing queries. It means that you don't need to think about which column is the target of the join. It's always ID - or whatever. For the same reason, I use a consistent naming convention for foreign keys. My convention is <entity name> + underscore + ID (e.g. Customer_ID). I'm not saying that's a good convention; the point is that it's consistent, which means you can (usually) see at a glance if you get it wrong in a query.

    But I do stress this is my personal choice. It's really up to you (and the developers who will be accessing your database) as to whether it's right for you.

    Mike

  3. #3
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,458
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Mikl View Post
    My convention is <entity name> + underscore + ID (e.g. Customer_ID). I'm not saying that's a good convention;
    i am

    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok, thanks. I like the sound of your setup so i think i'll go with that.

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
  •