Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Foreign Keys and Deleting
Old 05-11-2008, 06:09 AM Foreign Keys and Deleting
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
Hi Guys

Was hoping someone could help me out with a wee problem im having...

I have two tables: one called "fixtures" and one called "teams". The "fixtures" table has 2 fields called "homeTeamID" and "awayTeamID" which are set as foreign keys to the "teamID" field within the "teams" table.

Now i would like to delete an entry from the "fixtures" table but I am getting the error "Cannot delete or update a parent row: a foreign key constraint fails". As i understand it i should be able to delete a row from the fixtures table without deleting anything from the teams table, but i dont seem to be allowed.

Is it the way i have set up the foreign keys? or is this normal?

The lines i used for creating the foreign keys are:

Code:
  ADD CONSTRAINT `fixtures_ibfk_2` FOREIGN KEY (`homeTeamID`) REFERENCES `team` (`teamID`),
  ADD CONSTRAINT `fixtures_ibfk_3` FOREIGN KEY (`awayTeamID`) REFERENCES `team` (`teamID`);
Cheers
Stoot
stoot98 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-11-2008, 07:04 PM Re: Foreign Keys and Deleting
vividearth's Avatar
Experienced Talker

Posts: 33
Location: London, UK
Trades: 0
Hi Stoot

Do you have the full DML for the tables as I do not believe what you have outlined above would cause the problem you are having.

For instance this should mock what you have said but works fine:

CREATETABLE [dbo].[teams](
[teamID] [int] IDENTITY(1,1)NOTNULL,
[name] [nvarchar](50)COLLATE Latin1_General_CI_AS NOTNULL,
CONSTRAINT [PK_teams] PRIMARYKEYCLUSTERED(
[teamID] ASC
)WITH(PAD_INDEX =OFF, IGNORE_DUP_KEY =OFF)ON [PRIMARY]
)ON [PRIMARY]
GO
CREATETABLE [dbo].[fixtures](
[id] [int] IDENTITY(1,1)NOTNULL,
[homeTeamID] [int] NOTNULL,
[awayTeamID] [int] NOTNULL,
CONSTRAINT [PK_fixtures] PRIMARYKEYCLUSTERED(
[id] ASC
)WITH(PAD_INDEX =OFF, IGNORE_DUP_KEY =OFF)ON [PRIMARY]
)ON [PRIMARY]
GO
ALTERTABLE [dbo].[fixtures] WITHCHECKADDCONSTRAINT [FK_fixtures_awayteams] FOREIGNKEY([awayTeamID])
REFERENCES [dbo].[teams] ([teamID])
GO
ALTERTABLE [dbo].[fixtures] CHECKCONSTRAINT [FK_fixtures_awayteams]
GO
ALTERTABLE [dbo].[fixtures] WITHCHECKADDCONSTRAINT [FK_fixtures_hometeams] FOREIGNKEY([homeTeamID])
REFERENCES [dbo].[teams] ([teamID])
GO
ALTERTABLE [dbo].[fixtures] CHECKCONSTRAINT [FK_fixtures_hometeams]
INSERTINTO [dbo].[teams] ([Name])VALUES('Team A')
INSERTINTO [dbo].[teams] ([Name])VALUES('Team B')
INSERTINTO [dbo].[fixtures] (homeTeamID, awayTeamID)VALUES(1, 2)
INSERTINTO [dbo].[fixtures] (homeTeamID, awayTeamID)VALUES(2, 1)
DELETEFROM [dbo].[fixtures] WHERE ID = 1

v
vividearth is offline
Reply With Quote
View Public Profile Visit vividearth's homepage!
 
Old 05-11-2008, 07:05 PM Re: Foreign Keys and Deleting
vividearth's Avatar
Experienced Talker

Posts: 33
Location: London, UK
Trades: 0
Sorry DDL not DML
vividearth is offline
Reply With Quote
View Public Profile Visit vividearth's homepage!
 
Reply     « Reply to Foreign Keys and Deleting
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.66836 seconds with 12 queries