ניצור שתי טבלאות:
Mysql>CREATE TABLE person ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, name CHAR(60) NOT NULL, PRIMARY KEY (id) );
Mysql>CREATE TABLE shirt ( -> id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, -> style ENUM('t-shirt', 'polo', 'dress') NOT NULL, -> color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL, -> owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id), -> PRIMARY KEY (id) ); |
שימו לב שהטבלה השניה מתייחסת למפתח הראשי בטבלה הראשונה.
עכשיו נכניס נתונים לטבלאות:
Mysql>INSERT INTO person VALUES (NULL, 'Antonio Paz');
Mysql>INSERT INTO shirt VALUES
->(NULL, 'polo', 'blue', LAST_INSERT_ID()),
->(NULL, 'dress', 'white', LAST_INSERT_ID()),
->(NULL, 't-shirt', 'blue', LAST_INSERT_ID());
Mysql>INSERT INTO person VALUES (NULL, 'Lilliana Angelovska');
Mysql>INSERT INTO shirt VALUES
->(NULL, 'dress', 'orange', LAST_INSERT_ID()),
->(NULL, 'polo', 'red', LAST_INSERT_ID()),
->(NULL, 'dress', 'blue', LAST_INSERT_ID()),
->(NULL, 't-shirt', 'white', LAST_INSERT_ID()); |
נבדוק שהנתונים הוכנסו כהלכה:
;Mysql>SELECT * FROM person |
והתוצאה:
|
;SELECT * FROM shirt |
והתוצאה:
|
עכשיו נרצה לראות את כל החולצות השייכות לליליאנה אשר אינן לבנות:
Mysql>SELECT s.* FROM person p, shirt s -> WHERE p.name LIKE 'Lilliana%' -> AND s.owner = p.id -> AND s.color <> 'white'; |
והתוצאה:
|