Eric Smith
Eric Smith

Reputation: 37

SQL Server 2012 Constraint

How to implement this constraint?

I have a column LastModifiedBy (datatype Int) for which I want to restrict the data from UserTable.UserId and also allow null.

How to implement this in SQL Server 2012?

Thank you, Eric

Upvotes: 0

Views: 55

Answers (1)

Joe Taras
Joe Taras

Reputation: 15399

Try this:

ALTER TABLE [dbo].[yourtable] WITH NOCHECK
ADD CONSTRAINT [usertable$FK_usertable_1]
FOREIGN KEY([LastModifiedBy]) REFERENCES [dbo].[UserTable] ([UserId])

This script alter your table (where you have LastModifiedBy) adding a constraint of foreing key type to point UserTable

Upvotes: 3

Related Questions