How to create a foreign key in MsSql

Scenario : We have two tables 1 Customers 2 Sales

I have to make sure that all the client id's that are inserted in the Sales table exists in the Client table

Solution  : Add a foreign key

ALTER TABLE dbo.Sales
ADD CONSTRAINT FK_Client_ClientId FOREIGN KEY (ClientId) REFERENCES dbo.Client (ClientId)

Lucian Leonte

MCT, MCITP, MCTS

Powered by CodeReview – Let's make it Better!