DB Naming

  • I am trying to establish the standards for my database naming convention. Can you please provide me the best standards which worked for you. Thanks in Advance!

  • "best" standards really depend on your environment and how often objects will change and how your team works.

    For example, I like naming my tables with the name of the entity it will contain in singular form (ie it defines a row).  So I like having a "person" table vs a "people" table, or employee vs employees.

    Others prefer having the table name describe the contents of the table and thus prefer it in plural form.

    With stored procedures, I like a 2-3 letter acronym at the start to define the process (adm for admin, fin for finance, etc) followed by _ and then what type of procedure it is (read only (RO), write only (WO), read/write (RW)) followed by another _ then the name of the procedure where it will describe the end result of the process upon success.  Such as "adm_RW_ResetPassword".

    Views, we prefix with "vw_" followed by the purpose such as "vw_EmployeeSalary" which would map the Employee table and Salary table together in a view.

    Triggers we prefix with trg followed by the type of trigger (I for insert, U for update, D for delete, and can combine them like IU for insert or update) followed by _ and then what the trigger does.  So "trgIUD_AuditLog" for logging an audit trail of changes on a table for example.

    Our overall document for standards is lengthy but contains 2 main sections - requirements and recommendations.  Requirements has naming conventions that are hard requirements and not following it will result in code verification failure.  Recommendations is things that we recommend to do, but won't cause code verification failure.  Our SQL Coding standards expand past naming conventions and include things like DROP and CREATE vs ALTER and Operations and keywords to avoid (such as CURSORs and NOLOCK).

    We like having the hard requirements (don't use deprecated datatype for example, or don't start a stored procedure with sp_), and we like having recommendations (such as avoid using OPTION(RECOMPILE)) as it gives the developers flexibility.  Nothing more annoying than spending a month working on the SQL back end for a new application only to have it fail verification because you used tabs instead of spaces in your code.

    The best standards that work for my team (tiny little team of 3 developers who also do DBA work) is to make it flexible and have open communication during code review.  If I see something that I think would be hard to support, I bring it up with the developer and we talk through it.  Sometimes it is the way to go, sometimes we come up with more efficient ways to code it.  I worked with a developer to remove a cursor from their code and it cut the execution time down by about 80%.  Now, if it was an urgent release and needed to go live immediately, the cursor could have remained as we have it as a "recommendation" not a "requirement".  It allows for flexibility in the code and for faster release cycles with the intention to go back and improve the code at a future date if needed.  Sometimes, the code runs "good enough", so we don't bother going back to fix things.  A report (for example) that takes 20 seconds to load, but is a scheduled report that fires in the middle of the night and nobody looks at live data doesn't need to be optimized.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • In general:

    • Be consistent. A mediocre naming convention applied consistently is more user-friendly and less error-prone than a great convention applied only sporadically.
    • Don't obsess -- don't spend too much time, energy, or emotion on naming, and don't fight religious wars over it.
    • Model the real world. Don't use cutesy names like your girlfriend, favorite snack foods, favorite band, etc. (seen all these in code, if not in databases).

      Use clear expressive names and don't abbreviate unless necessary, abbreviation/acronym is almost universally understood in your industry and company, and/or full descriptive name that distinguishes it from similar entities would be insanely long, or even exceed RDBMS limits In general, and within limits, comprehension is better with full expressive names. We're decades beyond need to keep things to 8 characters.

    • Do not use  Leszynski or Hungarian notation. IDEs such as SSMS already categorize objects by type. Prefixing names w/ type is redundant, throws off sorting, and makes it harder to browse and find objects.
    • Plan ahead a little.

      • If you might use views and/or synonyms (within the same database) that are very similar  to the underlying objects -- e.g., to hide/encapsulate schema from applications so that you have more flexibility to refactor and it doesn't become ever-more-difficult to refactor), how will you vary those names? Will you use a suffix (e.g., "View")? In cases where I have singular table names, I use plural view names to distinguish them. Perhaps you could use a different schema if the synonym and table/procedure name are otherwise identical. But have a plan so you don't suddenly finding yourself having to create arbitrary --  and almost inevitably, inconsistent & confusing -- names.
      • There might be rare cases where you want (or be required) to obfuscate column names so as to not draw attention to hacker targets like password (which better be hashed & salted) if they are in the database) or PII such as SSN (which should probably be encrypted)

    •  When objects are only used by certain applications/services, or are specific to certain organizations, use separate schemas to logically separate & organize those objects

    Tables:

    • Be consistent: If you have several tables referencing addresses, don't use "Location" in one table name and "Address" in another. (Note Location might be valid if it represents not a typical address but instead Latitude & Longitude)
    • There are debates on singular vs. plural naming.  I like the argument that a table is a collection and therefore is logically plural. Others see it differently. Some ORMS may assume singular names. Just pick one and be consistent.

    Columns:

    • Use domain naming -- Where possible, use the same name for a column (property/attribute) in all tables that reference it. Don't use CustomerID in one table, customer_id in another, CustID in another, and cust in another. Inconsistent column names are confusing and error prone, leading people to miss foreign key relationships and dependencies or mismap them.  They make ir much harder for ORMs (especially if performance forces you to forego foreign keys. They make it much hard to use schema compare tools to push changes from environment to environment. The exception is where you might have multiple subtypes of an ID.  e.g., you might have HomePhone, BusinessPhone, MobilePhone, etc. (though that is often handled better w/ a table containing something like PhoneType and PhoneNumber)

    Stored procedures and functions:

    • Don't prefix with "sp_" -- that designates system stored procedures & functions.

     

     

  • Thanks. How about the database name standards?

    • This reply was modified 3 years, 1 month ago by  sqlguru.
  • For the database itself, our naming convention is to either name it after the application that uses it, or if multiple applications are using it, then the processes that are contained inside.  For example, "Operations" for all production operation related data, or CONFIGURATION for application configuration related data, or ServiceDesk for the database that will hold all of the Service Desk data, or MessageManager for our service broker centralized logging database.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • I agree w/ Mr. Gale that using prefix "vw" for views and "trg" for triggers is a good, common, reasonable exception to the "don't prefix w/ object type" rule. And these are generally less common objects. Please don't use "tbl" prefix for tables, nor "prc" or "proc" prefix for stored procedures -- usually two of your most common objects!

    (though I personally still prefer using a suffix -- something as simple but useful as clicking on the first letter of an object in the UI to navigate to an object rather than having to searching is broken if every object starts w/ "v" or "t").

     

  • Give your databases names that reflect their business purpose.  Don't use prefixes or suffixes in the name to indicate "database".  MS got this half right: MS has dbs "master", "model", "msdb" (why db??) and "tempdb" (why db??).

    In fact, be consistency is the most important thing.  So, consistently don't include a prefix/suffix for type in base object names.

    Thus, don't use "vw_"/"_view"; "proc"/"sp"; "fn"/"func", etc.  For a view, what happens later if that view then needs to become an actual table ... are you really going to rename it everywhere it's used?  No, of course not, the name will become inaccurate and misleading.  Instead, give a view a name that matches its business function, just like with all other base objects.

    As to triggers, and other elements related to tables, I don't object to TRG and other suffixes in the name.  They aren't base objects.

    Also, I don't start any name with a type prefix (PK_, TRG_, etc.).  People seem to have blindly copied this from MS.  Instead, for tables, I start all names with the table name then add a suffix to match the type.  For example, "table1__TRG_INS" (or INSERT) for an insert trigger; "table1__CL" for the clus index on a table; "table1__IX_column_b" for a nonclus index keyed on column_b; etc..  This approach insures that each table's objects sort together and that each index has its own unique name.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Mr. Brian Gale wrote:

    With stored procedures, I like a 2-3 letter acronym at the start to define the process (adm for admin, fin for finance, etc) followed by _ and then what type of procedure it is (read only (RO), write only (WO), read/write (RW)) followed by another _ then the name of the procedure where it will describe the end result of the process upon success.  Such as "adm_RW_ResetPassword".

    Using a prefix to group functionality for procs makes perfect sense.

    I very strongly object, however, to the RO/RW/WO, etc., approach.  One big problem is: what do you when a proc that was previously read only then needs to do an update?  Do you rename the proc everywhere?  No, you don't, because it's too much hassle.  So now you have an inaccurate name.

    Instead, leave those types of details out of the name.  Just name the proc after the business task it does.  If you want to maintain write info, use a table to store that.  It can be made more accurate and you can use it for searches and analysis.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Whatever you choose, please, please, OH PLEASE, focus first on clarity and communication. Any other kind of rigid enforcement of some particular little persnickety rule must come second. The purpose of a naming standard must be to assist communication, not to hide stuff from the uninitiated.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Keep in mind, too, that the point of naming is to give accurate business names to objects, not to make things easier for developers.  That is, just because a naming style is convenient for developers to use does not automatically make it a good style, no matter how much developers insist otherwise.

    And, yes, the "vw_" thing would fall under that convenient-for-developer-but-bad-approach-for-business/overall functionality naming.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply