We help IT Professionals succeed at work.

Create foreign key in sql table

Hi,

I have created this table :

CREATE TABLE [dbo].[tblGRNote](
      [PKGRNoteID] [int] IDENTITY(1,1) NOT NULL,
      [DateIn] [datetime] NULL,
      [SiteID] [nchar](8) NULL,
      [EngineerID] [nchar](6) NULL,
      [CallOutNo] [int] NULL,
      [ACCOUNT_REF] [nvarchar](8) NULL,
      [SupplierName] [nvarchar](60) NULL,
      [OrderNo] [int] NULL
)

I want to create a Foreign Key field SiteID. How do I do this?

Can I do it within Microsoft Management Studion 2008?
Comment
Watch Question

Senior Developer
Commented:
Maybe by defining the source?

E.g.

ALTER TABLE [dbo].[tblGRNote] FOREIGN KEY ( SiteID ) REFERENCES sourcetable ( column );

Open in new window

Mike EghtebasDatabase and Application Developer

Commented:
ALTER TABLE tblGRNote
 ADD CONSTRAINT FK_tblGRNote_SiteID FOREIGN KEY (SiteID) REFERENCES tblSite (SiteID)

Assuming tblSite is your other table with PK of SiteID

Revised...
Vitor MontalvãoIT Engineer
Distinguished Expert 2017

Commented:
CMChalcraft, do you need more help on this question?
CMChalcraftFinance Director

Author

Commented:
No thanks sorted for now.