Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Enhancing the readability of your code: Table aliasing in sql Expand / Collapse
Author
Message
Posted Thursday, May 07, 2009 4:46 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, March 17, 2014 7:18 AM
Points: 2,439, Visits: 6,426
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!

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)
Post #711880
Posted Thursday, May 07, 2009 4:53 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 8:46 AM
Points: 335, Visits: 1,956
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 :)

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
Post #711883
Posted Thursday, May 07, 2009 4:59 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, March 17, 2014 7:18 AM
Points: 2,439, Visits: 6,426
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.


--------------------------------------------------------------------------
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)
Post #711886
Posted Thursday, May 07, 2009 5:04 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, March 17, 2014 7:18 AM
Points: 2,439, Visits: 6,426
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 :)

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)
Post #711893
Posted Thursday, May 07, 2009 5:07 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 11, 2011 8:19 AM
Points: 35, 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.


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

Post #711894
Posted Thursday, May 07, 2009 5:08 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 8:46 AM
Points: 335, Visits: 1,956
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.
Post #711897
Posted Thursday, May 07, 2009 5:21 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, April 10, 2014 12:03 PM
Points: 39, Visits: 352
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.
Post #711905
Posted Thursday, May 07, 2009 5:33 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 22, 2012 3:07 AM
Points: 48, 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...
Post #711912
Posted Thursday, May 07, 2009 5:41 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, April 15, 2011 11:15 PM
Points: 224, 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.

Post #711916
Posted Thursday, May 07, 2009 5:45 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 08, 2011 2:19 PM
Points: 3, Visits: 13
Do they have computers in France?
Post #711920
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse