[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

TSQL script to create foriegn key

I have two tables.  Trauma and PatientInjuries.  The CREATE statments for them are below.

Now i want to create a relationship between these tables using the column in tra_id.  In the table PatientInjuries the column tra_id is a foriegn key into the Trauma table.

Can someone write me the TSQL script that creates this relationship, and also will cascade deletes down from Trauma into PatientInjuries?

I am using SQL Server 2008.

Thanks.
CREATE TABLE [dbo].[Trauma](
	[tra_id] [int] IDENTITY(1,1) NOT NULL,
	[pat_id] [int] NOT NULL,
	[kli_KlinikNr] [int] NOT NULL,
     ........
	[tra_t_alarm] [datetime] NULL,
	[tra_NISS] [int] NULL,
 CONSTRAINT [PK_Trauma] PRIMARY KEY CLUSTERED 
(
	[tra_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[PatientInjuries](
	[pij_id] [int] IDENTITY(1,1) NOT NULL,
	[tra_id] [int] NOT NULL,
	[pat_PatientId] [nvarchar](14) NOT NULL,
	[reg_Region] [int] NOT NULL,
	[reg_Side] [char](1) NULL,
	[ais_AISCode] [nvarchar](10) NOT NULL,
	[icd_ICD] [nvarchar](10) NOT NULL,
	[ais_BodyRegion] [int] NOT NULL,
	[pat_Id] [int] NULL
) ON [PRIMARY]

Open in new window

0
soozh
Asked:
soozh
  • 3
3 Solutions
 
Ryan McCauleyCommented:
The syntax for what you want is:

ALTER TABLE dbo.PatientInjuries
ADD CONSTRAINT fk_PatientInjuries_Patients_traid FOREIGN KEY tra_id
REFERENCES dbo.Patients.tra_id
ON DELETE CASCADE

Open in new window


Let me know if you have any problems!
0
 
JoeNuvoCommented:
I think reference should be Truma table?


ALTER TABLE dbo.PatientInjuries
ADD CONSTRAINT FK_PatientInjuries_Trauma FOREIGN KEY ( tra_id )
REFERENCES dbo.Trauma ( tra_id ) ON DELETE CASCADE 

Open in new window

0
 
Ryan McCauleyCommented:
Snap, you're right - Too many questions open at once :)

In any case, I missed the parenthesis as well, and you've got the correct syntax.

0
 
soozhAuthor Commented:
i have discovered there already is a foriegn key so how do i just add the cascade delete?
0
 
Ryan McCauleyCommented:
I don't believe you can modify an existing constraint - you have to drop the constraint that's already there and re-create it with the ON DELETE CASCADE clause. Dropping the constraint won't cause any problems - the data will be unaffected.

You also have the option, if you'd prefer, or creating the new Foreign Key constraint with a new name, and then dropping the old one once the new one is in place. That prevents any data from being inserted in the meantime that doesn't comply with the constraint.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now