?
Solved

TSQL script to create foriegn key

Posted on 2011-03-15
5
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 28

Assisted Solution

by:Ryan McCauley
Ryan McCauley earned 1332 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 668 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 1332 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.​
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

801 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