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.
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
I personally don't like to prefix a stored procedure name with sp_ - it is redundant, IMO. Instead, I like to prefix them with a "unit of functionality" identifier. e.g. I'll call the sprocs to deal with orders order_Save, order_GetById, order_GetByCustomer, etc. It keeps them all logically grouped in management studio and makes it harder to pick the wrong one. (GetOrderByProduct, GetCustomerById, etc...)
Of course, it is personal preference, other people may prefer to have all the Get sprocs together, all the Save ones, etc.
Just my 2c.
Data Types to be used: We should use only following data types:
Give default value for BIT datatype. It should not be nullable Table and Column names should never be in lower case. It should describe its purpose. Avoid using short forms. While creating a table FK and PK to be well thought and defined. Variables names should start with one/two letter indicating its data type in lower case. For e.g. INT variable should begin with i. Name should be descriptive and short forms should be avoided. It each word should start with capital letter followed by all small letters.
E.g.
Correct way: – iTotalCount
Incorrect way: – xyz
Table columns used with “WHERE” clause inside stored procedures should be indexed/keyed. This will increase the speed of data processing. Ordering of parameters in WHERE clause should be done properly. Primary key/index should precede bit variables. E.g.:- An index is created on combination of columns (REF_ID, T_TYPE_STR, CNUMBER, TLOG_ID)
— Correct way where Indexed keys are used in sequence in ‘WHERE’ clause
While writing a stored procedure we should have description section at the beginning which will contain Author:
Creation date:
Description:
If any sp is modified this section should be appended with
Modified by:
Modified on:
Description:
ROW_INSERTION_DATE_TIME AND ROW_UPDATION_DATE_TIME column should have default values as GETDATE().
More at : http://www.writeulearn.com/sql-database-coding-standards/
Wouldn't call it coding standard - more like coding style
I know this is long, but bear with me, it's important. This question opened a cool can of worms. And if you don't like database blocks, read on.
And, before anyone thinks about knocking down my response, please see the following article and connected articles to it about locking, and recompiles; two of the most damaging resources hits on a SQL database.
http://support.microsoft.com/kb/263889
I can type pretty quickly, and I don't like to type any more than the next person. But the points below I follow extremely closely, even if it is more typing. So much that I've built my own SP apps to do it for me.
The points I bring up are really important! You might even say to yourself, "are you kidding, that's not an issue", well, then you didn't read the articles above. AND, it's totally moronic that M$ would put these points in as NOTEs. These issues to me should be BOLD and SCREAMING.
I also do a lot of coding to build my basic scripts using C# applications to speed up development and these practices are very sound (10 years worth) to make scripting SPs easier and especially faster.
There are more than this, but this is what I do for the first 60% of everything.
Best practices
Preferences
Select
Update
Insert
OR
Delete
Play around with www.sqlinform.com - I recommend using the ANSI-92 standard, and then pretty it up with that site.