How to use foreign key in SQL statements?

1

I'm trying to use the foreign key but I can not. This SQL statement will create a table named tbl_estado with 4 columns id, nome, uf, pais and in column pais will enter the foreign key that will be searched in tbl_pais id of the country that is the id column.

$ where = axitech20 (bank)

$ tablep = tbl_pais

$ tablee = tbl_estado

$sql8="CREATE TABLE IF NOT EXISTS $aonde.$tablee (
            id INT(6) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY,
            nome        VARCHAR(75) DEFAULT NULL,
            uf          VARCHAR(5) DEFAULT NULL,
            pais        INT(6) DEFAULT NULL KEY '??????????' ($tablep)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=28";

I can not make the relationship that is something there in the ?????????? of the statement. Can you help me hit foreign key ?

    
asked by anonymous 17.10.2014 / 18:36

1 answer

3

This should work:

CREATE TABLE axitech20.tb1_pais (
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT
);

CREATE TABLE axitech20.tbl_estado (
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    id_pais INT NOT NULL,
    nome VARCHAR(75),
    uf VARCHAR(5),
    FOREIGN KEY (id_pais) REFERENCES tb1_pais(id)
);
    
18.10.2014 / 14:18