Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The right way to write "ID" in columns in SQL

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?

like image 838
radbyx Avatar asked May 01 '10 12:05

radbyx


2 Answers

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.

like image 172
outis Avatar answered Oct 20 '22 00:10

outis


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.

like image 26
Martin Smith Avatar answered Oct 20 '22 01:10

Martin Smith