SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    just curious: how do you name your pk?

    Hi

    I am curious about pk and names. Some people use ID, other pk etc... Some people use also the table name in their pk name like user_pk, or userpk, or userID, user_id etc...

    It's an open discussion, not really a question, so let's start the debate

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    debate???

    name it whatever you like, as long as you don't stick "col" on the front of it

    you know, like how some people stick "tbl" on the front of their table names

    i like id for PKs and foo_id for related FKs (where foo is the entity)

    this way, your sql join conditions look like
    Code:
    customers.id = orders.customer_id
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937
    debate???
    hum... forgive my broken english. I used the wrong word. Let's start the discussion then

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by r937
    name it whatever you like, as long as you don't stick "col" on the front of it

    you know, like how some people stick "tbl" on the front of their table names
    Ooh that infuriates me! I know when I'm accessing a column or a table, I don't need Hungarian notation slowing me down .

    As for primary key conventions, I just use [something]id, where [something] relates to the entity information being stored in the table, like customerid or postid.

  5. #5
    SitePoint Addict
    Join Date
    May 2003
    Location
    Auckland
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    As for primary key conventions, I just use [something]id, where [something] relates to the entity information being stored in the table, like customerid or postid.
    Same here. So my joins would look like:
    Code:
    Customer.CustomerID = Order.CustomerID
    I've never really tried it they way you do Rudy, but i think i like the look of it

  6. #6
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the good thing with rudy's solution is that it can save you time with some text editor (not a big amount of time but... still some time )

    customer_id

    you can cut and paste it easily, just click on the part before the underscore and only that part will be highlighted. That way, you don't always have to rewrite the '_id' part. Can be useful sometimes.

  7. #7
    SitePoint Wizard davidjmedlock's Avatar
    Join Date
    Dec 2002
    Location
    Nashville, TN USA
    Posts
    1,688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937
    debate???

    name it whatever you like, as long as you don't stick "col" on the front of it

    you know, like how some people stick "tbl" on the front of their table names

    i like id for PKs and foo_id for related FKs (where foo is the entity)

    this way, your sql join conditions look like
    Code:
    customers.id = orders.customer_id
    Ditto. Makes it easy to identify the keys on a table.

  8. #8
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937
    name it whatever you like
    But in a uniform way throughout your applications.

    I tend to go for 'tablename_id'. And the same for the fk. I'm finding it practical to illustrate the join in this way.

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    TX
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    r937 hit is on the head w/his example...and I too would rather not see "tbl" @ the beginning of table names. My professor actually takes OFF on our assignments if we don't have that prefix on every one of our tables

  10. #10
    SitePoint Addict
    Join Date
    May 2003
    Location
    Auckland
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by null
    r937 hit is on the head w/his example...and I too would rather not see "tbl" @ the beginning of table names. My professor actually takes OFF on our assignments if we don't have that prefix on every one of our tables
    So he marks you down if you don't prefix every table with tbl_ ?

    Tell him he's an idiot.

  11. #11
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    i'm curious, does your professor also prefix his column names with "col"?

    just in case, you know, he ever accidentally goes to write a query like

    SELECT * FROM colDateAdded
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  12. #12
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    TX
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So he marks you down if you don't prefix every table with tbl_ ?

    Tell him he's an idiot.
    No, he doesn't care about the column names, but he's picky about the table names....but ya, if there is a "Customers" table, he'll take off if we have anything OTHER than "tbl" as the prefix. I totally understand the need for using a common naming convention, that makes perfect sense. I personally use "t" as the prefix...I did this on my last assignment and he deducted 1 point per table. An assignment worth 20 points, 9 tables...I'm sure y'all are qualified enough to do the math Was the only one that got all the core design and the queries correct, but had one of the lowest grades

  13. #13
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah man.... that sucks.

    tell your teacher to have a little 'discussion' with rudy.....



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
  •