Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
mridang_agarwal
Starting Member
22 Posts |
Posted - 2006-11-11 : 03:07:26
|
| Hey guys,Sorry to bug you once again! I got a small query but I cant figure out the solution myself:I have two Tables called Parent and Child. CREATE TABLE [dbo].[Parent]( [ParentID] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, CONSTRAINT [PK_Parent] PRIMARY KEY CLUSTERED ( [ParentID] ASC) ON [PRIMARY]) ON [PRIMARY]CREATE TABLE [dbo].[Child]( [ChildID] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [ParentID] [int] NOT NULL, CONSTRAINT [PK_Child] PRIMARY KEY CLUSTERED ( [ChildID] ASC) ON [PRIMARY]) ON [PRIMARY]ALTER TABLE [dbo].[Child] WITH CHECK ADD CONSTRAINT [FK_Child_Parent] FOREIGN KEY([ParentID])REFERENCES [dbo].[Parent] ([ParentID])As you can see theres a foreign key relationship between the child and parent table. Now when I try to delete a record from the parent table i get a foreign key error. Its quite obvious this is due to the foreign key relationship with the child table.How do i delete a particular parent record and the corresponding child records? Could someone type the query please? Thanks guys! |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-11-11 : 03:34:51
|
| You need to delete the child first, then you will be allowed to delete the parentFor example:DELETE Child WHERE ParentID = 1234DELETE Parent WHERE ParentID = 1234should be fine, but it Child records exist then:DELETE Parent WHERE ParentID = 1234on its own would raise an error.Kristen |
 |
|
|
mridang_agarwal
Starting Member
22 Posts |
Posted - 2006-11-11 : 03:56:09
|
| got it.. thanks man! |
 |
|
|
|
|
|