-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Add SQL development guidelines and best practices to instructions #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| --- | ||
| description: 'Guidelines for generating SQL statements and stored procedures' | ||
| applyTo: '**/*.sql' | ||
| --- | ||
|
|
||
| # SQL Development | ||
|
|
||
| ## Database schema generation | ||
| - all table names should be in singular form | ||
| - all column names should be in singular form | ||
| - all tables should have a primary key column named `id` | ||
| - all tables should have a column named `created_at` to store the creation timestamp | ||
| - all tables should have a column named `updated_at` to store the last update timestamp | ||
|
|
||
| ## Database schema design | ||
| - all tables should have a primary key constraint | ||
| - all foreign key constraints should have a name | ||
| - all foreign key constraints should be defined inline | ||
| - all foreign key constraints should have `ON DELETE CASCADE` option | ||
| - all foreign key constraints should have `ON UPDATE CASCADE` option | ||
| - all foreign key constraints should reference the primary key of the parent table | ||
|
|
||
| ## SQL Coding Style | ||
| - use uppercase for SQL keywords (SELECT, FROM, WHERE) | ||
| - use consistent indentation for nested queries and conditions | ||
| - include comments to explain complex logic | ||
| - break long queries into multiple lines for readability | ||
| - organize clauses consistently (SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY) | ||
|
|
||
| ## SQL Query Structure | ||
| - use explicit column names in SELECT statements instead of SELECT * | ||
| - qualify column names with table name or alias when using multiple tables | ||
| - limit the use of subqueries when joins can be used instead | ||
| - include LIMIT/TOP clauses to restrict result sets | ||
| - use appropriate indexing for frequently queried columns | ||
| - avoid using functions on indexed columns in WHERE clauses | ||
|
|
||
| ## Stored Procedure Naming Conventions | ||
| - prefix stored procedure names with 'sp_' | ||
| - use PascalCase for stored procedure names | ||
| - use descriptive names that indicate purpose (e.g., sp_GetCustomerOrders) | ||
| - include plural noun when returning multiple records (e.g., sp_GetProducts) | ||
| - include singular noun when returning single record (e.g., sp_GetProduct) | ||
|
|
||
| ## Parameter Handling | ||
| - prefix parameters with '@' | ||
| - use camelCase for parameter names | ||
| - provide default values for optional parameters | ||
| - validate parameter values before use | ||
| - document parameters with comments | ||
| - arrange parameters consistently (required first, optional later) | ||
|
|
||
|
|
||
| ## Stored Procedure Structure | ||
| - include header comment block with description, parameters, and return values | ||
| - return standardized error codes/messages | ||
| - return result sets with consistent column order | ||
| - use OUTPUT parameters for returning status information | ||
| - prefix temporary tables with 'tmp_' | ||
|
|
||
|
|
||
| ## SQL Security Best Practices | ||
| - parameterize all queries to prevent SQL injection | ||
| - use prepared statements when executing dynamic SQL | ||
| - avoid embedding credentials in SQL scripts | ||
| - implement proper error handling without exposing system details | ||
| - avoid using dynamic SQL within stored procedures | ||
|
|
||
| ## Transaction Management | ||
| - explicitly begin and commit transactions | ||
| - use appropriate isolation levels based on requirements | ||
| - avoid long-running transactions that lock tables | ||
| - use batch processing for large data operations | ||
| - include SET NOCOUNT ON for stored procedures that modify data | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.