SQL學習(選擇數據庫和檢索數據)

學習環境

    sudo apt-get install mysql-server

以下操作實際上均在終端上執行,但建議在MySQL Workbench上執行,圖形化界面更利於交互,更容易理解數據庫的結構。

瞭解數據庫和表

 數據庫、表、列、用戶、權限、等的信息被存儲在數據庫和表中。不過內部的表一般不允許訪問,可用Mysql的SHOW命令來顯示這些信息(MySQL從內部表中來提取這些信息)

SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| SQL_learning_DB    |
| mysql              |
| performance_schema |
| shiyanlou001       |
| sys                |
+--------------------+

返回可用數據庫的一個列表。爲了獲得一個數據庫內的表的列表,使用SHOW TABLES;但在這之前得先選一個想要查表的數據庫,否則會報錯
mysql> SHOW TABLES;
ERROR 1046 (3D000): No database selected

mysql> USE SQL_learning_DB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

mysql> SHOW TABLES;
+---------------------------+
| Tables_in_SQL_learning_DB |
+---------------------------+
| customers                 |
| orderitems                |
| orders                    |
| productnotes              |
| products                  |
| vendors                   |
+---------------------------+
6 rows in set (0.00 sec)

也可以用SHOW來顯示錶列

mysql> SHOW COLUMNS FROM customers;
+--------------+-----------+------+-----+---------+----------------+
| Field        | Type      | Null | Key | Default | Extra          |
+--------------+-----------+------+-----+---------+----------------+
| cust_id      | int(11)   | NO   | PRI | NULL    | auto_increment |
| cust_name    | char(50)  | NO   |     | NULL    |                |
| cust_address | char(50)  | YES  |     | NULL    |                |
| cust_city    | char(50)  | YES  |     | NULL    |                |
| cust_state   | char(5)   | YES  |     | NULL    |                |
| cust_zip     | char(10)  | YES  |     | NULL    |                |
| cust_country | char(50)  | YES  |     | NULL    |                |
| cust_contact | char(50)  | YES  |     | NULL    |                |
| cust_email   | char(255) | YES  |     | NULL    |                |
+--------------+-----------+------+-----+---------+----------------+
9 rows in set (0.00 sec)

也可以用DESCRIBE 作爲SHOW COLUMNS的一種快捷方式。

mysql> DESCRIBE customers;
+--------------+-----------+------+-----+---------+----------------+
| Field        | Type      | Null | Key | Default | Extra          |
+--------------+-----------+------+-----+---------+----------------+
| cust_id      | int(11)   | NO   | PRI | NULL    | auto_increment |
| cust_name    | char(50)  | NO   |     | NULL    |                |
| cust_address | char(50)  | YES  |     | NULL    |                |
| cust_city    | char(50)  | YES  |     | NULL    |                |
| cust_state   | char(5)   | YES  |     | NULL    |                |
| cust_zip     | char(10)  | YES  |     | NULL    |                |
| cust_country | char(50)  | YES  |     | NULL    |                |
| cust_contact | char(50)  | YES  |     | NULL    |                |
| cust_email   | char(255) | YES  |     | NULL    |                |
+--------------+-----------+------+-----+---------+----------------+
9 rows in set (0.00 sec)

檢索數據

SELECT用來從一個列表或多個列表來檢索信息,爲了使用SELECT,必須給出兩條信息——想選擇什麼,以及從什麼地方選擇。

檢查單個列

mysql> SELECT prod_name FROM products;
+----------------+
| prod_name      |
+----------------+
| .5 ton anvil   |
| 1 ton anvil    |
| 2 ton anvil    |
| Detonator      |
| Bird seed      |
| Carrots        |
| Fuses          |
| JetPack 1000   |
| JetPack 2000   |
| Oil can        |
| Safe           |
| Sling          |
| TNT (1 stick)  |
| TNT (5 sticks) |
+----------------+
14 rows in set (0.00 sec)

這裏因爲沒有明確排序查詢結果,返回的數據的順序是隨機的。只要返回相同數目的行,就是正常的。

檢查多個列

