views:

151

answers:

10

Hi, why in the database tables most of the times primery key name is: tablename_id and not id? example: user_id and not just id. Thanks

+3  A: 

What makes you think they are always named something_id? Sometimes I use something_KEY like if I am building dimension tables. I don't think I have ever made a key/id with tablename_id. In short, I think it depends on the situation, the architect, and your standards (i.e., all caps, underscores, etc).

ajdams
+3  A: 

This is totally a local convention, and will vary dramatically depending on your company's practice (or the project you're working with).

As for me, I pretty much always choose "ID" because I'm very comfortable with the table aliasing idiom in SQL queries. For me select p.id from products p is more comfortable than select product_id from products. But I am not everybody else. I value consistency and I prefer to have as close as possible a mapping between my object model and my database; from my perspective, using "id" is more predictable across the domain. Some people prefer to have unique-as-possible names so that they don't accidentally refer to the wrong "id" by accident.

JasonTrue
+2  A: 

I've seen a few cases of database schemas where all columns have been prefixed with either the table name or an abbreviation of the table name. The explanation I got when questioning this was that it was practical not having to worry about column name collisions when joining tables, because each column then got a name that was unique in the schema.

I guess that this applies to primary keys as well, especially as they are frequently used when doing joins.

In my opinion, this doesn't make sense, because it is very easy to just prefix the column name with the table name using dot notation in the sql queries, and thereby removing the ambiguity... if at all you have to write the SQL yourself, that is.

norheim.se
Not to mention tedious. One table convention I endure has the entire table name embedded in the column prefix; an earlier schema by the same guy used shorter, esoteric column prefixes applied inconsistently.
JasonTrue
+5  A: 

I use a similar convention: TableNameID because:

  • foreign keys can have the same column name, which helps when you have lots of tables
  • selecting columns form multiple tables with the same name forces you to use an alias, so naming the column more uniquely helps.
  • My tables are not mapped to objects in the application, so don't need to consider that.
KM
+2  A: 

This syntax can make it easier when doing joins, avoiding the 'as' keyword. ex:

SELECT `tbl_user`.`user_id`, `tbl_emails`.`email_id` FROM `tbl_emails` LEFT JOIN `tbl_users` ON 'tbl_emails`.`user_id` = `tbl_users`.`user_id`

rather than:

SELECT SELECT `tbl_user`.`id` AS `user_id`, `tbl_emails`.`id` AS `email_id` FROM `tbl_emails` LEFT JOIN `tbl_users` ON 'tbl_emails`.`user_id` = `tbl_users`.`id`

However, this is only a preference that some coders feel. Coding guidelines and naming standards are different everywhere. I personally use only a simple 'id' for the primary key, then use the 'as' keywords. There's not necessarily a right or wrong, as long as what you do makes sense and you do it consistently.

TallGreenTree
+1  A: 

When you have some JOIN then ON A.customer_id = B.customer_id looks better than ON A.customer_id = B.id. If you don't use JOINs, I see no real reason for tablename_id. About JOIN on Wikipedia

Mafi
+2  A: 

Naming the primary key the same as its foreign keys is necessary if you want to use the NATURAL JOIN syntax. I think it's easier to understand as well, but there are people who feel strongly about it either way. In any case, it's really only a matter of style and local convention -- whatever you decide to do, at least be consistent!

