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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 How to add a constraint back in after I dropped it

Author  Topic 

dba123
Yak Posting Veteran

90 Posts

Posted - 2007-03-12 : 12:07:39
I took a look at the table script for my PurchaseDownload table.

I'm trying to do this which works, but I need to add back in that constraint but not sure of the right syntax to do so.

First, what I'm performing is this so far:

ALTER TABLE PurchaseDownload
DROP CONSTRAINT [FK_PurchaseDownload_ProductFile]

delete from ProductFile
where ProductID in (
select f.ProductID from ProductFile f
inner join product p ON p.ProductID = f.ProductID
inner join SellerStore s ON s.SellerStoreID = p.SellerStoreID
where s.SellerStoreID = 1000
and FileName like '%zip%')

-- I need to readd that constraint or check back in here to restore it

Here's the PurchaseDownload table create script currently:

USE [MyDB]
GO
/****** Object: Table [dbo].[PurchaseDownload] Script Date: 03/12/2007 10:13:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[PurchaseDownload](
[PurchaseDownloadID] [bigint] IDENTITY(1,1) NOT NULL,
[PurchaseID] [bigint] NOT NULL,
[DownloadDate] [datetime] NOT NULL,
[IPAddress] [varchar](15) NOT NULL,
[BrowserInfo] [varchar](100) NOT NULL,
[ProductSourceStatusID] [int] NULL,
[CreateDate] [datetime] NOT NULL CONSTRAINT [DF_PurchaseDownload_CreateDate] DEFAULT (getdate()),
[CreateUserID] [int] NOT NULL,
[UpdateDate] [datetime] NULL,
[UpdateUserID] [int] NULL,
[Active] [bit] NOT NULL DEFAULT (1),
[ProductFileID] [bigint] NULL,
CONSTRAINT [PK_PurchaseDownload] PRIMARY KEY CLUSTERED
(
[PurchaseDownloadID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[PurchaseDownload] WITH CHECK ADD CONSTRAINT [FK_PurchaseDownload_ProductFile] FOREIGN KEY([ProductFileID])
REFERENCES [dbo].[ProductFile] ([ProductFileID])
GO
ALTER TABLE [dbo].[PurchaseDownload] CHECK CONSTRAINT [FK_PurchaseDownload_ProductFile]
GO
ALTER TABLE [dbo].[PurchaseDownload] WITH CHECK ADD CONSTRAINT [FK_PurchaseDownload_ProductSourceStatus] FOREIGN KEY([ProductSourceStatusID])
REFERENCES [dbo].[ProductSourceStatus] ([ProductSourceStatusID])
GO
ALTER TABLE [dbo].[PurchaseDownload] CHECK CONSTRAINT [FK_PurchaseDownload_ProductSourceStatus]
GO
ALTER TABLE [dbo].[PurchaseDownload] WITH CHECK ADD CONSTRAINT [FK_PurchaseDownload_Purchase] FOREIGN KEY([PurchaseID])
REFERENCES [dbo].[Purchase] ([PurchaseID])
GO
ALTER TABLE [dbo].[PurchaseDownload] CHECK CONSTRAINT [FK_PurchaseDownload_Purchase]

how can I add that constraint that I dropped back in?

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-12 : 12:14:04
[code]ALTER TABLE PurchaseDownload
ADD CONSTRAINT FK_PurchaseDownload_ProductFile
FOREIGN KEY (COL1) REFERENCES TABLE2(COL2)
[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

dba123
Yak Posting Veteran

90 Posts

Posted - 2007-03-12 : 12:18:18
so what do I put in for Col1 and Col2?
Go to Top of Page

dba123
Yak Posting Veteran

90 Posts

Posted - 2007-03-12 : 12:19:16
ALTER TABLE PurchaseDownload
ADD CONSTRAINT [FK_PurchaseDownload_ProductFile] FOREIGN KEY([ProductFileID])
REFERENCES [dbo].[ProductFile] ([ProductFileID])

error: ALTER TABLE statement conflicted with COLUMN FOREIGN KEY constraint 'FK_PurchaseDownload_ProductFile'. The conflict occurred in database 'MyDB', table 'ProductFile', column 'ProductFileID'.
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-12 : 12:23:58
You already add back constraint in the script which you posted. See the red portion below:

quote:
Originally posted by dba123

I took a look at the table script for my PurchaseDownload table.

I'm trying to do this which works, but I need to add back in that constraint but not sure of the right syntax to do so.

First, what I'm performing is this so far:

ALTER TABLE PurchaseDownload
DROP CONSTRAINT [FK_PurchaseDownload_ProductFile]

delete from ProductFile
where ProductID in (
select f.ProductID from ProductFile f
inner join product p ON p.ProductID = f.ProductID
inner join SellerStore s ON s.SellerStoreID = p.SellerStoreID
where s.SellerStoreID = 1000
and FileName like '%zip%')

-- I need to readd that constraint or check back in here to restore it

Here's the PurchaseDownload table create script currently:

USE [MyDB]
GO
/****** Object: Table [dbo].[PurchaseDownload] Script Date: 03/12/2007 10:13:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[PurchaseDownload](
[PurchaseDownloadID] [bigint] IDENTITY(1,1) NOT NULL,
[PurchaseID] [bigint] NOT NULL,
[DownloadDate] [datetime] NOT NULL,
[IPAddress] [varchar](15) NOT NULL,
[BrowserInfo] [varchar](100) NOT NULL,
[ProductSourceStatusID] [int] NULL,
[CreateDate] [datetime] NOT NULL CONSTRAINT [DF_PurchaseDownload_CreateDate] DEFAULT (getdate()),
[CreateUserID] [int] NOT NULL,
[UpdateDate] [datetime] NULL,
[UpdateUserID] [int] NULL,
[Active] [bit] NOT NULL DEFAULT (1),
[ProductFileID] [bigint] NULL,
CONSTRAINT [PK_PurchaseDownload] PRIMARY KEY CLUSTERED
(
[PurchaseDownloadID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[PurchaseDownload] WITH CHECK ADD CONSTRAINT [FK_PurchaseDownload_ProductFile] FOREIGN KEY([ProductFileID])
REFERENCES [dbo].[ProductFile] ([ProductFileID])

GO
ALTER TABLE [dbo].[PurchaseDownload] CHECK CONSTRAINT [FK_PurchaseDownload_ProductFile]
GO
ALTER TABLE [dbo].[PurchaseDownload] WITH CHECK ADD CONSTRAINT [FK_PurchaseDownload_ProductSourceStatus] FOREIGN KEY([ProductSourceStatusID])
REFERENCES [dbo].[ProductSourceStatus] ([ProductSourceStatusID])
GO
ALTER TABLE [dbo].[PurchaseDownload] CHECK CONSTRAINT [FK_PurchaseDownload_ProductSourceStatus]
GO
ALTER TABLE [dbo].[PurchaseDownload] WITH CHECK ADD CONSTRAINT [FK_PurchaseDownload_Purchase] FOREIGN KEY([PurchaseID])
REFERENCES [dbo].[Purchase] ([PurchaseID])
GO
ALTER TABLE [dbo].[PurchaseDownload] CHECK CONSTRAINT [FK_PurchaseDownload_Purchase]

how can I add that constraint that I dropped back in?




Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

dba123
Yak Posting Veteran

90 Posts

Posted - 2007-03-12 : 12:34:45
no, that's just the table script of the existing table (right-clicked the table and create to script). I grab the code from when I did a Create Script off the existing table. It's not working as you can see
Go to Top of Page

dba123
Yak Posting Veteran

90 Posts

Posted - 2007-03-12 : 12:35:24
so in other words, the drop works fine, I tried to grab this and recreate it again after my delete script and it's erroring out as I showed last

ALTER TABLE [dbo].[PurchaseDownload] WITH CHECK ADD CONSTRAINT [FK_PurchaseDownload_ProductFile] FOREIGN KEY([ProductFileID])
REFERENCES [dbo].[ProductFile] ([ProductFileID])
Go to Top of Page

dba123
Yak Posting Veteran

90 Posts

Posted - 2007-03-12 : 12:36:30
so, in other words here's my latest script:

ALTER TABLE PurchaseDownload (nolock)
DROP CONSTRAINT [FK_PurchaseDownload_ProductFile]

delete from ProductFile (nolock)
where ProductID in
(select f.ProductID from ProductFile f
inner join product p ON p.ProductID = f.ProductID
inner join SellerStore s ON s.SellerStoreID = p.SellerStoreID
where s.SellerStoreID = 1000
and FileName like '%zip%')

ALTER TABLE PurchaseDownload
ADD CONSTRAINT [FK_PurchaseDownload_ProductFile] FOREIGN KEY([ProductFileID])
REFERENCES [dbo].[ProductFile] ([ProductFileID])

Error: ALTER TABLE statement conflicted with COLUMN FOREIGN KEY constraint 'FK_PurchaseDownload_ProductFile'. The conflict occurred in database 'MyTable', table 'ProductFile', column 'ProductFileID'.

I don't know what the hell this means and why it's complaining when clearly it's the same damn script form the Table script
Go to Top of Page

dba123
Yak Posting Veteran

90 Posts

Posted - 2007-03-12 : 12:41:20
sob, asked our DBA, she put NOCHECK, not sure why I need this but

ALTER TABLE PurchaseDownload WITH NOCHECK

ADD CONSTRAINT [FK_PurchaseDownload_ProductFile] FOREIGN KEY([ProductFileID])

REFERENCES [dbo].[ProductFile] ([ProductFileID
Go to Top of Page
   

- Advertisement -