views:

132

answers:

7

My first question in here. I like the site so much :)

I've a SQL table called Product. In that table I want to make a column for the ID of the product, and I really want to know the right way to write ID because I make a lot of ID columns in the long run.

So is it: "ID" or "Id" ?

And also a foreign key in another table, is that called ProductID or ProductId?

Thx guys.

+1  A: 

Personally, I'd ProductID, ProductName etc in a Product table and for the FKs too to avoid having ID and Name columns everywhere

Just be consistent

gbn
+5  A: 

There isn't a "right" way of doing it. Just be consistent. My personal preference is to use ProductId in both tables. If you use "ID" for all your tables and then join onto another table you will likely end up aliasing them anyway to distinguish between the two ID fields.

Martin Smith
Up-voted for not supporting the use of "id" as the primary key name. That's always seemed to me like the waste of an opportunity to make one's code clearer.
Larry Lustig
@Larry -- in the context of the application, the usage is almost always more readable if you simply use `id`. As in, `product.id` vs. `product.product_id` (Rails-style). I see no value in repeating product in both the name of the entity and in the property name.
tvanfosson
@tvanfosson The value is surely whenever you have a query that will return a result set with more than 1 ID field (e.g. PostId, ThreadId, UserId)
Martin Smith
@tvanfosson: it may be more redundant, but I don't think any significant readability is lost. And it is more explicit. I will say that I'm probably influenced by the fact that I do a lot of work in a product that 1) allows you to leave relation names off column names in queries and 2) contains a forms package that does natural joins and nothing else when more than one table is used.
Larry Lustig
@Larry - I guess I shouldn't have taken that English class either. The redundancy really bothers me.
tvanfosson
i also prefer the product.product_id option
Randy
+6  A: 

Capitalization in SQL is largely a matter of coding style. Consistency is the most important aspect. That said, "ProductId" looks a little like "Productld" (that's a lower-case-L before the "d") to me, so I'd prefer "ProductID" (or "productID" or "product_id").

As for prefixing column names with table names, it's too much in my book. Products.id is sufficient; Products.productID is redundant.

outis
A: 

Well, "Id" is short for "Identification" so it "should" be "Id".

On the other hand, it's pronounced "I-D" rather than "id", so "ID" is acceptable too.

I'd prefer "ProductId" rather than just "Id" for a table's primary key, so that both sides of the foreign key relationship match.

James Curran
The id, along with the ego and super-ego, is also one of the three parts of the psyche in Freud's model.
tvanfosson
+2  A: 

As you prefer.

Most (many? some? the ones that I know?) frameworks seem to have adopted lower-case "id" for the primary key and "primarykeytablename_id" for the foreign key, at least for the default naming convention. I don't like this convention myself, since I like the key to have the same name on both sides of the relationship.

My preference is EntityNameID in both places (for example ProductID, OrderID, OrderDtlID, and so on).

Larry Lustig
+1  A: 

I probably should never have taken that psychology class, but because there is a word "id", I always uppercase the abbreviation for identifier, thus I use "ID". I've experimented both with just using ID and ProductID for the primary key name. Because I use LINQ and simply map the class in the designer, I've taken to naming the columns the way I want them in my code. Because I'd prefer to have product.ID than product.ProductID, I use the shorter name. For foreign keys, I use the table/column format (with no separator), so the foreign key would become ProductID. This isn't an issue for me in my code, though, because I almost always use the mapped entity, e.g., cart.Product rather than the key itself, cart.ProductID.

EDIT: Note I'm assuming a target of the .NET framework, thus using (mostly), the naming conventions from .NET. If I were doing Rails development, these would probably be lowercase and I would use underscores as separators.

tvanfosson
For INNER JOINS you could obviously just use the longer name from the referencing table but presumably that complicates OUTER JOINS if you have 2 tables both with columns named ID? Or does that never come up with LINQ?
Martin Smith
@Martin -- I'll fully admit that I'm less concerned with the actual SQL and more concerned with the translation to code (my classes). YMMV. In LINQ, I'm usually mapping onto an appropriately named variable to disambiguate the id columns. If I were selecting only the ids out, they would probably go into an anonymous type and I'd create "properties" in that type with unambiguous names. That's a relatively rare occurrence and I'm willing to deal with it for the simplicity in the general case.
tvanfosson
A: 

I tend to go for not having 'id' or 'ID'.

I always go for table - productName

pkProductName ProductName fkProductCode

and table - ProductCode pkProductCode productCode

so even when you are are using SQL or in code the relationships are explict and meaningful.

jpg