Best Practice/Standard Alias Schema/Table Convention

Bobby P 221 Reputation points
2024-02-21T15:20:45.85+00:00

Just looking for suggestions for best practices and/or standards for table aliases in a Query. I know this is very subjective...but just looking for some general guidelines from experts and their experience and what works and what might not work. Should we use all periods? Ex. [EDW.Member].[FirstName] Or underscores? Ex. [EDW_Member].[FirstName] Where [EDW] is our Schema Name and [Member] is our Table. Is there a preference to use "_" underscore as opposed to "." period or prefer some other designation to separate and segregate the two? Schema and Table Name And we do prefer to include the Schema Name as we also have a [REF] Schema in our [EDW] Database. Thanks for your review and am hoping for some solid replies with solid experience.

SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
66 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,597 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 51,341 Reputation points
    2024-02-21T16:14:01.55+00:00

    I'm not going to be able to provide you suggestions or best practices for naming so I'll let the others speak. However I would never recommend using a dot in a name. The reason is that if you do that then you'll always have to wrap the name in square brackets otherwise it could cause problems. SQL uses a dot to separate server, schema, table, column names. If you have a dot in the middle of one of those then it is going to get confused. For example EDW.member, is that the Member table of EDW schema, the Member column of the EDW table, or what? It gets even more confusing when you need the full name <server>.<schema>.EDW.Member.<column>. To prevent confusion you'll always have to wrap it in brackets (e.g. <server>.<schema>.[EDW.Member].<column>) which I personally don't like.

    If you had to choose I would say use an underscore. But honestly I wouldn't even do that. I don't feel that EDW_Member is any more readable then simply EDWMember or EdwMember. While SQL doesn't care about the casing, it does make it easier for the rest of us. If you really, really need have clarification then you could also use schemas. So EDW.Member would be the Member table in the EDW schema. This is useful for databases with large, logically separated tables. Refer to WorldWideImporter database as an example.

    0 comments No comments

  2. Erland Sommarskog 106.5K Reputation points
    2024-02-21T22:38:22.4966667+00:00

    I don't there are any general answers here. It depends quite a bit on local custom.

    In a system I worked with for many years, we had three-letter abbreviations for all tables that were used in stored procedure names etc. These official abbreviations lend themselves well to be used as aliases. But obviously, that is not going to work some place where you don't have these established abbreviations.

    Where I am now, I tend to use "natural" abbreviations as long as possible. For the Project table, I use P. For ProjectOrganization I use PO etc. But obviously sometimes this breaks down.

    As for [EDW.Member] vs [EDW_Member], both are too long for my taste. I want the aliases to be short, to reduce the noise level. And aliases that needs quoting with brackets? No way!

    But it is all a matter of personal taste.

    0 comments No comments

  3. Bruce (SqlWork.com) 61,266 Reputation points
    2024-02-22T00:47:33.9466667+00:00

    If the alias is not shortcut, why use it? Just use the actual table name. As above I use simple abbreviations of a couple chars. When multiple joins to the same table just append a digit.

    0 comments No comments