Skip to content

Instantly share code, notes, and snippets.

@theorigin
Created July 30, 2013 10:17
Show Gist options
  • Save theorigin/6111805 to your computer and use it in GitHub Desktop.
Save theorigin/6111805 to your computer and use it in GitHub Desktop.
SQL Server naming conventions
-- Tables
-- Basically the table name (singular)
CREATE TABLE [dbo].[Product] (...)
-- Columns
-- Identity
[Id] INT NOT NULL IDENTITY (1, 1) -- Id column with auto increment
[Id] UNIQUEIDENTIFIER NOT NULL -- Id based on GUID
-- Reference another table/column
-- [ForeignTableName_ColumnInForeignTable]
-- underscore indicates table_column split
[ProductStatus_Id] INT -- Means Id column in ProductStatus table
-- If you've got two columns that are foreigned keyed
[Current_ProductStatus_Id] INT
[Previous_ProductStatus_Id] INT
-- Common columns
[CreatedBy] NVARCHAR(50) NOT NULL,
[CreatedDate] DATETIME NOT NULL,
[LastUpdatedBy] NVARCHAR(50) NOT NULL,
[LastUpdatedDate] DATETIME NOT NULL,
[Version] TIMESTAMP,
-- Constraint names
-- primary key
-- format is [PK_Table name]
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED ([Id] ASC)
-- foreign key
-- format is [FK_OwningTable_ColumnName_FKTable_FKColumn]
CONSTRAINT [FK_Product_Current_ProductStatus_Id_ProductStatus_Id] FOREIGN KEY ([ProductStatus_Id]) REFERENCES [dbo].[ProductStatus] ([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION
-- Indexes
CREATE INDEX [IX_OrderLine_OrderHeader_Id] ON [dbo].[OrderLine] ([OrderHeader_Id]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment