Wednesday, March 23, 2016

Creating Primary and foreign key relationship between two tables in mysql

Primary and foreign key relationship between two tables

1.First we need to create a Primary key field table
CREATE TABLE Sale(
    sale_id CHAR(40),
    PRIMARY KEY(sale_id),
    discount DOUBLE,
    type VARCHAR(255),
    price DOUBLE
) ENGINE=INNODB;

2.After creating primary field table, we have to create second table 

CREATE TABLE Normal_Sale(
    sale_id CHAR(40),
    PRIMARY KEY(sale_id),
    FOREIGN KEY (sale_id) REFERENCES Sale(sale_id)
) ENGINE=INNODB;

No comments:

Post a Comment