Daniel Pryden
Natural join is an abomination. It's a bug in the SQL specification.
Stephanie Page
@Stephanie: But the SQL specification can be viewed as a buggy implementation of the relational algebra. I'd argue that NATURAL JOIN is a "natural" expression of a relation. But that's really beside the point: as I say in my answer, it really comes down to a stylistic issue.
Daniel Pryden
Natural join is a very fine thing. It's just that the SQL implementation is awkward due to the absurd convolutions of the SELECT statement syntax.
dportas
So if you have a NATURAL JOIN between tables A and B... B always had a description column and you add a description column to table A and there's no error generated, just the SQL stops returning the data it used to because you added a description column. This is fine? A column addition should break nothing.
Stephanie Page
@Stephanie: As Daniel suggested, that isn't a problem in the algebra or other languages because you can easily project on just the required columns BEFORE you do the join. It's the SQL syntax that sucks. The SELECT statement is this weird compound operator that assumes a fixed excution order where join always comes before projection - so the only way to do a "safe" natural join is to nest the SELECT statements as a derived table subquery with yet more SELECT statements.
dportas
Natural join in Tutorial D syntax: A{x,a,b} JOIN B{x,c,d}; Is that so bad?
dportas
Why have to do a "safe" natural join? The times I've seen it used, wasn't in the safe method you describe. It's inherently implicit, and given the choice, explicit should be the watchword in code.
Stephanie Page
@Stephanie: I think you're missing the point. In a "pure" relational algebra sense, tuple elements should always be unique, except for when they're related. The idea of having "columns" with "names" is an artifact of the SQL-centric database design, not a feature of the relational model (which is a set-theoretic construct). That said, you're completely right, there are pragmatic reasons to avoid the NATURAL JOIN syntax in SQL. I never said otherwise!
Daniel Pryden
@Daniel, I have a problem with the "pure" anything-other-than-the-tools-i-can-solve-business-problems-with. Most of the questions here are from people trying to solve a problem with code, not propose intellectual challenges in the realm relational algebra. I think we were just cross-realms. I do appreciate your comments.
Stephanie Page
+5  A: 

A sound principle of database naming schemes is that a name should uniquely identify the thing being named within the domain of discourse (see ISO 11179 for example). It is also a convention in relational database design that a foreign key should have the same name as the candidate key that it references.

"id" is certainly a poor name for anything because it tells us nothing about the attribute being named and it is presumably unlikely to be unique within the database.

dportas
Rock on with your badself
Stephanie Page
A: 

Thank you very much for good anwers!

Yosef
read this: http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work/5235#5235
KM
+3  A: 

I wish those who label PK columns as ID would offer a list of benefits.

You get responses like this:

it is very easy to just prefix the column name with the table name using dot notation

User_ID or User.ID

That's not an advantage of just ID as a name. What does it do for you that USER_ID won't do?

Or

I pretty much always choose "ID" because I'm very comfortable with the table aliasing

That's awesome this guy is comfortable, what's the benefit?

Here's my argument:

Would you have any other n columns all named exactly the same, all meaning completely different things? Would you allow the column "COST" to mean 5 different things in five different tables? Wouldn't you want to call them, "TOTAL_COST", "DIRECT_COST", "AVERAGE_COST", etc? SO why would you do that with ID? If you wouldn't do that for any other column, be consistent.

I use the dictionary tables a lot. I like to query the database's own tables for discovery. Sometimes you'll find missing referential integrity constraints. If the columns are named the same, you can find most missing constraints with no other information. You look for PK's, get the column name USER_ID, Look for other tables with USER_ID, check to see if it has a constraint. If you name your PK ID, you'll also have to know what the convention is Is the FK column named, %Table%_ID or %Table%ID or %Table%_FK or %Table%FK? A tautological column naming convention is much simpler than some function.

Stephanie Page
+1, I agree 100%
KM
Main benefits: (1) a.dep_id = b.id - immediately clear which is primary key. (2) dep.dep_id is obviously (for me) a self referencing FK.
jva
As I wrote, using "ID" as the primary key is more predictable across the domain. That's the benefit. It's also terser, though that's just a bonus. I do use the entity_id pattern for foreign keys in my tables; if I used entity_id for the primary key, I wouldn't be able to distinguish foreign from primary keys. (Again, predictability). Although I agree with your example of cost, since the descriptors aren't related to the entity per se, I would generally use the same name that mean the same thing across all tables consistently: product.name, customer.name, for example.
JasonTrue
@jva and JasonTrue: Why is that a benefit that it's "immediately clear"? Is it that hard to see what "a" and "b" are? Since this is a join condition either a or b is probably defined a few characters prior. [User u inner join company c on u.company_id = c.company_id] Guess what, I too can immediately see which is the PK. <tablename>_ID is just as predictable, just as recognizable. In today's world I completely disagree that terseness is a benefit. Long Names are good, they help people read code. intellisense has removed "keystrokes" as a disadvantage.
Stephanie Page
@KM, are you sure? ;-)
Stephanie Page