mysql> SELECT prod_id,vend_id,prod_name,prod_price FROM products;
+---------+---------+----------------+------------+
| prod_id | vend_id | prod_name      | prod_price |
+---------+---------+----------------+------------+
| ANV01   |    1001 | .5 ton anvil   |       5.99 |
| ANV02   |    1001 | 1 ton anvil    |       9.99 |
| ANV03   |    1001 | 2 ton anvil    |      14.99 |
| DTNTR   |    1003 | Detonator      |      13.00 |
| FB      |    1003 | Bird seed      |      10.00 |
| FC      |    1003 | Carrots        |       2.50 |
| FU1     |    1002 | Fuses          |       3.42 |
| JP1000  |    1005 | JetPack 1000   |      35.00 |
| JP2000  |    1005 | JetPack 2000   |      55.00 |
| OL1     |    1002 | Oil can        |       8.99 |
| SAFE    |    1003 | Safe           |      50.00 |
| SLING   |    1003 | Sling          |       4.49 |
| TNT1    |    1003 | TNT (1 stick)  |       2.50 |
| TNT2    |    1003 | TNT (5 sticks) |      10.00 |
+---------+---------+----------------+------------+
14 rows in set (0.00 sec)

列名之間用逗號間隔即可。

檢索所有列

mysql> SELECT * FROM products;
+---------+---------+----------------+------------+----------------------------------------------------------------+
| prod_id | vend_id | prod_name      | prod_price | prod_desc                                                      |
+---------+---------+----------------+------------+----------------------------------------------------------------+
| ANV01   |    1001 | .5 ton anvil   |       5.99 | .5 ton anvil, black, complete with handy hook                  |
| ANV02   |    1001 | 1 ton anvil    |       9.99 | 1 ton anvil, black, complete with handy hook and carrying case |
| ANV03   |    1001 | 2 ton anvil    |      14.99 | 2 ton anvil, black, complete with handy hook and carrying case |
| DTNTR   |    1003 | Detonator      |      13.00 | Detonator (plunger powered), fuses not included                |
| FB      |    1003 | Bird seed      |      10.00 | Large bag (suitable for road runners)                          |
| FC      |    1003 | Carrots        |       2.50 | Carrots (rabbit hunting season only)                           |
| FU1     |    1002 | Fuses          |       3.42 | 1 dozen, extra long                                            |
| JP1000  |    1005 | JetPack 1000   |      35.00 | JetPack 1000, intended for single use                          |
| JP2000  |    1005 | JetPack 2000   |      55.00 | JetPack 2000, multi-use                                        |
| OL1     |    1002 | Oil can        |       8.99 | Oil can, red                                                   |
| SAFE    |    1003 | Safe           |      50.00 | Safe with combination lock                                     |
| SLING   |    1003 | Sling          |       4.49 | Sling, one size fits all                                       |
| TNT1    |    1003 | TNT (1 stick)  |       2.50 | TNT, red, single stick                                         |
| TNT2    |    1003 | TNT (5 sticks) |      10.00 | TNT, red, pack of 10 sticks                                    |
+---------+---------+----------------+------------+----------------------------------------------------------------+
14 rows in set (0.00 sec)

檢索所有列用*表示。

檢索不同的行

mysql> SELECT vend_id FROM products;
+---------+
| vend_id |
+---------+
|    1001 |
|    1001 |
|    1001 |
|    1002 |
|    1002 |
|    1003 |
|    1003 |
|    1003 |
|    1003 |
|    1003 |
|    1003 |
|    1003 |
|    1005 |
|    1005 |
+---------+
14 rows in set (0.00 sec)

mysql> SELECT DISTINCT vend_id FROM products;
+---------+
| vend_id |
+---------+
|    1001 |
|    1002 |
|    1003 |
|    1005 |
+---------+
4 rows in set (0.00 sec)

SELECT DISTINCT vend_id告訴MySQL只返回不同(唯一)的vend_id行,因此只返回4行

限定結果

SELECT 語句返回所有匹配的行,它們可能是制定表中的每個行。爲了返回第一行或前幾行,可使用LIMIT子句。

mysql> SELECT DISTINCT vend_id FROM products LIMIT 2;
+---------+
| vend_id |
+---------+
|    1001 |
|    1002 |
+---------+
2 rows in set (0.00 sec)

LIMIT 2 指示MySQL返回不多於2行。

mysql> SELECT DISTINCT vend_id FROM products LIMIT 2,2;
+---------+
| vend_id |
+---------+
|    1003 |
|    1005 |
+---------+
2 rows in set (0.00 sec)

LIMIT 2,2 指示MySQL返回從行2開始的2行。列的第一行爲行0。

使用完全限定的表名

SELECT products.prod_name FROM products;
SELECT products.prod_name FROM SQL_learning_DB.products

上面兩條語句實現的檢索是一樣的。有一些情況下是需要完全限定名的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章