Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/4907fa77ffd698d902e4 to your computer and use it in GitHub Desktop.
Save bjoerntx/4907fa77ffd698d902e4 to your computer and use it in GitHub Desktop.
SQL to create table
CREATE TABLE [dbo].[Categories] (
[CategoryId] [int] NOT NULL IDENTITY,
[Name] [nvarchar](max),
CONSTRAINT [PK_dbo.Categories] PRIMARY KEY ([CategoryId])
)
CREATE TABLE [dbo].[Products] (
[ProductId] [int] NOT NULL IDENTITY,
[Name] [nvarchar](max),
[CategoryId] [int] NOT NULL,
CONSTRAINT [PK_dbo.Products] PRIMARY KEY ([ProductId])
)
CREATE INDEX [IX_CategoryId] ON [dbo].[Products]([CategoryId])
ALTER TABLE [dbo].[Products] ADD CONSTRAINT [FK_dbo.Products_dbo.Categories_CategoryId] FOREIGN KEY ([CategoryId]) REFERENCES [dbo].[Categories] ([CategoryId]) ON DELETE CASCADE
SET IDENTITY_INSERT [dbo].[Categories] ON
INSERT INTO [dbo].[Categories] ([CategoryId], [Name]) VALUES (1, N'Category 1')
INSERT INTO [dbo].[Categories] ([CategoryId], [Name]) VALUES (2, N'Category 2')
INSERT INTO [dbo].[Categories] ([CategoryId], [Name]) VALUES (3, N'Category 3')
SET IDENTITY_INSERT [dbo].[Categories] OFF
SET IDENTITY_INSERT [dbo].[Products] ON
INSERT INTO [dbo].[Products] ([ProductId], [Name], [CategoryId]) VALUES (2, N'Product 1', 1)
INSERT INTO [dbo].[Products] ([ProductId], [Name], [CategoryId]) VALUES (3, N'Product 2', 2)
INSERT INTO [dbo].[Products] ([ProductId], [Name], [CategoryId]) VALUES (4, N'Product 3', 1)
SET IDENTITY_INSERT [dbo].[Products] OFF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment