Professional Documents
Culture Documents
Last updated: May 17th '01 | Best viewed with: All popular browsers | Best viewed at: 1024x768 | Links to
external sites will open in a new window
About myself NEW!!! Subscribe to my newsletter:
My technical Want to keep in touch with the latest in SQL Server world? Email
skills vyaskn@hotmail.com with 'subscribe' in the subject line
See
My favorites also:
My picture Database programming
SQL Server DBA and developer interview questions
album guidelines, conventions
Evaluation of federated database SQL Server FAQ on programming, designing,
servers administration, tools, replication
Shortcut SQL Server security best
SQL Server administration best practices
keys practices
My code
library There exist so many different naming conventions for database objects,
none of them is wrong. It's more of a personal preference of the person
VB resources who designed the naming convention. However, in an organization, one
SQLServer person (or a group) defines the database naming conventions,
resources standardizes it and others will follow it whether they like it or not.
SQLServer
books My database object naming conventions:
Replication
FAQ I came up with a naming convention which is a mixture of my own ideas
Scripting and views of SQL experts like Joe Celko! This article references Microsoft
resources SQL Server databases in some examples, but can be used generically
ASP with other RDBMSs like Oracle, Sybase etc. too. So, here's my preferred
resources naming convention for:
Search my
• Tables
site
Sign my • Views
guestbook • Stored procedures
Contact • User defined functions
information • Triggers
• Indexes
SQL Server • Columns
Articles New
• User defined data types
This month's 2 • Primary keys
click survey: • Foreign keys
Is .NET
important for a • Default and Check constraints
database • Variables
professional?
Tables:
Tables represent the instances of an entity. For example, you store all
your customer information in a table. Here, 'customer' is an entity and
all the rows in the customers table represent the instances of the entity
'customer'. So, why not name your table using the entity it represents,
Receive & 'Customer'. Since the table is storing 'multiple instances' of customers,
Send money make your table name a plural word.
over the net
easily! So, name your customer table as 'Customers'.
Name your order storage table as 'Orders'.
Name your error messages table as 'ErrorMessages'.
If your database deals with different logical functions and you want to
group your tables according to the logical group they belong to, it won't
hurt prefixing your table name with a two or three character prefix that
can identify the group.
For example, your database has tables which store information about
Sales and Human resource departments, you could name all your tables
related to Sales department as shown below:
SL_NewLeads
SL_Territories
SL_TerritoriesManagers
You could name all your tables related to Human resources department
as shown below:
HR_Candidates
HR_PremierInstitutes
HR_InterviewSchedules
This kind of naming convention makes sure, all the related tables are
grouped together when you list all your tables in alphabetical order.
However, if your database deals with only one logical group of tables,
you need not use this naming convention.
Note that, sometimes you end up vertically partitioning tables into two
or more tables, though these partitions effectively represent the same
entity. In this case, append a word that best identifies the partition, to
the entity name.
Views:
A view is nothing but a table, for any application that is accessing it. So,
the same naming convention defined above for tables, applies to views
as well, but not always. Here are some exceptions:
2) Views can summarize data from existing base tables in the form of
reports. You can see this type of views in the 'Northwind' database that
ships with SQL Server 7.0 and above. Here's the convention that
database follows. (I prefer this):
Stored procedures:
Stored procedures always do some work for you, they are action
oriented. So, let their name describe the work they do. So, use a verb to
describe the work.
ORD_InsertOrder
ORD_InsertOrderDetails
ORD_ValidateOrder
If you are using Microsoft SQL Server, never prefix your stored
procedures with 'sp_', unless you are storing the procedure in the
master database. If you call a stored procedure prefixed with sp_, SQL
Server always looks for this procedure in the master database. Only
after checking in the master database (if not found) it searches the
current database.
In Microsoft SQL Server 2000, User Defined Functions (UDFs) are almost
similar to stored procedures, except for the fact that UDFs can be used
in SELECT statements. Otherwise, both stored procedures and UDFs are
similar. So, the naming conventions discussed above for stored
procedures, apply to UDFs as well. You could even use a prefix to
logically group your UDFs. For example, you could name all your string
manipulation UDFs as shown below:
str_MakeProperCase
str_ParseString
Triggers:
So, here's how I would name the insert, update and delete trigger on
titles table:
titles_instrg
titles_updtrg
titles_deltrg
Microsoft SQL Server 7.0 started allowing more than one trigger per
action per table. So, you could have 2 insert triggers, 3 update triggers
and 4 delete triggers, if you want to! In SQL Server 7.0 you can't control
the order of firing of these triggers, however you have some control
over the order of firing in SQL Server 2000. Coming back to the point, if
you have 2 insert triggers on titles table, use the following naming
convention to distinguish the triggers:
titles_ValidateData_instrg
titles_MakeAuditEntries_instrg
If you have a single trigger for more than one action (same trigger for
insert and update or update and delete or any such combination), use
the words 'ins', 'upd', 'del' together in the name of the trigger. Here's an
example. If you have a single trigger for both insert and update on titles
table, name the trigger as titles_InsUpdtrg
Indexes:
Just like triggers, indexes also can't exist on their own and they are
dependent on the underlying base tables. So, again it makes sense to
include the 'name of the table' and 'column on which it's built' in the
index name. Further, indexes can be of two types, clustered and
nonclustered. These two types of indexes could be either unique or non-
unique. So, the naming convention should take care of the index types
too.
For example, I would name the unique, clustered index on the TitleID
column of Titles table as shown below:
Titles_TitleID_U_Cidx
Publishers_PubID_U_Nidx
OrderDetails_OrderID_NU_Nidx
OrderDetails_OrderIDOrderDetailID_U_Cidx
Sure, these index names look long and ugly, but who is complaining?
You'll never need to reference these index names in code, unless you
are creating/dropping/rebuilding the indexes. So, it's not a pain, but it's
a very useful naming convention.
Columns:
CustomerID
CustomerFirstName
CustomerAddress
As shown above, it'll be a good idea to prefix the column names with
the entity that they are representing.
Here's another idea. Decide on a standard two to four character code
for each table in your database and make sure it's unique in the
database. For example 'Cust' for Customers table, 'Ord' for Orders
tables, 'OrdD' for OrderDetails table, 'Adt' for Audit tables etc. Use this
table code to prefix all the column names in that table. Advantage of
this convention is that in multi-table queries involving complex joins,
you don't have to worry about ambiguous column names, and don't
have to use table aliases to prefix the columns. It also makes your
queries more readable.
Cust_CustomerID
The same CustomerID column appears in the Orders table too, but in
Orders table, here's how it's named:
Ord_CustomerID
User defined data types are just a wrapper around the base types
provided by the database management system. They are used to
maintain consistency of data types across different tables for the same
attribute. For example, if the CustomerID column appears half a dozen
tables, you must use the same data type for all the occurrences of the
CustomerID column. This is where user defined data types come in
handy. Just create a user defined data type for CustomerID and use it as
the data type for all the occurrences of CustomerID column.
So, the simplest way of naming these user defined data types would be:
Column_Name + '_type'. So, I would name the CustoerID type as:
CustomerID_type
Primary keys:
Primary key is the column(s) that can uniquely identify each row in a
table. So, just use the column name prefixed with 'pk_' + 'Table name'
for naming primary keys.
Here's how I would name the primary key on the CustomerID column of
Customers table:
pk_Customers_CustomerID
Foreign keys:
Based on the above convention, I would name the foreign key which
references the CustomerID column of the Customers table from the
Order's tables CustomerID column as:
fk_OrdersCustomerID_CustomersCustomerID
Variables:
For variables that store the contents of columns, you could use the
same naming convention that we used for Column names.
• I make sure I'm not using any reserved words for naming my
database objects, as that can lead to some unpredictable
situations. To get a list of reserved words for Microsoft SQL
Server, search Books Online for 'Reserved keywords'