Wednesday, February 15, 2012

Erroon trying take make a relation between 2 tables

I want a PK-FK relation between my tables:

Products (PK = ProductID)
Orders = (FK = ProductID)

How can I do this in SQL 2005?

Just add this to your query window to create table ,this is just a sample syntax.U can change it according to your need.

ALTER TABLE [dbo].[Products] WITH CHECK ADD CONSTRAINT [PK_ProductID] PRIMARY KEY([ProductID])

(the above synatax would make ProductID as primary key in your Products Table)

ALTER TABLE [dbo].[Orders] WITH CHECK ADD CONSTRAINT [FK_ProductID] FOREIGN KEY([ProductID])
REFERENCES [dbo].[Products] ([ProductID])

(this syntax woud create PK-FK relation between your Product and Orders Tables)

No comments:

Post a Comment