Skip to content

Instantly share code, notes, and snippets.

@tekiegirl
Created March 24, 2016 14:36
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 tekiegirl/7018182e92f0e5ce53b2 to your computer and use it in GitHub Desktop.
Save tekiegirl/7018182e92f0e5ce53b2 to your computer and use it in GitHub Desktop.
Adding a relationship constraint to an existing or new column in MS SQL
ALTER TABLE [dbo].[MyTable]
ADD CONSTRAINT FK_MyTable_OtherTable FOREIGN KEY (OType)
REFERENCES [dbo].[OtherTable] (Ident)
ON DELETE CASCADE
ON UPDATE CASCADE
CREATE TABLE [dbo].[MyTable] (Ident int NOT NULL, Name nvarchar(50), OType int
CONSTRAINT PK_MyTable PRIMARY KEY NONCLUSTERED (Ident),
CONSTRAINT FK_MyTable_OtherTable FOREIGN KEY (OType)
REFERENCES [dbo].[OtherTable] (Ident)
ON DELETE CASCADE
ON UPDATE CASCADE
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment