Naming Conventions

The main goal of adopting a naming convention for database objects is so that you and others can easily identify the type and purpose of all objects contained in the database. The information presented here serves as a guide for you to follow when naming your database objects. When reading these rules and guidelines, remember that consistent naming can be the most important rule to follow. Please also keep in mind that following the guidelines as outlined in this document can still produce long and cryptic names, but will limit their numbers and impact. However, ultimately your unique situation will dictate the reasonability of your naming convention. The goal of this particular naming convention is to produce practical, legible, concise, unambiguous and consistent names for your database objects.

This section is a generic DBMS-neutral guide for naming common objects. While most databases contain more types of objects than those discussed here (User Defined Types, Functions, Queries, etc.), the 7 types of objects mentioned here are common among all major database systems.

The following types of database objects are discussed here:

  1. Tables
  2. Columns (incl. Primary, Foreign and Composite Keys)
  3. Indexes
  4. Constraints
  5. Views
  6. Stored Procedures
  7. Triggers

All Database Objects

  • Try to limit the name to 50 characters (shorter is better)
  • Avoid using underscores even if the system allows it, except where noted in this document. PascalCase notation achieves the same word separation without them and in fewer characters.
  • Use only letters or underscores (try to avoid numbers – and limit the use of underscores to meet standards for Constraints, Special-Purpose Indexes and Triggers or unless implementing a modular naming convention as defined in this document).
  • Use a letter as the first character of the name. (don't start names with underscores or numbers)
  • Limit the use of abbreviations (can lead to misinterpretation of names)
  • Limit the use of acronyms (some acronyms have more than one meaning e.g. “ASP”)
  • Make the name readable (they shouldn't sound funny when read aloud).
  • Avoid using spaces in names even if the system allows it.

Tables

When naming your database tables, give consideration to other steps in the development process. Keep in mind you will most likely have to utilize the names you give your tables several times as part of other objects, for example, procedures, triggers or views may all contain references to the table name. You want to keep the name as simple and short as possible. Some systems enforce character limits on object names also.

Rule 1a (Singular Names)

Table names should be singular, for example, “Customer” instead of “Customers”. This rule is applicable because tables are patterns for storing an entity as a record – they are analogous to Classes serving up class instances. And if for no other reason than readability, you avoid errors due to the pluralization of English nouns in the process of database development. For instance, activity becomes activities, ox becomes oxen, person becomes people or persons, alumnus becomes alumni, while data remains data.

Rule 1b (Prefixes)

Don’t use prefixes unless they are deemed necessary to help you organize your tables into related groups or distinguish them from other unrelated tables. Generally speaking, prefixes will cause you to have to type a lot of unnecessary characters.

Do not give your table names prefixes like “tb” or “TBL_” as these are redundant and wordy. It will be obvious which names are the table names in SQL statements because they will always be preceded by the FROM clause of the SELECT statement. In addition, many RDBMS administrative and/or query tools (such as SQL Server Management Studio) visually separate common database objects in the development environment. Also note that Rule 5a provides a means to distinguish views from tables.

In some cases, your tables might be sharing a schema/database with other tables that are not related in any way. In this case, it is sometimes a good idea to prefix your table names with some characters that group your tables together. For example, for a healthcare application you might give your tables an “Hc” prefix so that all of the tables for that application would appear in alphabetized lists together. Note that even for the prefix, use PascalCase. This is discussed in Rule 1c. Do not use underscores in your prefixes, which is discussed in more depth in Rule 1d. The last kind of prefix that is acceptable is one that allows you to group logical units of tables. A plausible example could entail a large application (30 to 40+ tables) that handled both Payroll and Benefits data. You could prefix the tables dealing with payroll with a “Pay” or “Prl” prefix and give the tables dealing with benefits data a “Ben” or “Bfts” prefix. The goal of both this prefix and the aforementioned shared schema/database prefix is to allow you to group specific tables together alphabetically in lists and distinguish them from unrelated tables. Lastly, if a prefix is used for this purpose, the shared schema/database prefix is a higher grouping level and comes first in the name, for example, “HcPayClients” not “PayHcClients”.

Rule 1c (Notation)

For all parts of the table name, including prefixes, use Pascal Case. Using this notation will distinguish your table names from SQL keywords (camelCase). For example, “select CustomerId, CustomerName from MyAppGroupTable where CustomerName = '%S'” shows the notation for the table name distinguishing it from the SQL keywords used in the query. PascalCase also reduces the need for underscores to visually separate words in names.

Rule 1d (Special Characters)

For table names, underscores should not be used. The underscore character has a place in other object names but, not for tables. Using PascalCase for your table name allows for the upper-case letter to denote the first letter of a new word or name. Thus there is no need to do so with an underscore character. Do not use numbers in your table names either. This usually points to a poorly-designed data model or irregularly-partitioned tables. Do not use spaces in your table names either. While most database systems can handle names that include spaces, systems such as SQL Server require you to add brackets around the name when referencing it (like [table name] for example) which goes against the rule of keeping things as short and simple as possible.

Rule 1e (Abbreviations)

Avoid using abbreviations if possible. Use “Accounts” instead of “Accts” and “Hours” instead of “Hrs”. Not everyone will always agree with you on what your abbreviations stand for - and - this makes it simple to read and understand for both developers and non-developers. This rule can be relaxed in the sake of space constraints for junction table names (See Rule 1f). Avoid using acronyms as well. If exceptions to this rule are deemed necessary, ensure that the same convention is followed by all project members.

Rule 1f (Junction a.k.a Intersection Tables)

Junction tables, which handle many to many relationships, should be named by concatenating the names of the tables that have a one to many relationship with the junction table. For example, you might have “Doctors” and “Patients” tables. Since doctors can have many patients and patients can have many doctors (specialists) you need a table to hold the data for those relationships in a junction table. This table should be named DoctorPatient“. Since this convention can result in lengthy table names, abbreviations sometimes may be used at your discretion.

Columns

(incl. PRIMARY, FOREIGN, AND COMPOSITE KEYS)

When naming your columns, keep in mind that they are members of the table, so they do not need the any mention of the table name in the name. When writing a query against the table, you should be prefixing the field name with the table name or an alias anyway. Just like with naming tables, avoid using abbreviations, acronyms or special characters. All column names should use PascalCase to distinguish them from SQL keywords (camelCase).

Rule 2a (Identity Primary Key Fields)

For fields that are the primary key for a table and uniquely identify each record in the table, the name should simply be [tableName] + “Id“(e.g.in a Customer table, the primary key field would be “CustomerId”. A prefix is added mainly because “Id” is a keyword in SQL Server and we would have to wrap it in brackets when referencing it in queries otherwise. Though CustomerId conveys no more information about the field than Customer.Id and is a far wordier implementation, it is still preferable to having to type brackets around “Id”.

Rule 2b (Foreign Key Fields)

Foreign key fields should have the exact same name as they do in the parent table where the field is the primary. For example, in the Customers table the primary key field might be “CustomerId”. In an Orders table where the customer id is kept, it would also be “CustomerId”. There is one exception to this rule, which is when you have more than one foreign key field per table referencing the same primary key field in another table. In this situation, it might be helpful to add a descriptor before the field name. An example of this is if you had an Address table. You might have another table with foreign key fields like HomeAddressId, WorkAddressId, MailingAddressId, or ShippingAddressId.

This rule combined with rule 2a makes for much more readable SQL:

... File inner join Directory on File.FileID = Directory.FileID ...

whereas this has a lot of repeating and confusing information:

... File inner join Directory on File.FileId_Pk = Directory.FileId_Fk ...

Rule 2c (Composite Keys)

If you have tables with composite keys (more than one field makes up the unique value), it’s recommended that a seeded identity column is created to use as the primary key for the table.

Rule 2d (Prefixes)

Do not prefix your fields with “fld_” or “Col_” as it should be obvious in SQL statements which items are columns (before or after the FROM clause). Do not use a data type prefix for the field either, for example, “IntCustomerId” for a numeric type or “VcName” for a varchar type. These “clog up” our naming and add little value; most integer fields can be easily identified as such and character fields would have to be checked for length in the Object Browser anyway.

Rule 2e (Data Type-Specific Naming)

Bit fields should be given affirmative boolean names like “IsDeleted”, “HasPermission”, or “IsValid” so that the meaning of the data in the field is not ambiguous; negative boolean names are harder to read when checking values in T-SQL because of double-negatives (e.g. “Not IsNotDeleted”). If the field holds date and/or time information, the word “Date” or “Time” should appear somewhere in the field name. It is sometimes appropriate to add the unit of time to the field name also, especially if the field holds data like whole numbers (“3” or “20”). Those fields should be named like “RuntimeHours” or “ScheduledMinutes”.

Rule 2f (Field Name Length)

Field names should be no longer than 50 characters and all should strive for less lengthy names if possible. You should, however, not sacrifice readability for brevity and avoid using abbreviations unless it is absolutely necessary.

Rule 2g (Special Characters)

Field names should contain only letters and numbers. No special characters, underscores or spaces should be used.

Indexes

Indexes will remain named as the SQL Server default, unless the index created is for a special purpose. All primary key fields and foreign key fields will be indexed and named in the SQL Server default. Any other index will be given a name indicating it’s purpose.

Rule 3a (Naming Convention)

Indexes will remain named as the SQL Server default, unless the index created is for a special purpose, in which case the naming convention for special-purpose indexes follows this structure:

{U/N}IX_{TableName}{SpecialPurpose}

where “U/N” is for unique or non-unique and “IX_” matches the default prefix that SQL Server assigns indexes.

Rule 3b (Prefixes and Suffixes)

Avoid putting any prefix other than that specified in Rule 3a before your special-purpose indexes.

Constraints

Constraints are at the field/column level so the name of the field the constraint is on should be used in the name. The type of constraint (Check, Referential Integrity a.k.a Foreign Key, Primary Key, or Unique) should be noted also. Constraints are also unique to a particular table and field combination, so you should include the table name also to ensure unique constraint names across your set of database tables.

Rule 4a (Naming Convention)

The naming convention syntax for constraints looks like this:

{constraint type}{table name}_{field name}

Examples:

  1. PkProducts_Id - primary key constraint on the Id field of the Products table
  2. FkOrders_ProductId - foreign key constraint on the ProductId field in the Orders table
  3. CkCustomers_AccountRepId - check constraint on the AccountRepId field in the Customers table

The reason underscores are used here with Pascal Case notation is so that the table name and field name are clearly separated. Without the underscore, it would become easy to get confused about where the table name stops and the field name starts.

Rule 4b (Prefixes)

A two letter prefix gets applied to the constraint name depending on the type

  • Primary Key: Pk
  • Foreign Key: Fk
  • Check: Ck
  • Unique: Un

Views

Views follow many of the same rules that apply to naming tables. There are only two differences (Rules 5a and 5b). If your view combines entities with a join condition or where clause, be sure to combine the names of the entities that are joined in the name of your view. This is discussed in more depth in Rule 5b.

Rule 5a (Prefixes)

While it is pointless to prefix tables, it can be helpful for views. Prefixing your views with “vw” is a helpful reminder that you're dealing with a view, and not a table. Whatever type of prefix you choose to apply, use at least 2 letters and not just “v” because a prefix should use more than one letter or its meaning can be ambiguous.

Rule 5b (View Types)

Some views are simply tabular representations of one or more tables with a filter applied or because of security procedures (users given permissions on views instead of the underlying table(s) in some cases). Some views are used to generate report data with more specific values in the WHERE clause. Naming your views should be different depending on the type or purpose of the view. For simple views that just join one or more tables with no selection criteria, combine the names of the tables joined. For example, joining the “Customer” and “StateAndProvince” table to create a view of Customers and their respective geographical data should be given a name like “vwCustomerStateAndProvince”. Views created expressly for a report should have an additional prefix of Report applied to them, e.g. vwReportDivisionSalesFor2008.

Stored Procedures

Unlike a lot of the other database objects discussed here, stored procedures are not logically tied to any table or column. Typically though, stored procedures perform one or more common database activities (Read, Insert, Update, and/or Delete) on a table, or another action of some kind. Since stored procedures always perform some type of operation, it makes sense to use a name that describes the operation they perform. Use a verb to describe the type of operation, followed by the table(s) the operations occur on.

Rule 6a (Prefixes or Suffixes)

The way you name your stored procedures depends on how you want to group them within a listing. It is recommended that you have your procedures ordered by the table/business entity they perform a database operation on, and adding the database activity ” Get, Save, or Delete“ as a suffix, e.g., (“ProductInfoGet” or “OrdersSave”) or (“spProductInfoGet” or “spOrdersSave”).

If your procedure returns a scalar value, or performs an operation like validation, you should use the verb and noun combination. For example, “ValidateLogin”.

The use of the “sp” prefix is acceptable and encouraged. This will help developers identify stored procedures and differentiate them from other non prefixed objects such as tables when viewing a listing of database objects.

Rule 6b (Grouping Prefixes)

If you have many stored procedures, you might want to consider using a grouping prefix that can be used to identify which parts of an application the stored procedure is used by. For example, a “Prl” prefix for Payroll related procedures or a “Hr” prefix for Human Resources related procedures can be helpful. This prefix would come before the table/business entity prefix (See Rule 6a). Please note that this should be avoided unless absolutely necessary.

Rule 6c (Bad Prefixes)

Do not prefix your stored procedures with something that will cause the system to think it is a system procedure. For example, in SQL Server, if you start a procedure with “sp_”, “xp_” or “dt_” it will cause SQL Server to check the master database for this procedure first, causing a performance hit. Spend a little time researching if any of the prefixes you are thinking of using are known by the system and avoid using them if they are.

Functions

Functions follow many of the same rules that apply to naming stored procedures. There are only two differences (Rules 5a and 5b) that exist so the user of the function knows they are dealing with a function and not a stored procedure and all of the details that involves (value returned, can be used in a select statement, etc.).

Rule 7a (Prefixes)

While it is makes little sense to prefix stored procedures, it is necessary to distinguish functions from stored procedures. To that end, you should prefix your functions with “fn” as a helpful reminder that you're dealing with a function, and not a stored procedure.

Rule 7b (Function Purpose)

Functions should be named as a verb, because they will always return a value (e.g. “fnGetOpenDate”, “fnParseTableToString”, “fnFormatZip”, etc.).

Rule 7c (Function Suffix)

Scalar functions do not need a suffix, but table-valued functions should indicate that they return a table by using a “Table” suffix.

Triggers

Triggers have many things in common with stored procedures. However, triggers are different than stored procedures in two important ways. First, triggers don't exist on their own. They are dependent upon a table. So it is wise to include the name of this table in the trigger name. Second, triggers can only execute when an Insert, Update, or Delete happens on one or more of the records in the table. So it also makes sense to include the type of action that will cause the trigger to execute.

Rule 8a (Prefixes)

To distinguish triggers from other database objects, it is helpful to add “tr” as a prefix, e.g. “tr_ProductsIns”. As long as you include the table name, the operation that executes the trigger (Ins, Upd, or Del) and the “tr” prefix, it should be obvious to someone working with the database what kind of object it is. As with all conventions you use, pick one and remain consistent.

Rule 8b (Multiple Operations)

If a trigger handles more than one operation (both INSERT and UPDATE for example) then include both operation abbreviations in your name. For example, “trProductsInsUpd” or “trProductsUpdDel”

Rule 8c (Multiple Triggers)

Some systems allow multiple triggers per operation per table. In this case, you should make sure the names of these triggers are easy to distinguish between, e.g. “trUsers_ValidateEmailAddress_Ins” and “trUsers_MakeActionEntries_Ins”.

User-Defined Data Types

User-defined data types should be avoided whenever possible. They are an added processing overhead whose functionality could typically be accomplished more efficiently with simple data type variables, table variables, or temporary tables.

Rule 9a (Prefixes and Suffixes)

To distinguish a user-defined data type from other database objects, it is helpful to add “ud” as a prefix.

Rule 9b (Special Characters)

User-defined data type names should contain only letters, numbers and underscores. No special characters or spaces should be used.

Variables

In addition to the general naming standards regarding no special characters, no spaces, and limited use of abbreviations and acronyms, common sense should prevail in naming variables; variable names should be meaningful and natural.

Rule 10a (Name length limit)

Variable names should describe its purpose and not exceed 50 characters in length.

Rule 10b (Prefix)

All variables must begin with the “@” symbol. Do NOT user “@@” to prefix a variable as this signifies a SQL Server system global variable and will affect performance.

Rule 10c (Case)

All variables should be written in camelCase, e.g. “@firstName” or “@city” or “@siteId”.

Rule 10d (Special Characters)

Variable names should contain only letters and numbers. No special characters or spaces should be used.

Using Modular Prefixes

When the scope of the database is expected to be enterprise level or scalability is a great concern consider using a modular object naming convention for primary database objects such as stored procedures and views. This methodology involves identifying primary modules of the system and assigning each of those modules a prefix. When these prefixes are applied to the names of database objects this allows us to easily locate module specific procedures and database objects for easier modification and debugging.

Example 1

We have a modular system that deals with students and teacher data separately. We have defined these modules as such:

Module Name Prefix
Student STU
Teacher TEA

As we implement naming conventions for our new objects processing specific to each module we create an easier way to find and view module specific functionality.

  • spSTU_Attendance_InserUpdate
  • spTEA_Credentials_Delete
  • spSTU_ValidateStudentID
  • spTEA_Address_InsertUpdate
  • spSTU_Address_InsertUpdate
  • vwSTU_AllStudents

Example 2

Should you find the need for even more granularity when implementing your naming conventions, consider using sub-modules as well.

Module Name Prefix
Reporting RPT
Data Collection DTA
Sub Module Name Prefix
Student STU
Teacher TEA
  • spDTA_STU_Attendance_InserUpdate
  • spDTA_TEA_Credentials_Delete
  • vwRPT_STU_StudentAchievementReport
  • spRPT_TEA_TeacherQualificationsReport
  • spDTA_STU_Address_InsertUpdate
  • vwDTA_STU_ValidateStudent
templates/process/naming_conventions.txt · Last modified: 2014/10/19 13:38 (external edit)
 
DWH Wiki is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license are available in our license.

Deutsches DWH Wiki | DWH Blog | Recent changes RSS feed | Imprint | Provided by Michael Olschimke