SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Enhancing the readability of your code: Table aliasing in sql


Enhancing the readability of your code: Table aliasing in sql

Author
Message
Jan Van der Eecken
Jan Van der Eecken
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2648 Visits: 6495
David Hutcheson (5/7/2009)
What Jeff Moden said.

I totally agree with his point about leading commas. It might save you an error or two when you forget to delete it from the end of the previous row, but you have completely destroyed the readability of it.


Well, I don't agree that it destroys readability, in fact I find it a lot more readable. But then that's me. Darn, I see Jeff rigging up the pork chop bazooka in front of my place. Keep 'em coming boy, I need something for supper! :-P

Be that as it may, I also use aliases as soon as there are 2 or more tables/subselects involved and keep them kinda meaningful. Always upper-case by the way, since I make table/view names upper-case as well. Oh, by the way, I hate the imposing behaviour that SQL2K8 (Un-)Intellisense cases my table names the way they are in the database. Does anyone else have the same gripe?

--------------------------------------------------------------------------
A little knowledge is a dangerous thing (Alexander Pope)

In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)
Samuel Vella
Samuel Vella
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1079 Visits: 2144
Jan Van der Eecken (5/7/2009)
Always upper-case by the way, since I make table/view names upper-case as well. Oh, by the way, I hate the imposing behaviour that SQL2K8 (Un-)Intellisense cases my table names the way they are in the database. Does anyone else have the same gripe?


There is so much wrong with this Smile

Table object is named in one manner e.g. MyTable
You refer to it as MYTABLE in your queries... removing any consitancies

SQL Server 2008 Intellisense will be using the same case as the table name in the database as a case sensitive server will not equate [MYTABLE] to [MyTable] which will mean your query will fail

I really can't see any benefit in not using the same case as the object naturally has, unless the object has been really badly named (CUSTOMERRELATIONSHIPMANAGEMENTTABLEHEIRACHY) and its a case insensitive server
Jan Van der Eecken
Jan Van der Eecken
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2648 Visits: 6495
lfallis (5/7/2009)
Each to their own again... I prefer proper case as personally I find it more readable. Keywords are highlighted differently anyway (working on the assumption that their aren't many cases where a developer would be working without syntax highlighting).
Interestingly UK (and many other countries) road signs are always in proper case e.g. London instead of LONDON as proper case is quicker to read

Code readability, much like object naming conventions usually comes down to systems. As long as there is a system in place, e.g. always trailing commas, always upper case for keywords and always using the same aliases for the same main group of tables (every system has a core of at most 10 tables which are used in 90% of the queries) then it doesn't matter what the system is as long as it makes sense and is stuck to within a team


I agree also (although being from England that may be why).

For anyone who has read a book on GUI's, most would advise to stay away from CAPITALS as they block out the screen more and make it more difficult to read. I always prefer lowercase, using capitals where words are joined and aliases even where there are no joins i.e. from myTable mt join anotherTable at on...

You never know when you are going to have to add a join in, and then you will need to go through and add them for the possible duplicate columns.


Are you showing REAL table and column names in your GUI's? If you do, don't outsource DB dev to a non-english speaking developer :-) Believe me, I have to cope with a database that goes with a product that was created in France. All table and column names are French. And although I do speak French as one of the four languages I mastered (kind of), I have a hard time. Guess what your users would be doing to their hairdo if the word "ordinateur" showed up on their screens. w00t

--------------------------------------------------------------------------
A little knowledge is a dangerous thing (Alexander Pope)

In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)
Jan Van der Eecken
Jan Van der Eecken
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2648 Visits: 6495
Samuel Vella (5/7/2009)
Jan Van der Eecken (5/7/2009)
Always upper-case by the way, since I make table/view names upper-case as well. Oh, by the way, I hate the imposing behaviour that SQL2K8 (Un-)Intellisense cases my table names the way they are in the database. Does anyone else have the same gripe?


There is so much wrong with this Smile

Table object is named in one manner e.g. MyTable
You refer to it as MYTABLE in your queries... removing any consitancies

SQL Server 2008 Intellisense will be using the same case as the table name in the database as a case sensitive server will not equate [MYTABLE] to [MyTable] which will mean your query will fail

I really can't see any benefit in not using the same case as the object naturally has, unless the object has been really badly named (CUSTOMERRELATIONSHIPMANAGEMENTTABLEHEIRACHY) and its a case insensitive server


Sorry to disagree, but I don't see what's wrong with this, unless your collation sequence is case-sensitive, which in most cases is a mistake in the first place. What I do have a gripe with is that I want my code to look consistent, which Intellisense prevents me from doing if somewhere in history some developer made one single table name CamelCase, but all the other ones are as per coding standards (whatever those may be).

--------------------------------------------------------------------------
A little knowledge is a dangerous thing (Alexander Pope)

In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)
lfallis
lfallis
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 178
Are you showing REAL table and column names in your GUI's? If you do, don't outsource DB dev to a non-english speaking developer :-) Believe me, I have to cope with a database that goes with a product that was created in France. All table and column names are French. And although I do speak French as one of the four languages I mastered (kind of), I have a hard time. Guess what your users would be doing to their hairdo if the word "ordinateur" showed up on their screens. w00t


Ha, no I wasn't speaking from a database point of view. I meant advice on warnings, messages, general HCI that sort of thing.

:-D
Samuel Vella
Samuel Vella
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1079 Visits: 2144
Jan Van der Eecken (5/7/2009)

Sorry to disagree, but I don't see what's wrong with this, unless your collation sequence is case-sensitive, which in most cases is a mistake in the first place. What I do have a gripe with is that I want my code to look consistent, which Intellisense prevents me from doing if somewhere in history some developer made one single table name CamelCase, but all the other ones are as per coding standards (whatever those may be).


Naming and case conventions should be consistant across the database, this includes all table, procedure, function, variable names and the code which is being written for the database

If some developer has named a table CamelCase instead of CAMELCASE then they need re-education on how the system at that organisation works and the object should be renamed.
scott mcnitt
scott mcnitt
SSC-Enthusiastic
SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)

Group: General Forum Members
Points: 152 Visits: 437
YIKES (not yelling).

Two pages so far on leading commas, UPPER or lower case, the horror and amateurishness of lower cased key words...at least I do not have French-flavored SQL (in my mind I have Inspector Clouseau reading the query to me-- "aye sayd, odor bye beumb").

Next I suppose we will start a rant about GOTO.
David Hutcheson
David Hutcheson
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 100
scott mcnitt (5/7/2009)
YIKES (not yelling).

Two pages so far on leading commas, UPPER or lower case, the horror and amateurishness of lower cased key words...at least I do not have French-flavored SQL (in my mind I have Inspector Clouseau reading the query to me-- "aye sayd, odor bye beumb").

Next I suppose we will start a rant about GOTO.


And here was me thinking that this was an article about readability...
pp.pragnesh
pp.pragnesh
SSC Veteran
SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)

Group: General Forum Members
Points: 264 Visits: 53
Hi, Sylvia

I totally agree with you, many of the developers are using meaningless aliasing and that would be very tough to debug in future and waste of time. even i found some developers are not using proper indentation. It will be always good practice to provide proper indentation like below


SELECT Table1.Field1, Table1.Field2,
Table2.Field3, Table2.Field4,
FROM Table1
LEFT OUTER JOIN Table2 ON Table2.Field1 = Table2.Field3
WHERE Table1.Field1 = CompareValue1
AND Table2.Field3 = CompareValue2



Also it is good practice to provide the syntax / reserved words in Upper case for better readability.
Gene Marini
Gene Marini
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 13
Do they have computers in France?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search