-1

I have already created a table in MySQL! And have tried a number of queries to alter the table and add foreign key to the table! But none of them work?? No error message no nothing but still nothing happening...

I need the exact query which would work! :(

Details:

  1. Table1: users column: id
  2. Table2: Pokemon_ref column: pkmn_id

If an insertion is done in Table1 then it should also be added in Table2!

user3531660
  • 15
  • 2
  • 9

2 Answers2

1
    category INT NOT NULL, id INT NOT NULL,
    price DECIMAL,
    PRIMARY KEY(category, id)
)   ENGINE=INNODB;

CREATE TABLE customer (
    id INT NOT NULL,
    PRIMARY KEY (id)
)   ENGINE=INNODB;

CREATE TABLE product_order (
    no INT NOT NULL AUTO_INCREMENT,
    product_category INT NOT NULL,
    product_id INT NOT NULL,
    customer_id INT NOT NULL,

    PRIMARY KEY(no),
    INDEX (product_category, product_id),
    INDEX (customer_id),

    FOREIGN KEY (product_category, product_id)
      REFERENCES product(category, id)
      ON UPDATE CASCADE ON DELETE RESTRICT,

    FOREIGN KEY (customer_id)
      REFERENCES customer(id)
) 
Reena Shirale
  • 1,992
  • 1
  • 17
  • 15
  • ALTER TABLE tabl2 ADD COLUMN FOREIGNID INT NOT NULL, ADD FOREIGN KEY (FOREIGNID) REFERENCES tabl1(ID) – aΨVaN Apr 15 '14 at 05:21
1

sample:

ALTER TABLE tablename
ADD CONSTRAINT FK_Name_ID FOREIGN KEY (fk_ID)
    REFERENCES (R_id);

https://dev.mysql.com/doc/refman/5.1/en/create-table-foreign-keys.html

http://www.w3schools.com/sql/sql_foreignkey.asp

Alter table to give foreign key constraint

Community
  • 1
  • 1
jmail
  • 5,944
  • 3
  • 21
  • 35