Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Foreign key

Question posted by: sva0008 (Newbie) on June 30th, 2008 04:57 PM
I have a table name product
that have column productCode (primary key), name address , ph , warehouse .



I want to create a new table with columns productcode and price.

with productcode as primary key and foreign key to productinfo table.



I am new to sql server can anyone let me know how to make it foreign key.
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
amitpatel66's Avatar
amitpatel66
Moderator
1,837 Posts
July 1st, 2008
10:54 AM
#2

Re: Foreign key
Code: ( text )
  1. -- if the table is already created:
  2.  
  3. ALTER TABLE Match_Scores
  4. ADD CONSTRAINT [FK_Match_Scores_Matches] FOREIGN KEY (Match_Id)
  5. REFERENCES [Matches] ([Match_Id])
  6. GO
  7.  
  8.  
  9.  
  10. CREATE TABLE works_on (emp_no     INTEGER NOT NULL,
  11. project_no CHAR(4) NOT NULL,
  12. job        CHAR (15) NULL,
  13. enter_date DATETIME NULL,
  14. CONSTRAINT prim_works PRIMARY KEY (emp_no, project_no),
  15. CONSTRAINT foreign_works FOREIGN KEY (emp_no) REFERENCES employee (emp_no))

Reply
akashazad's Avatar
akashazad
Newbie
24 Posts
July 2nd, 2008
06:42 PM
#3

Re: Foreign key
Hi ,
I think this will help U out

CREATE TABLE ProductInfo
(
ProductCode INT,
ProductName VARCHAR (20),
SupplierName VARCHAR (30),
PRIMARY KEY (ProductCode)
)

CREATE TABLE ProductSale
(
ProductCode INT,
CustomerName VARCHAR (20),
Amount DOUBLE,
PRIMARY KEY (ProductCode),
FOREIGN KEY (ProductCode) REFERENCES ProductInfo (ProductCode)
)

Reply
Reply
Not the answer you were looking for? Post your question . . .
182,197 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Microsoft SQL Server Contributors