Solved

TSQL script to create foriegn key

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

22 Experts available now in Live!

Get 1:1 Help Now