Code Generator-Friendly Database Naming Conventions
Posted by Randolph Cabral on Wednesday October 17, 2007
The Jammer.NET Code Generator is currently designed to read tables and stored procedures from a Microsoft SQL Server database and generate C# code files for the entity ORM layer and business layer. Generating fully functional and error-free code with the code generator is a relatively easy thing to do as long as your database conforms to the following naming conventions.
Table Naming Guidelines
Prefixes
All tables should be prefixed with the one of the following values according to its contents:
- “tbl_” – Tables that contain data that will be modified.
- “ref_” – Tables that contain data for reference purposes only. Not typically for use with tables that will need updates, inserts, deletes.
- “rel_” – Tables that contain data about relationships to other tables (typically used for many-to-many relationships). This prefix is best followed with the name of the Parent table and Child table names without any prefixes or suffixes. (e.g. rel_OrderProduct)
- “sys_” – Tables that are not referenced by the application code. For use by database specific jobs and processes only.
Table Name
After the prefix, choose a word, in singular lemmatized form, that best describes the data contained in the table. Examples:
- “tbl_Order” – A table that contains order data that can be modified.
- “tbl_Customer” – A table that contains customer data that can be modified.
- “ref_ProductType” – A table that contains product type data for reference purposes.
- “rel_CustomerOrder” – A table that contains a many-to-many relationship based on primary and foreign key constraints of related tables.
Suffixes
There are no recommended table suffixes for the code generator at this time. So if you need a suffix, go for it. Note, however, that the code generator will only trim out prefixes. Names with suffixes separated by underscores will not be trimmed.
Column Naming Guidelines
Prefixes
All columns should be prefixed according to the following rules:
- All lowercase.
- Should not be too long. Use your judgment. It’s a prefix!
- Must represent an existing table in the form of an abbreviation or an acronym.
- Be consistent. Use the same prefix for all tables, columns, and stored procedures across the database. This rule goes for foreign keys as well.
- Even for the current table, you should have a prefix that is consistent across all database objects.
- End all prefixes with an underscore to delimit the prefix with the column name. (e.g. “cust_”)
Column Name
All columns should have a unique name. After the prefix, choose words, in singular lemmatized form, that best describes the data contained in the column. Note: The name that follows the prefix should also be unique from other post-prefix names (e.g. “cust_Id” and “pref_Id” would cause the code generator to create two public “Id” properties). Examples:
- “cust_Id” – Primary key for the customer table.
- “pref_PreferenceId” – Foreign key column in the tbl_Customer table to the tbl_Preference table.
- “cust_Name” – Name column in the tbl_Customer table.
Suffixes
We currently do not recommend the use of suffixes for column names at this time. So if you need a suffix, go for it. Note, however, that the code generator will only trim out prefixes. Names with suffixes separated by underscores will not be trimmed.
Stored Procedure Naming Guidelines
Prefixes
All stored procedures should be prefixed according to the following rules:
- All lowercase.
- Should not be too long. Again, use your judgment. It’s a prefix!
- Be consistent. Use the same prefix for all application specific stored procedures across the database.
- End all prefixes with an underscore to delimit the prefix with the stored procedure name. (e.g. “usp_”, “proc_”, “sync_”)
Stored Procedure Name
All stored procedures should have a unique name. After the prefix, choose words, in singular lemmatized form, that best describes the table(s) that the procedure will operate on. Examples:
- “usp_Tenant_Get” – Gets a single record from the Tenant table.
- “usp_TenantView_Get” – Gets multiple records from the Tenant table.
- “sync_TenantImportStep01” – The first of a series of import stored procedures for the Tenant table.
Suffixes
All stored procedures should be suffixed with the one of the following values according to its function:
- “_Get” – Represents a query stored procedure that returns either a set of records or a single record.
- “_Insert” – Represents a non-query stored procedure that inserts a single record.
- “_Update” – Represents a non-query stored procedure that updates a single record.
- “_Delete” – Represents a non-query stored procedure that deletes a single record.
Saturday November 10, 2007 at 10:26 am
hi thanks