Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What SQL coding standard do you follow? [closed]

Is there any widely used SQL coding standard out there? SQL is little bit different from C/C++ type of programming languages. Really don't know how to best format it for readability.

like image 900
thebat Avatar asked Feb 06 '09 21:02

thebat


People also ask

What coding does SQL use?

SQL is a fourth-generation language, meaning it is a scripting language that does not require compiling to run. Like most fourth-generation languages, SQL requires an interpreter that translates rather than compiles code. As with all languages, SQL has rules for issuing commands and queries.

What is proper SQL formatting?

If the name is the same as an SQL keyword, enclose the name within quotation marks. The name of an object in a database for a table or a column should be unique and not too long. Avoid special characters in the name like $, &, * , etc. (use only letters, numbers, and underscores).


2 Answers

Wouldn't call it coding standard - more like coding style

SELECT     T1.col1,     T1.col2,     T2.col3 FROM     table1 T1     INNER JOIN ON Table2 T2 ON T1.ID = T2.ID WHERE     T1.col1 = 'xxx'     AND T2.Col3 = 'yyy' 
  • capitalize reserved words
  • main keywords on new line
  • can't get used to commas before columns
  • always use short meaningful table aliases
  • prefix views with v
  • prefix stored procs with sp (however don't use "sp_" which is reserved for built in procs)
  • don't prefix tables
  • table names singular
like image 80
DJ. Avatar answered Sep 23 '22 22:09

DJ.


I like the comma preceding way:

SELECT       column1     , column2     , column3     , COALESCE(column4,'foo') column4 FROM     tablename WHERE     column1 = 'bar' ORDER BY        column1     , column2 

it makes it the easiest to read and debug in my opinion.

like image 40
Ryan Guill Avatar answered Sep 25 '22 22:09

Ryan Guill