With regards to writing good SQL code, there are a number of SQL naming conventions as well as best practices which should be followed.
Use all lowercase for column and table names and to separate words with an underscore (“_”). For example,
end_date as column names as opposed to
employees as a table name as opposed to
Employees. This works together with the good SQL style convention of capitalizing statements names, clauses, and other keywords for better code readability:
SELECT start_date, end_date FROM campaign;
For all SQL code and data, using table, column, variable names, or aliases which conflict with SQL keywords and reserved words should be avoided. Tables and columns should be given clear, easily understandable and uniquely identifying names. Abbreviations should not be used unless absolutely necessary as required by the maximum length enforced for column and table names – in which case it is likely that a different, more succinct name should be considered.
While some older style guides suggest it, the general consensus amongst more modern style guides is to avoid table and column names with prefixes or suffixes denoting the data or entity type or are a short abbreviation. For example, a table name of
employees is preferable to
tbl_employees, and a column name like
customer_account_tx is ambiguous, as it is unclear whether
tx is an abbreviation for “transaction” or designating that this column is a text field.
While not a hard-and-fast rule, many style guides suggest only using singular nouns for table names for clarity; e.g.
customer as opposed to
There are also a number of conventions which are enforced by the SQL engine. Though these vary somewhat between SQL “flavors”, SQL columns and table names should begin with a letter, not end in an underscore, and should contain only alphanumeric characters. Column and table names should not contain spaces. For some varieties of SQL, these rules must be followed explicitly or code will confuse the SQL interpreter and not execute.
Learn SQL Today
Get hands-on experience writing code with interactive tutorials in our free online learning platform.
- Free and fun
- Designed for beginners
- No downloads or setup required