Presents your SQL SERVER E-NEWSLETTER for May 29, 2003 <-------------------------------------------> WHAT'S IN A NAME? IDENTIFIERS All database objects require names in order to use them in functions or operations. SQL Server 2000 creates the object's identifier when the object is defined. Then, the identifier is used to reference the object. Almost all objects require identifiers--with the exception of certain objects, such as constraints. For example, when you create a PRIMARY KEY, the subsequent constraint has no name, as it is inherent in the key creation. There are two kinds of identifiers: regular identifiers, which follow Transact-SQL naming conventions, and delimited identifiers, which do not follow the conventions. Because delimited identifiers don't follow T-SQL rules, they're enclosed in quotation marks or brackets. Delimited identifiers can be especially useful if you're creating a table or a view that requires a particularly descriptive identifier. On the other hand, delimited identifiers may be cumbersome, as they can be long and tedious to type. As the database designer, you need to decide which type of identifier to use. Here are the basic identifier rules: * The first character must be a letter (lowercase or uppercase), @, #, or _. * An object name that begins with an @ symbol must be a local variable. * Since some functions start with two "at" symbols, @@, it's recommended that you don't use @@ to start your object name. * An object that begins with # (the number sign) denotes temporary tables or procedures. An object with two number signs, ##, denotes a global variable. * Subsequent characters can be letters, decimal numbers, or #, $, and _. * Identifiers can be up to 128 characters long. * Identifiers cannot be reserved SQL keywords, either lowercase or uppercase. * No embedded spaces. If you use embedded spaces, the name must be delimited either in quotes or brackets. Now let's look at some names and decide if they're valid. * 5EASYPIECES: It's not valid because identifiers must begin with a letter or @, #, ##, or _. * FIVE EASY PIECES: It's not valid because identifiers cannot have embedded spaces. * "FIVE EASY PIECES": This is valid. * @PHONE: This is only valid as a local variable. * #PHONE: This is only valid as a temporary object. * MY TABLE: This isn't valid for two reasons: There's an embedded space, and table is a reserved keyword. * MYTABLE: This is valid. * TBLTABLE: This is valid. Identifiers may seem like a simple concept, but it's still important to keep the basic rules in mind, especially when typing queries. Even if you know that the correct object name is MyTable, unconsciously adding that embedded space will create a bug that can result in lost time. Therefore, it's better to avoid using confusing or error-prone identifiers altogether; TblEmployees would be a much better alternative. ----------------------------------------