<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'Datadude' and 'naming conventions'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=Datadude,naming+conventions&amp;orTags=0</link><description>Search results matching tags 'Datadude' and 'naming conventions'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Enforcing naming conventions using database unit testing</title><link>http://sqlblog.com/blogs/jamie_thomson/archive/2010/10/05/enforcing-naming-conventions-using-database-unit-testing.aspx</link><pubDate>Tue, 05 Oct 2010 22:42:17 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:29199</guid><dc:creator>jamiet</dc:creator><description>&lt;h3&gt;My naming convention obsession&lt;/h3&gt;  &lt;p&gt;Anyone that has ever worked with me will tell you that I am a stickler for naming conventions. I have a somewhat obsessive reputation for it; I can’t help it – I seem to have a deep seated uncontrollable desire to ensure that every object in my database(s) is/are named consistently (is there anyone else out there equally as obsessive?). &lt;/p&gt;  &lt;p&gt;I have tried various techniques down the years to try and enforce naming conventions but none of them really worked. I’ve got scripts that alter object names (such a script is in my &lt;a href="http://sqlblog.com/blogs/jamie_thomson/archive/2010/10/03/take-your-script-library-with-you-t-sql.aspx" target="_blank"&gt;script library&lt;/a&gt; in fact) but these are only any use if they actually get run, they don’t actually &lt;em&gt;enforce &lt;/em&gt;the conventions – that’s a manual step. I’ve thought about using Policy-Based Management (PBM) to enforce naming conventions but given I’m a developer and not a DBA that’s not something that is readily available to me and besides, using PBM to enforce naming conventions is reactive rather than proactive if you are developing the code on a machine where the policies are not enforced.&lt;/p&gt;  &lt;p&gt;Another option I looked into using was Team Foundation Server (TFS) check-in policies; these are policies that can be applied to artefacts when they get checked-in to TFS’s source control system. This option really appealed to me because the naming conventions could be enforced during check-in (i.e. very very early) and didn’t require DBA intervention. In practice though enforcing naming conventions using TFS check-in policies has a few sizable issues:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Its not easy. It would require you to parse the file that was getting checked-in, decide what sort of object is defined in the file, and then check the name of the object based on things like object name, schema, etc... &lt;/li&gt;    &lt;li&gt;TFS check-in policies are not installed on the TFS server, they are installed on the development workstations. This means there is a dependency and, even though the source code for the check-in policies can be distributed with your application source code, I didn’t really like this. &lt;/li&gt;    &lt;li&gt;You’re relying on each developer to enforce the check-in policy and with the greatest will in the world….that aint gonna happen. Its too easy to turn them off. &lt;/li&gt;    &lt;li&gt;There is the obvious dependency on using TFS, not something every development shop uses even in the Microsoft space. &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Database unit testing to the rescue&lt;/h3&gt;  &lt;p&gt;No, a better solution was needed and I came up with one in the shape of automated database unit testing. I have spoken recently about how I have become a big fan of database unit testing (see my post &lt;a href="http://sqlblog.com/blogs/jamie_thomson/archive/2010/09/18/experiences-from-writing-sp-cascadingdataviewer-db-unit-testing-and-code-distribution.aspx" target="_blank"&gt;Experiences from writing sp_CascadingDataViewer – DB unit testing and code distribution&lt;/a&gt;) and being able to enforce naming conventions is one very good reason for that. Enforcing naming conventions using automated unit tests has a number of advantages:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;They can be written against the metadata of the objects themselves (i.e. by querying SQL Server’s system views) so there’s no parsing that needs to be done. &lt;/li&gt;    &lt;li&gt;They can be employed as part of a Continuous Integration (CI) process and run as a build verification test (BVT). Someone checks-in an object that violates the naming convention? Bang: broken build! &lt;/li&gt;    &lt;li&gt;Developers can’t circumvent the tests. &lt;/li&gt;    &lt;li&gt;Nothing needs to be installed on the development workstations. The tests live wholly as part of your source code. &lt;/li&gt;    &lt;li&gt;Not dependent on use of a particular source control system &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Hence I have written some unit tests that enforce the following naming conventions:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Check constraints must be of the form CK_&amp;lt;schemaName&amp;gt;&amp;lt;tableName&amp;gt;_XXX &lt;/li&gt;    &lt;li&gt;Column names must begin with a capital letter &lt;/li&gt;    &lt;li&gt;Column names cannot contain underscores &lt;/li&gt;    &lt;li&gt;Default constraints must be named DF_&amp;lt;schemaName&amp;gt;&amp;lt;tableName&amp;gt;_&amp;lt;ColumnName&amp;gt; &lt;/li&gt;    &lt;li&gt;Foreign keys must be of the form FK_&amp;lt;parentObjectSchema&amp;gt;&amp;lt;parentObject&amp;gt;_REF_&amp;lt;referencedObjectSchema&amp;gt;&amp;lt;referencedObject&amp;gt;XXX &lt;/li&gt;    &lt;li&gt;Non-unique clustered keys must be of the form IXC_&amp;lt;schemaName&amp;lt;TableName&amp;gt;_&amp;lt;Column&amp;gt;&amp;lt;Column&amp;gt;&amp;lt;Column&amp;gt;… &lt;/li&gt;    &lt;li&gt;Non-unique non-clustered keys must be of the form IX_&amp;lt;schemaName&amp;gt;&amp;lt;TableName&amp;gt;_&amp;lt;Column&amp;gt;&amp;lt;Column&amp;gt;&amp;lt;Column&amp;gt;... &lt;/li&gt;    &lt;li&gt;Unique clustered keys must be of the form IXUN_&amp;lt;schemaName&amp;gt;&amp;lt;TableName&amp;gt;_&amp;lt;Column&amp;gt;&amp;lt;Column&amp;gt;&amp;lt;Column&amp;gt;… &lt;/li&gt;    &lt;li&gt;Unique non-clustered keys must be of the form IXUN_&amp;lt;schemaName&amp;gt;&amp;lt;TableName&amp;gt;_&amp;lt;ColumnColumnColumn&amp;gt;...      &lt;ul&gt;&lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Primary keys must be of the form PK_&amp;lt;schemaName&amp;gt;&amp;lt;tableName&amp;gt; &lt;/li&gt;    &lt;li&gt;Stored procedure names should not contain underscores &lt;/li&gt;    &lt;li&gt;Stored procedure names must begin with a capital letter &lt;/li&gt;    &lt;li&gt;Table names must not contain underscores &lt;/li&gt;    &lt;li&gt;Table names must begin with a capital letter &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I’m not stating that you should agree with these naming conventions (I don’t necessarily agree with them myself – they were defined before I arrived on my current project), the point here is that all of these rules can be enforced and its very easy to do it. Here’s the code for the unit test that enforces the&amp;#160; primary key naming convention:&lt;/p&gt; &lt;code style="font-size:12px;"&gt;&lt;span style="color:black;"&gt;&lt;font face="Consolas"&gt;&amp;#160;&amp;#160; &lt;/font&gt;&lt;/span&gt;&lt;span style="color:green;"&gt;&lt;font face="Consolas"&gt;/*PK name is PK_&amp;lt;schemaName&amp;gt;&amp;lt;tableName&amp;gt;*/       &lt;br /&gt;&amp;#160;&amp;#160; &lt;/font&gt;&lt;/span&gt;&lt;font face="Consolas"&gt;&lt;span style="color:blue;"&gt;SET NOCOUNT ON       &lt;br /&gt;&amp;#160;&amp;#160; DECLARE &lt;/span&gt;&lt;span style="color:#434343;"&gt;@cnt&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:gray;"&gt;;       &lt;br /&gt;        &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT&amp;#160; &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:gray;"&gt;*       &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;INTO&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:#434343;"&gt;#t       &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;FROM&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:gray;"&gt;(       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT&amp;#160; &lt;/span&gt;&lt;span style="color:magenta;"&gt;OBJECT_NAME&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;c.[parent_object_id]&lt;/span&gt;&lt;span style="color:gray;"&gt;) &lt;/span&gt;&lt;span style="color:blue;"&gt;AS &lt;/span&gt;&lt;span style="color:black;"&gt;[TableName]&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;OBJECT_SCHEMA_NAME&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;c.[parent_object_id]&lt;/span&gt;&lt;span style="color:gray;"&gt;) &lt;/span&gt;&lt;span style="color:blue;"&gt;AS &lt;/span&gt;&lt;span style="color:black;"&gt;[SchemaName]&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;c.&lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:gray;"&gt;*       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;FROM&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:black;"&gt;[sys].[key_constraints] c       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;INNER JOIN &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:black;"&gt;[sys].[tables] t        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;ON&amp;#160; &lt;/span&gt;&lt;span style="color:black;"&gt;c.[parent_object_id] &lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:black;"&gt;t.[object_id]&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:magenta;"&gt;LEFT &lt;/span&gt;&lt;span style="color:gray;"&gt;OUTER &lt;/span&gt;&lt;span style="color:blue;"&gt;JOIN &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:black;"&gt;sys.extended_properties ep       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;ON&amp;#160; &lt;/span&gt;&lt;span style="color:black;"&gt;t.[object_id] &lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:black;"&gt;ep.major_id       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:gray;"&gt;AND &lt;/span&gt;&lt;span style="color:black;"&gt;ep.[name] &lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:red;"&gt;'microsoft_database_tools_support'       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;WHERE &lt;/span&gt;&lt;span style="color:black;"&gt;ep.[major_id] &lt;/span&gt;&lt;span style="color:blue;"&gt;IS &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:gray;"&gt;NULL       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AND &lt;/span&gt;&lt;span style="color:black;"&gt;c.[type] &lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:red;"&gt;'PK'       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:gray;"&gt;)&lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:black;"&gt;q       &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;WHERE&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:black;"&gt;[name] &lt;/span&gt;&lt;span style="color:gray;"&gt;&amp;lt;&amp;gt; &lt;/span&gt;&lt;span style="color:red;"&gt;N'PK_' &lt;/span&gt;&lt;span style="color:gray;"&gt;+ &lt;/span&gt;&lt;span style="color:black;"&gt;[SchemaName] &lt;/span&gt;&lt;span style="color:gray;"&gt;+ &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:black;"&gt;[TableName]       &lt;br /&gt;        &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;SET&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:#434343;"&gt;@cnt &lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;span style="color:#434343;"&gt;@@ROWCOUNT&lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:gray;"&gt;;       &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;IF&amp;#160; &lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:#434343;"&gt;@cnt &lt;/span&gt;&lt;span style="color:gray;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="color:black;"&gt;0&lt;/span&gt;&lt;/font&gt;&lt;span style="color:gray;"&gt;&lt;font face="Consolas"&gt;)       &lt;br /&gt;&amp;#160;&amp;#160; &lt;/font&gt;&lt;/span&gt;&lt;font face="Consolas"&gt;&lt;span style="color:blue;"&gt;BEGIN       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DECLARE &lt;/span&gt;&lt;span style="color:#434343;"&gt;@msg &lt;/span&gt;&lt;span style="color:blue;"&gt;NVARCHAR&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;2048&lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:gray;"&gt;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT&amp;#160; &lt;/span&gt;&lt;span style="color:#434343;"&gt;@msg &lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;span style="color:red;"&gt;'%d Primary Keys do not conform to naming convention (PK_&amp;lt;schemaName&amp;gt;&amp;lt;tableName&amp;gt;):' &lt;/span&gt;&lt;span style="color:gray;"&gt;+ &lt;/span&gt;&lt;span style="color:magenta;"&gt;STUFF&lt;/span&gt;&lt;span style="color:gray;"&gt;((&lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;&lt;span style="color:red;"&gt;', ' &lt;/span&gt;&lt;span style="color:gray;"&gt;+ &lt;/span&gt;&lt;span style="color:black;"&gt;[name] &lt;/span&gt;&lt;span style="color:blue;"&gt;FROM &lt;/span&gt;&lt;span style="color:#434343;"&gt;#t &lt;/span&gt;&lt;span style="color:black;"&gt;a &lt;/span&gt;&lt;span style="color:blue;"&gt;FOR XML &lt;/span&gt;&lt;span style="color:black;"&gt;PATH&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:red;"&gt;''&lt;/span&gt;&lt;span style="color:gray;"&gt;) ),&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;2&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:red;"&gt;''&lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:gray;"&gt;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;FROM&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:gray;"&gt;(       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT&amp;#160; &lt;/span&gt;&lt;span style="color:black;"&gt;1 &lt;/span&gt;&lt;span style="color:blue;"&gt;AS &lt;/span&gt;&lt;span style="color:black;"&gt;[Id]&lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:gray;"&gt;,*       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;FROM&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:#434343;"&gt;#t &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:black;"&gt;t       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:gray;"&gt;)&lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:black;"&gt;q       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;GROUP&amp;#160;&amp;#160; BY &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:black;"&gt;q.[Id]       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:#434343;"&gt;@msg       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;RAISERROR&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:#434343;"&gt;@msg&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;11&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:#434343;"&gt;@cnt&lt;/span&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;&lt;span style="color:gray;"&gt;);       &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;END&lt;/span&gt;&lt;/font&gt;&lt;/code&gt;  &lt;p&gt;Essentially all it does is pull all of the primary keys out of &lt;font size="1" face="Consolas"&gt;&lt;span style="color:black;"&gt;[sys].[key_constraints]&lt;/span&gt;&lt;/font&gt;, checks to see what the name &lt;em&gt;should &lt;/em&gt;be, then if it finds any that violate the naming convention raise an error containing the names of all the primary keys in question. Here’s the error obtained when running the test against [AdventureWorks] (I’ve highlighted the pertinent bit):&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Test method Prs.SchemaTests.NamingConventions.PrimaryKeys threw exception:      &lt;br /&gt;System.Data.SqlClient.SqlException: &lt;strong&gt;70 Primary Keys do not conform to naming convention (PK_&amp;lt;schemaName&amp;gt;&amp;lt;tableName&amp;gt;)&lt;/strong&gt;:PK_ErrorLog_ErrorLogID, PK_Address_AddressID, PK_AddressType_AddressTypeID, PK_AWBuildVersion_SystemInformationID, PK_BillOfMaterials_BillOfMaterialsID, PK_Contact_ContactID, PK_ContactCreditCard_ContactID_CreditCardID, PK_ContactType_ContactTypeID, PK_CountryRegionCurrency_CountryRegionCode_CurrencyCode, PK_CountryRegion_CountryRegionCode, PK_CreditCard_CreditCardID, PK_Culture_CultureID, PK_Currency_CurrencyCode, PK_CurrencyRate_CurrencyRateID, PK_Customer_CustomerID, PK_CustomerAddress_CustomerID_AddressID, PK_DatabaseLog_DatabaseLogID, PK_Department_DepartmentID, PK_Document_DocumentID, PK_Employee_EmployeeID, PK_EmployeeAddress_EmployeeID_AddressID, PK_EmployeeDepartmentHistory_EmployeeID_StartDate_DepartmentID, PK_EmployeePayHistory_EmployeeID_RateChangeDate, PK_Illustration_IllustrationID, PK_Individual_CustomerID, PK_JobCandidate_JobCandidateID, PK_Location_LocationID, PK_Product_ProductID, PK_ProductCategory_ProductCategoryID, PK_ProductCostHistory_ProductID_StartDate, PK_ProductDescription_ProductDescriptionID, PK_ProductDocument_ProductID_DocumentID, PK_ProductInventory_ProductID_LocationID, PK_ProductListPriceHistory_ProductID_StartDate, PK_ProductModel_ProductModelID, PK_ProductModelIllustration_ProductModelID_IllustrationID, PK_ProductModelProductDescriptionCulture_ProductModelID_ProductDescriptionID_CultureID, PK_ProductPhoto_ProductPhotoID, PK_ProductProductPhoto_ProductID_ProductPhotoID, PK_ProductReview_ProductReviewID, PK_ProductSubcategory_ProductSubcategoryID, PK_ProductVendor_ProductID_VendorID, PK_PurchaseOrderDetail_PurchaseOrderID_PurchaseOrderDetailID, PK_PurchaseOrderHeader_PurchaseOrderID, PK_SalesOrderDetail_SalesOrderID_SalesOrderDetailID, PK_SalesOrderHeader_SalesOrderID, PK_SalesOrderHeaderSalesReason_SalesOrderID_SalesReasonID, PK_SalesPerson_SalesPersonID, PK_SalesPersonQuotaHistory_SalesPersonID_QuotaDate, PK_SalesReason_SalesReasonID, PK_SalesTaxRate_SalesTaxRateID, PK_SalesTerritory_Territor...&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;I am currently including these tests inside a C# test project inside Visual Studio 2010. Visual Studio has a rather nice feature that allows you to link to artefacts in other projects and hence we can host our single test class containing all of these tests in one place and link to it from whichever test project we want (typically you will have a test project per database) thus following the &lt;a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" target="_blank"&gt;DRY principle&lt;/a&gt;. Here I show the dialog that demonstrates adding a link to an existing test class:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/jamie_thomson/image_799DEAA6.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;margin:;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://sqlblog.com/blogs/jamie_thomson/image_thumb_55B5134C.png" width="619" height="424" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;And how it appears in the project. Note that NamingConventions.cs exists in both test projects but one is just a link to the other:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/jamie_thomson/image_12F5C814.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;margin:;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://sqlblog.com/blogs/jamie_thomson/image_thumb_5FB98EAA.png" width="244" height="229" /&gt;&lt;/a&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h3&gt;Wrap-up&lt;/h3&gt;  &lt;p&gt;I’m not sure my colleagues are too happy about these new tests given that they’re now breaking the build more often but nonetheless I think they realise the value (I guess I’ll find out tomorrow when they read this!!!) &lt;img style="border-bottom-style:none;border-left-style:none;border-top-style:none;border-right-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://sqlblog.com/blogs/jamie_thomson/wlEmoticon-smile_7770C610.png" /&gt; All-in-all its working very well for us and I’m now a very happy bunny knowing that naming conventions are being enforced and will continue to be so with zero effort from here on in. I have made the test class that contains all of the tests that I detailed above available on my SkyDrive at &lt;a title="http://cid-550f681dad532637.office.live.com/self.aspx/Public/BlogShare/20101005/MIControlTest.zip" href="http://cid-550f681dad532637.office.live.com/self.aspx/Public/BlogShare/20101005/MIControlTest.zip"&gt;http://cid-550f681dad532637.office.live.com/self.aspx/Public/BlogShare/20101005/MIControlTest.zip&lt;/a&gt;. If you want to use it you should simply be able to drop it into an existing C# database test project and away you go (change the tests to suit your naming conventions of course though).&lt;/p&gt;  &lt;p&gt;Hope this helps. If it does please let me know, I’d really love some feedback on this.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://twitter.com/jamiet"&gt;@Jamiet&lt;/a&gt;&lt;/p&gt;</description></item></channel></rss>