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
 General SQL Server Forums
 New to SQL Server Programming
 How do I create create this relationship?

Author  Topic 

spmchugh82
Starting Member

13 Posts

Posted - 2009-02-17 : 19:21:57
Hi, I'm stupid. I need help again. Here is a picture that shows what I'm working with: http://www.seansbagoftricks.com/uploads/compositePK.jpg

The Pizza column in the Pizza table needs to reference the Pizza column in the Used Ingredients table and the Ingredients column in the Ingredients table needs to reference the Ingredients table in the Used Ingredients table.

In other words, how to I reference a composite primary key if the foriegn key is only part of the composite primary key???

If you look at the picture you'll understand what I'm saying.

Thanks in advance!

X002548
Not Just a Number

15586 Posts

Posted - 2009-02-17 : 19:30:56
The "Pizza" table?

Do they teach SQL?


What are you trying to do?

Create a Foreign key constraint?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

spmchugh82
Starting Member

13 Posts

Posted - 2009-02-17 : 19:34:08
pizza------>(pizza|ingredient)<-------ingredient

thats what i want to do
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-02-17 : 19:43:07
ALTER TABLE PizzaIngredient
ADD CONSTRAINT FK_Pizza_PizzaIngredient
FOREIGN KEY (Pizza) REFERENCES Pizza (Pizza)
GO


ALTER TABLE PizzaIngredient
ADD CONSTRAINT FK_Ingredient_PizzaIngredient
FOREIGN KEY (Ingredient) REFERENCES Ingredient (Ingredient)
GO

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-02-17 : 19:46:00
[code]
ALTER TABLE [dbo].[Pizza_Ingredients] WITH CHECK
ADD CONSTRAINT [FK_Pizza_Ingredients_Pizza_1]
FOREIGN KEY([Pizza])
REFERENCES [dbo].[Pizza] ([Pizza])

[/code]



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-02-17 : 19:46:45
quote:
Originally posted by tkizer

ALTER TABLE PizzaIngredient
ADD CONSTRAINT FK_Pizza_PizzaIngredient
FOREIGN KEY (Pizza) REFERENCES Pizza (Pizza)
GO


ALTER TABLE PizzaIngredient
ADD CONSTRAINT FK_Ingredient_PizzaIngredient
FOREIGN KEY (Ingredient) REFERENCES Ingredient (Ingredient)
GO

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog





BRILLIANT!



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

spmchugh82
Starting Member

13 Posts

Posted - 2009-02-17 : 19:58:53
Okay, thanks guys. I tried yours Tara and got it to accept the commands after changing things around. Yep, I had it backwards anyhow.

THANKS EVERYONE!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-02-17 : 20:00:52
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -