/
, , , , FK. .
, , , .
:
, . "user_purchase.i_id items". , , .
, FOREIGN KEY:
CREATE TABLE user(
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE items(
i_id INT(11) NOT NULL AUTO_INCREMENT,
name TINYTEXT NOT NULL,
price DECIMAL(8,2) NOT NULL,
PRIMARY KEY (i_id)
);
CREATE TABLE user_purchase(
i_id INT(11) NOT NULL,
name TINYTEXT NOT NULL,
id INT(11) NOT NULL,
);
, - . , user (id), , items (i_id), name. :
user user_purchase items
| id username | | id i_id | | i_id name price |
| 23 john | | 55 10 | | 10 chocolate bar 3.42 |
| 55 mary | | 70 10 | | 33 mobile phone 82.11 |
| 70 fred | | 70 33 | | 54 toothpaste 8.67 |
| 55 10 | | 26 toy car 6.00 |
| 70 26 |
. user_purchase, , . , :
select * from user_purchase p
join user u on (p.id=u.id)
join items i on (p.i_id=i.i_id)
, .
, , :
insert into user_purchase (id,i_id) values (23,99)
-, . id=23, i_id=99. RDBMS , . .
, . FOREIGN KEY (i_id) REFERENCES items(i_id) user_purchase, RDBMS : i_id , items.i_id, . , , , .
, , select , , FK. , FK, RDBMS, .
... , ? ?
: ? , . :
user_purchase items
| id i_id name | | i_id name price |
| 55 10 chocolate bar | | 10 chocolate bar 3.42 |
| 70 10 chocolate bar | | 33 mobile phone 82.11 |
| 70 33 mobile phone | | 54 toothpaste 8.67 |
| 55 10 toothpaste | | 26 toy car 6.00 |
| 70 26 toy car |
? 55 , ? , , . , name , .
, , , PRIMARY KEY(i_id,name) items ( UNIQUE(i_id,name) , ), FOREIGN KEY(i_id,name) REFERENCES items(i_id,name). , (i_id, name), items, user_purchases. , , , , i_id , ( name).
. , . person(id,name) parent(person,father,mother), :
person parent
| id name | | person father mother |
| 14 John | | 21 14 59 |
| 43 Jane | | 14 76 43 |
| 21 Mike |
| 76 Frank |
| 59 Mary |
, parent person. , : - , person. , , , person parent, , - .