Foreign key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sva0008
    New Member
    • Apr 2008
    • 19

    Foreign key

    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.
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    [code=sql]

    -- if the table is already created:

    ALTER TABLE Match_Scores
    ADD CONSTRAINT [FK_Match_Scores _Matches] FOREIGN KEY (Match_Id)
    REFERENCES [Matches] ([Match_Id])
    GO



    CREATE TABLE works_on (emp_no INTEGER NOT NULL,
    project_no CHAR(4) NOT NULL,
    job CHAR (15) NULL,
    enter_date DATETIME NULL,
    CONSTRAINT prim_works PRIMARY KEY (emp_no, project_no),
    CONSTRAINT foreign_works FOREIGN KEY (emp_no) REFERENCES employee (emp_no))


    [/code]

    Comment

    • akashazad
      New Member
      • Sep 2007
      • 38

      #3
      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)
      )

      Comment

      Working...