· 6 years ago · Jun 18, 2019, 12:48 AM
1CREATE TABLE IF NOT EXISTS parent (
2 p_id INT NOT NULL,
3 p_org INT NOT NULL,
4 PRIMARY KEY(p_id, p_org),
5 p_name VARCHAR(12))
6ENGINE=InnoDB;
7
8CREATE TABLE IF NOT EXISTS child (
9 c_id INT NOT NULL PRIMARY KEY,
10 c_org INT NOT NULL,
11 c_p_id INT NOT NULL, FOREIGN KEY(c_p_id, c_org) REFERENCES parent(p_id, p_org),
12 c_info VARCHAR(12))
13ENGINE=InnoDB;
14
15insert into parent values(100, 1, 'name-1'), (100, 2, 'name-2');
16
17insert into child values(1000, 2, **100**, 'info-for-2');
18
19p_id p_org p_name c_id c_org c_p_id c_info
20100 1 name-1 1000 2 100 info-for-2
21100 2 name-2 1000 2 100 info-for-2