Solved

TSQL script to create foriegn key

Posted on 2011-03-15
5
328 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

860 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