פקודות בסיסיות MySQL:
mysql -u root -p - התחברות אל מסד הנתונים משתמש רוט
quit - יציאה מהשירות
show databases; - הצגת מסדי הנתונים הקיימים
create database supersecret; - supersecret יצירת מסד נתונים בשם
use information_schema - information_schema מעבר אל מסד הנתונים
show tables; - הצגת טבלאות
יצירת טבלה חדשה עם מספר עמודות (לכל עמודה יש להגדיר מראש את סוג הקלט):
mysql> create table employees (-> id INTEGER,
-> name TEXT,
-> age INTEGER
-> );
Query OK, 0 rows affected (0.01 sec)
הצגת מידע אודות העמודות המופיעות בטבלה:
mysql> describe employees;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | text | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
הכנסת נתונים אל טבלה:
mysql> insert into employees (id, name, age)-> values (1, 'Haim Levanon', 42);
Query OK, 1 row affected (0.00 sec)
הצגת נתונים מטבלה:
mysql> select * from employees;
+------+---------------+------+
| id | name | age |
+------+---------------+------+
| 1 | Haim Levanon | 42 |
| 2 | Itzhak Shamir | 29 |
+------+---------------+------+
2 rows in set (0.00 sec)
הוספתי עוד מספר עובדים:
mysql> select * from employees;
+------+------------------+------+
| id | name | age |
+------+------------------+------+
| 1 | Haim Levanon | 42 |
| 2 | Itzhak Shamir | 29 |
| 3 | Damba Rubma | 42 |
| 4 | Runda Juakim | 49 |
| 5 | Marta Cohen | 29 |
| 6 | Kalev Ben Yefune | 35 |
+------+------------------+------+
6 rows in set (0.00 sec)
הצגת כל העובדים שגילם 29:
mysql> select * from employees where age = 29;
+------+---------------+------+
| id | name | age |
+------+---------------+------+
| 2 | Itzhak Shamir | 29 |
| 5 | Marta Cohen | 29 |
+------+---------------+------+
2 rows in set (0.00 sec)
הצגת שמות כל העובדים:
mysql> select name from employees;+------------------+
| name |
+------------------+
| Haim Levanon |
| Itzhak Shamir |
| Damba Rubma |
| Runda Juakim |
| Marta Cohen |
| Kalev Ben Yefune |
+------------------+
6 rows in set (0.00 sec)
עדכון נתונים בטבלה:
mysql> select * from employees;+------+------------------+------+
| id | name | age |
+------+------------------+------+
| 1 | Haim Levanon | 42 |
| 2 | Itzhak Shamir | 29 |
| 3 | Damba Rubma | 42 |
| 4 | Runda Juakim | 49 |
| 5 | Marta Cohen | 29 |
| 6 | Kalev Ben Yefune | 35 |
+------+------------------+------+
6 rows in set (0.00 sec)
mysql> update employees
-> set age = 29
-> where id = 6;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
נבדוק האם השינויים התעדכנו:
mysql> select * from employees where id = 6;
+------+------------------+------+
| id | name | age |
+------+------------------+------+
| 6 | Kalev Ben Yefune | 29 |
+------+------------------+------+
1 row in set (0.00 sec)
הוספת עמודה חדשה לטבלה:
mysql> alter table employees add column-> phone integer;
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from employees;
+------+------------------+------+-------+
| id | name | age | phone |
+------+------------------+------+-------+
| 1 | Haim Levanon | 42 | NULL |
| 2 | Itzhak Shamir | 29 | NULL |
| 3 | Damba Rubma | 42 | NULL |
| 4 | Runda Juakim | 49 | NULL |
| 5 | Marta Cohen | 29 | NULL |
| 6 | Kalev Ben Yefune | 29 | NULL |
+------+------------------+------+-------+
6 rows in set (0.00 sec)
מחיקת נתונים מתוך הטבלה:
mysql> DELETE FROM employees where id=3;Query OK, 1 row affected (0.01 sec)
mysql> select * from employees;
+------+------------------+------+-------+
| id | name | age | phone |
+------+------------------+------+-------+
| 1 | Haim Levanon | 42 | NULL |
| 2 | Itzhak Shamir | 29 | NULL |
| 4 | Runda Juakim | 49 | NULL |
| 5 | Marta Cohen | 29 | NULL |
| 6 | Kalev Ben Yefune | 29 | NULL |
+------+------------------+------+-------+
5 rows in set (0.01 sec)
מחיקת עמודות:
mysql> alter table employees drop phone;
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from employees;
+------+------------------+------+
| id | name | age |
+------+------------------+------+
| 1 | Haim Levanon | 42 |
| 2 | Itzhak Shamir | 29 |
| 4 | Runda Juakim | 49 |
| 5 | Marta Cohen | 29 |
| 6 | Kalev Ben Yefune | 29 |
+------+------------------+------+
5 rows in set (0.00 sec)