Skip to content

Commit 2cad55c

Browse files
committed
Add useful information to name convention
1 parent 0701e7c commit 2cad55c

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

SQL Server Name Convention and T-SQL Programming Style.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ Reasons for using a naming convention (as opposed to allowing programmers to cho
3737
| [Disk-Based Wide Table - SPARSE Column] | U | PascalCase | 128 | No | No | `_SPR` | Yes | [A-z][0-9] | `MyTable_SPR` |
3838
| [Table Column] | | PascalCase | 128 | No | No | No | Yes | [A-z][0-9] | `MyColumn` |
3939
| [Table Column SPARSE] | | PascalCase | 128 | No | No | `_SPR` | Yes | [A-z][0-9] | `MyColumn_SPR` |
40-
| Table Default Values | D | PascalCase | 128 | No | `DF_` | No | Yes | [A-z][0-9] | `DF_MyTable_MyColumn` |
41-
| Table Check Column Constraint | C | PascalCase | 128 | No | `CK_` | No | Yes | [A-z][0-9] | `CK_MyTable_MyColumn` |
42-
| Table Check Table Constraint | C | PascalCase | 128 | No | `CTK_` | No | Yes | [A-z][0-9] | `CTK_MyTable_MyColumn_AnotherColumn` |
43-
| Table Primary Key | PK | PascalCase | 128 | No | `PK_` | No | Yes | [A-z][0-9] | `PK_MyTableID` |
44-
| Table Alternative Key | UQ | PascalCase | 128 | No | `AK_` | No | Yes | [A-z][0-9] | `AK_MyTable_MyColumn_AnotherColumn` |
45-
| Table Foreign Key | F | PascalCase | 128 | No | `FK_` | No | Yes | [A-z][0-9] | `FK_MyTable_ForeignTableID` |
46-
| Table Clustered Index | | PascalCase | 128 | No | `IXC` | No | Yes | [A-z][0-9] | `IXC_MyTable_MyColumn_AnotherColumn` |
47-
| Table Non Clustered Index | | PascalCase | 128 | No | `IX_` | No | Yes | [A-z][0-9] | `IX_MyTable_MyColumn_AnotherColumn` |
40+
| [Columns Check Constraint] | C | PascalCase | 128 | No | `CTK_` | No | Yes | [A-z][0-9] | `CTK_MyTable_MyColumn_AnotherColumn` |
41+
| [Column Check Constraint] | C | PascalCase | 128 | No | `CK_` | No | Yes | [A-z][0-9] | `CK_MyTable_MyColumn` |
42+
| [Column Default Values] | D | PascalCase | 128 | No | `DF_` | No | Yes | [A-z][0-9] | `DF_MyTable_MyColumn` |
43+
| [Table Primary Key] | PK | PascalCase | 128 | No | `PK_` | No | Yes | [A-z][0-9] | `PK_MyTableID` |
44+
| [Table Unique (Alternative) Key] | UQ | PascalCase | 128 | No | `AK_` | No | Yes | [A-z][0-9] | `AK_MyTable_MyColumn_AnotherColumn` |
45+
| [Table Foreign Key] | F | PascalCase | 128 | No | `FK_` | No | Yes | [A-z][0-9] | `FK_MyTable_ForeignTableID` |
46+
| [Table Clustered Index] | | PascalCase | 128 | No | `IXC` | No | Yes | [A-z][0-9] | `IXC_MyTable_MyColumn_AnotherColumn` |
47+
| [Table Non Clustered Index] | | PascalCase | 128 | No | `IX_` | No | Yes | [A-z][0-9] | `IX_MyTable_MyColumn_AnotherColumn` |
4848
| [DDL Trigger] | TR | PascalCase | 128 | No | `TR_` | `_DDL` | Yes | [A-z][0-9] | `TR_LogicalName_DDL` |
4949
| [DML Trigger] | TR | PascalCase | 128 | No | `TR_` | `_DML` | Yes | [A-z][0-9] | `TR_MyTable_LogicalName_DML` |
5050
| [Logon Trigger] | TR | PascalCase | 128 | No | `TR_` | `_LOG` | Yes | [A-z][0-9] | `TR_LogicalName_LOG` |
@@ -75,6 +75,14 @@ Reasons for using a naming convention (as opposed to allowing programmers to cho
7575
[Disk-Based Wide Table - SPARSE Column]:https://docs.microsoft.com/en-us/sql/relational-databases/tables/tables#wide-tables
7676
[Table Column]:https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql
7777
[Table Column SPARSE]:https://docs.microsoft.com/en-us/sql/relational-databases/tables/use-sparse-columns
78+
[Columns Check Constraint]:https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-check-constraints
79+
[Column Check Constraint]:https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-check-constraints
80+
[Column Default Values]:https://docs.microsoft.com/en-us/sql/relational-databases/tables/specify-default-values-for-columns
81+
[Table Primary Key]:https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-primary-keys
82+
[Table Unique (Alternative) Key]:https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-unique-constraints
83+
[Table Foreign Key]:https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-foreign-key-relationships
84+
[Table Clustered Index]:https://docs.microsoft.com/en-us/sql/relational-databases/indexes/clustered-and-nonclustered-indexes-described
85+
[Table Non Clustered Index]:https://docs.microsoft.com/en-us/sql/relational-databases/indexes/clustered-and-nonclustered-indexes-described
7886

7987
[DDL Trigger]:https://docs.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql
8088
[DML Trigger]:https://docs.microsoft.com/en-us/sql/relational-databases/triggers/dml-triggers
@@ -189,8 +197,10 @@ SQL Server T-SQL Coding Conventions, Best Practices, and Programming Guidelines.
189197
- Avoid using asterisk in select statements `SELECT *`, use explicit column names.
190198
More details [here](https://www.red-gate.com/hub/product-learning/sql-prompt/finding-code-smells-using-sql-prompt-asterisk-select-list).
191199
- No square brackets `[]` and [reserved words](https://github.com/ktaranov/sqlserver-kit/blob/master/Scripts/Check_Reserved_Words_For_Object_Names.sql) in object names and alias, use only Latin symbols **`[A-z]`** and numeric **`[0-9]`**.
192-
- Prefer [ANSI syntax](http://standards.iso.org/ittf/PubliclyAvailableStandards/c053681_ISO_IEC_9075-1_2011.zip) and functions (`CAST` instead `CONVERT`, `COALESE` instead `ISNULL`).
193-
- All finished expressions should have semicolon `;` at the end. This is ANSI standard and Microsoft announced with the SQL Server 2008 release that semicolon statement terminators will become mandatory in a future version so statement terminators other than semicolons (whitespace) are currently deprecated. This deprecation announcement means that you should always use semicolon terminators in new development.
200+
- Prefer [ANSI syntax](http://standards.iso.org/ittf/PubliclyAvailableStandards/c053681_ISO_IEC_9075-1_2011.zip) and functions ([`CAST`][10] instead [`CONVERT`][10], [`COALESE`](https://docs.microsoft.com/en-us/sql/t-sql/language-elements/coalesce-transact-sql) instead [`ISNULL`](https://docs.microsoft.com/en-us/sql/t-sql/functions/isnull-transact-sql), etc.).
201+
- All finished expressions should have semicolon `;` at the end.
202+
This is ANSI standard and Microsoft announced with the SQL Server 2008 release that semicolon statement terminators will become mandatory in a future version so statement terminators other than semicolons (whitespace) are currently deprecated.
203+
This deprecation announcement means that you should always use semicolon terminators in new development.
194204
More details [here](http://www.dbdelta.com/always-use-semicolon-statement-terminators/).
195205
- All script files should end with `GO` and line break.
196206
- Keywords should be in **UPPERCASE**: `SELECT`, `FROM`, `GROUP BY` etc.
@@ -199,7 +209,8 @@ SQL Server T-SQL Coding Conventions, Best Practices, and Programming Guidelines.
199209
- All system database and tables must be in **lowercase** for properly working for Case Sensitive instance: `master, sys.tables …`.
200210
- Avoid non-standard column aliases, use, if required, double-quotes for special characters and always `AS` keyword before alias:
201211
```sql
202-
SELECT p.LastName AS "Last Name"
212+
SELECT
213+
p.LastName AS "Last Name"
203214
FROM dbo.Person AS p;
204215
```
205216
More details [here](https://www.red-gate.com/hub/product-learning/sql-prompt/sql-prompt-code-analysis-avoid-non-standard-column-aliases).
@@ -487,7 +498,7 @@ More details [here](http://www.sqlservertutorial.net/sql-server-stored-procedure
487498
/* Bad */
488499
DECLARE @tsql nvarchar(max);
489500
DECLARE @id int = 2107154552;
490-
SET @tsql = N'SELECT object_id, "name" FROM master.sys.tables WHERE object_id = ' + CONVERT(nvarchar(max), @id);
501+
SET @tsql = N'SELECT object_id, "name" FROM master.sys.tables WHERE object_id = ' + CAST(@id AS nvarchar(max));
491502
EXEC sp_executesql @tsql;
492503
493504
/* Good */
@@ -567,3 +578,4 @@ More details [here](http://www.sqlservertutorial.net/sql-server-stored-procedure
567578

568579
[`sp_executesql`]:https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-executesql-transact-sql
569580
[`EXEC`]:https://docs.microsoft.com/en-us/sql/t-sql/language-elements/execute-transact-sql
581+
[10]:https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql

0 commit comments

Comments
 (0)