Solved

TSQL script to create foriegn key

Posted on 2011-03-15
5
324 Views
Last Modified: 2012-06-27
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
Comment
Question by:soozh
  • 3
5 Comments
 
LVL 28

Assisted Solution

by:Ryan McCauley
Ryan McCauley earned 333 total points
ID: 35139403
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
 
LVL 11

Accepted Solution

by:
JoeNuvo earned 167 total points
ID: 35139472
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
 
LVL 28

Expert Comment

by:Ryan McCauley
ID: 35139653
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
 

Author Comment

by:soozh
ID: 35140908
i have discovered there already is a foriegn key so how do i just add the cascade delete?
0
 
LVL 28

Assisted Solution

by:Ryan McCauley
Ryan McCauley earned 333 total points
ID: 35142062
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now