Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a Foreign Key Contraint

Status
Not open for further replies.

fixthebug2003

Programmer
Oct 20, 2003
294
US
Hi
How can I create a Foreign Key Contraint using SQL Enterprise Manager. I went to the table properties and picked the Relationships tab..but all of them are disabled..
I am using SQL Server 7

Thanks
Fixthebug2003
 
Here is a basic SQL script to create two tables and then add a foreign key constraint to the second table.

Code:
CREATE TABLE Alpha (
	ID int IDENTITY (1,1) NOT NULL,
	[Data] varchar(20) NULL
)
GO

CREATE TABLE Omega (
	R_ID int NOT NULL,
	Data varchar(20) NULL
)
GO

ALTER TABLE Alpha ADD CONSTRAINT
	PK_Alpha PRIMARY KEY CLUSTERED (ID)
GO

ALTER TABLE Omega ADD CONSTRAINT
	FK_Alpha_Omega FOREIGN KEY (R_ID) REFERENCES Alpha (ID)
GO

One reason that you may not be able to create any foreign key constraints is that there must be a primary key enabled on the base table before any foreign keys can be created.

Regards,

hmscott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top