Column Naming
June 7th, 2007 — ddelmoliLaurent’s post on column qualification reminded me of a conversation I had with one of my staff about column naming standards. But before I get to that conversation, I’d like to share my favorite example about using table aliases.
I’m a big fan of always using table aliases and always qualifying column names. My favorite example of why it’s a good idea to do it is as follows:
select * from v$session where saddr in (select saddr from v$transaction)
Can you spot the problem here? There isn’t an saddr column in v$transaction. But if you run that query, you don’t get an error. In fact, you get all of the sessions from v$session — when you probably only wanted the sessions which had active transactions. If you were in the habit of always qualifying the table names like so:
select s.* from v$session s where s.saddr in (select t.saddr from v$transaction t)
You’d get ORA-00904: “T”.”SADDR”: invalid identifier.
You can argue about whether or not the original statement should throw an error, but it doesn’t matter. If you used the good practice of always aliasing tables and qualifying columns, you wouldn’t need to waste your breath.
One “argument” I hear against qualifying column names comes from people who have a policy of making sure every column name in the system is unique, usually by including some kind of table name abbreviation prefix on every column in a table. My staff member asked me my opinion of this. Bleh. I’ve got nothing really against it, other than I think it’s not feasible to enforce and it hampers readability. I also don’t like how that policy treats foreign key columns — usually inconsistently. And in the end, it seems to just be an excuse for not having to qualify columns in queries anyway
June 7th, 2007 at 3:27 pm
I posted about this on http://laurentschneider.blogspot.com/2005/06/unexpected-results.html
June 7th, 2007 at 4:34 pm
Perhaps if we all say it loud enough, and often enough, we’ll stop seeing it!
June 8th, 2007 at 6:18 am
There is even a metalink note on this topic
https://metalink.oracle.com/metalink/plsql/ml2_documents.showDocument?p_database_id=NOT&p_id=281493.1
June 8th, 2007 at 10:34 am
Yeah, but the note says it only applies to 8.1.7 through 10.1.0.2
Hopefully that doesn’t mean it’s “fixed” in 10.2.0.3