943,965 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 17129
  • MS SQL RSS
Oct 30th, 2007
0

'There is already an object named 'ProductID' in the database.'

Expand Post »
Hey guys!

I seem to be posting here a lot the past few days but as I am new to SQL Server I am finding to quite a pain to get used to!

I am having a problem referencing foreign keys. I did post another thread about this but this is a slightly different problem.

everytime I try to reference a foreign key SQL Server says -
'There is already an object named 'ProductID' in the database.'

yes there is another Product ID in the Product table, why won't SQL Server recognise that it is a foreign key and needs to be in this table too? This is happening quite a lot when I am creating the tables and trying to reference the foreign key (not just ProductID). Here is the code for two of my tables:-

sql Syntax (Toggle Plain Text)
  1. CREATE TABLE HireLine (
  2. HireID INT IDENTITY NOT NULL ,
  3. ProductID INT NOT NULL,
  4. Quantity INT NULL,
  5. PRIMARY KEY (HireID),
  6. CONSTRAINT ProductID FOREIGN KEY (ProductID)
  7. REFERENCES Product (ProductID)
  8. ON DELETE CASCADE ON UPDATE CASCADE,
  9. );

sql Syntax (Toggle Plain Text)
  1. CREATE TABLE CorderLine (
  2. CorderID INT,
  3. ProductID INT,
  4. Quantity INT NOT NULL,
  5. PRIMARY KEY (CorderID, ProductID),
  6. CONSTRAINT CorderID FOREIGN KEY (CorderID)
  7. REFERENCES Corder (CorderID)
  8. ON DELETE CASCADE ON UPDATE CASCADE,
  9. CONSTRAINT ProductID FOREIGN KEY (ProductID)
  10. REFERENCES Product (ProductID)
  11. ON DELETE CASCADE ON UPDATE CASCADE,
  12. );

has anyone any ideas of where I am going wrong?
Last edited by peter_budo; Dec 13th, 2009 at 9:05 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Similar Threads
GLT
Reputation Points: 18
Solved Threads: 0
Junior Poster in Training
GLT is offline Offline
98 posts
since Aug 2007
Nov 1st, 2007
0

Re: 'There is already an object named 'ProductID' in the database.'

This problem is because you are trying to create two objects by same name. You are creating a column and a constraint by the same name with in a table . this why u are getting this error . Try to use different name for the coumn and the constraint.
Featured Poster
Reputation Points: 665
Solved Threads: 427
Posting Genius
debasisdas is offline Offline
6,406 posts
since Feb 2007
Nov 1st, 2007
0

Re: 'There is already an object named 'ProductID' in the database.'

Click to Expand / Collapse  Quote originally posted by debasisdas ...
This problem is because you are trying to create two objects by same name. You are creating a column and a constraint by the same name with in a table . this why u are getting this error . Try to use different name for the coumn and the constraint.
Thanks for your reply!

I am trying to make the ProductID column the foreign key. I thought that using a constraint like this was how to make it a foreign key. How would i code this? would I say:-

sql Syntax (Toggle Plain Text)
  1. CREATE TABLE HireLine (
  2. HireID INT IDENTITY NOT NULL ,
  3. ProductID INT NOT NULL,
  4. Quantity INT NULL,
  5. PRIMARY KEY (HireID),
  6. CONSTRAINT FK_ProductID FOREIGN KEY (ProductID)
  7. REFERENCES Product (ProductID)
  8. ON DELETE CASCADE ON UPDATE CASCADE,
  9. );

Thanks
GLT
Last edited by peter_budo; Dec 13th, 2009 at 9:06 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
GLT
Reputation Points: 18
Solved Threads: 0
Junior Poster in Training
GLT is offline Offline
98 posts
since Aug 2007
Nov 1st, 2007
0

Re: 'There is already an object named 'ProductID' in the database.'

Yep, that's it
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Dec 12th, 2009
0
Re: 'There is already an object named 'ProductID' in the database.'
im new to database

i think that in table Corder_line
is having a productID as primary key and the same time a foreign key
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iAmLast is offline Offline
6 posts
since Dec 2009
Dec 16th, 2009
0
Re: 'There is already an object named 'ProductID' in the database.'
So what is your problem ?
Featured Poster
Reputation Points: 665
Solved Threads: 427
Posting Genius
debasisdas is offline Offline
6,406 posts
since Feb 2007
Dec 21st, 2009
0
Re: 'There is already an object named 'ProductID' in the database.'
Ms. SQL Server treats constraint as an object. You can check it in sys.objects table.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
achied is offline Offline
7 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Query by Position
Next Thread in MS SQL Forum Timeline: SQL failure exposes plain text passwords





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC