MySQL初步

一 寫在開頭
1.1 本節內容
本節的主要內容是MySQL的基本操做(來自MySQL 5.7官方文檔)。

1.2 工具準備
一臺裝好了mysql的ubuntu 16.04 LTS機器。

二 MySQL的鏈接與斷開
2.1 鏈接與斷開MySQL服務
MySQL提供了一個Linux命令行程序mysql,利用它咱們能夠鏈接到MySQL並執行SQL操做。想要查看mysql自帶幫助文檔,請使用如下命令。mysql

mysql --help

2.2.1 使用如下命令鏈接到MySQLsql

mysql -h localhost -u root -p

命令詳解:
-h後面參數表示主機名。若是MySQL安裝在了本地,後面的參數可使用localhost表示本地。也能夠省略-h參數。
-u後面的參數表示使用的帳戶名。可使用root用戶,前提是你得記得安裝MySQL時輸入的root用戶密碼。
-p參數表示須要輸入密碼。通常來講-p參數是必須的。

2.1.2 使用quit命令退出mysql命令行數據庫

quit


三 鍵入查詢語句
3.1 查詢MySQL版本和當前時間ubuntu

SELECT VERSION(), CURRENT_DATE();
+-----------+----------------+
| VERSION() | CURRENT_DATE() |
+-----------+----------------+
| 5.7.21    | 2018-02-14     |
+-----------+----------------+
1 row in set (0.06 sec)

 

注意:SQL語句後面有個分號,quit命令後沒有分號。

3.2 使用MySQL作簡單計算函數

SELECT SIN(PI()/2), (4+1)*5;
+-------------+---------+
| SIN(PI()/2) | (4+1)*5 |
+-------------+---------+
|           1 |      25 |
+-------------+---------+
1 row in set (0.05 sec)


3.3 將多條命令寫在同一行工具

SELECT VERSION(); SELECT NOW();
+-----------+
| VERSION() |
+-----------+
| 5.7.21    |
+-----------+
1 row in set (0.00 sec)

+---------------------+
| NOW()               |
+---------------------+
| 2018-02-14 15:11:56 |
+---------------------+
1 row in set (0.00 sec)


3.4 將一條命令寫在多行中ui

SELECT 
USER()
,
CURRENT_DATE();
+----------------+----------------+
| USER()         | CURRENT_DATE() |
+----------------+----------------+
| root@localhost | 2018-02-14     |
+----------------+----------------+
1 row in set (0.05 sec)

注意:在MySQL中,識別一條SQL命令的標誌是分號。因此咱們能夠把一條命令寫在多行中。

3.5 撤銷當前輸入this

mysql> SELECT
-> USER()
-> \c
mysql>

 

注意:c是小寫的。

四 數據庫的建立和使用
4.1 查看當前系統中的數據庫spa

SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.15 sec)

注意:SHOW DATEBASES;只會將當前用戶具備的SHOW DATABASES命令權限的數據庫打印出來。

4.2 使用(進入)數據庫命令行

USE sys
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

 

注意:USE和前面的QUIT同樣,後面是不接分號的。與以前命令不同的是USE命令必須寫在單獨一行中,不能跨越多行。

4.3 建立一個數據庫

CREATE DATABASE menagerie;
Query OK, 1 row affected (0.07 sec)

注意:在Linux下數據庫名是大小寫敏感的。

 

4.4 在鏈接MySQL時就指定使用menagerie數據庫

mysql -u root -p menagerie


4.5 查看當前使用的是哪一個數據庫

SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| menagerie  |
+------------+
1 row in set (0.00 sec)


4.6 查看當前數據庫中有哪些表

SELECT TABLES;
Empty set (0.00 sec)

 

注意:剛剛建立的數據庫中是沒有表的。

4.7 建立表格

mysql> CREATE TABLE pet
    -> (
    -> name VARCHAR(20),
    -> owner VARCHAR(20),
    -> species VARCHAR(20),
    -> sex CHAR(1),
    -> birth DATE,
    -> death DATE
    -> );
Query OK, 0 rows affected (0.15 sec)

mysql> SHOW TABLES;
+---------------------+
| Tables_in_menagerie |
+---------------------+
| pet                 |
+---------------------+
1 row in set (0.01 sec)

 


4.8 查看錶的信息

mysql> DESCRIBE pet;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(20) | YES  |     | NULL    |       |
| owner   | varchar(20) | YES  |     | NULL    |       |
| species | varchar(20) | YES  |     | NULL    |       |
| sex     | char(1)     | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| death   | date        | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)


4.9 向表中輸入信息
第一步:建立一個pet.sql文件,文件內容以下。

INSERT INTO pet
VALUES ('Fluffy', 'Harold', 'cat', 'f', '1993-02-04', NULL);

INSERT INTO pet
VALUES ('Claws', 'Gwen', 'cat', 'm', '1994-03-17', NULL);

INSERT INTO pet
VALUES ('Buffy', 'Harold', 'dog', 'f', '1989-05-13', NULL);

INSERT INTO pet
VALUES ('Fang', 'Benny', 'dog', 'm', '1990-08-27', NULL);

INSERT INTO pet
VALUES ('Bowser', 'Diane', 'dog', 'm', '1979-08-31', '1995-07-29');

INSERT INTO pet
VALUES ('Chirpy', 'Gwen', 'bird', 'f', '1998-09-11', NULL);

INSERT INTO pet
VALUES ('Whistler', 'Gwen', 'bird', NULL, '1997-12-09', NULL);

INSERT INTO pet
VALUES ('Slim', 'Benny', 'snake', 'm', '1996-04-29', NULL);

INSERT INTO pet
VALUES ('Puffball', 'Diane', 'hamster', 'f', '1999-03-30', NULL);

第二步:使用下列命令執行sql文件,隨後查看錶的內容。

mysql> source ~/pet.sql
Query OK, 1 row affected (0.13 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.02 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM pet;
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |
| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |
| Whistler | Gwen   | bird    | NULL | 1997-12-09 | NULL       |
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |
| Puffball | Diane  | hamster | f    | 1999-03-30 | NULL       |
+----------+--------+---------+------+------------+------------+
9 rows in set (0.00 sec)


五 SELECT語句
5.1 選擇全部的列

mysql> SELECT * FROM pet;
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |
| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |
| Whistler | Gwen   | bird    | NULL | 1997-12-09 | NULL       |
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |
| Puffball | Diane  | hamster | f    | 1999-03-30 | NULL       |
+----------+--------+---------+------+------------+------------+
9 rows in set (0.00 sec)


5.2 修改數據

mysql> UPDATE pet SET birth = '1989-08-31' WHERE name = 'Bowser';
Query OK, 1 row affected (0.17 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT birth FROM pet WHERE name = 'Bowser';
+------------+
| birth      |
+------------+
| 1989-08-31 |
+------------+
1 row in set (0.00 sec)


5.3 清空表內容

mysql> DELETE FROM pet;
Query OK, 9 rows affected (0.08 sec)

mysql> SELECT * FROM pet;
Empty set (0.00 sec)

 


5.4 選擇特定行

mysql> SELECT * FROM pet WHERE name = 'Bowser';
+--------+-------+---------+------+------------+------------+
| name   | owner | species | sex  | birth      | death      |
+--------+-------+---------+------+------------+------------+
| Bowser | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+
1 row in set (0.00 sec)

 

5.5 根據條件選擇

mysql> SELECT * FROM pet WHERE birth >= '1998-1-1';
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| Chirpy   | Gwen  | bird    | f    | 1998-09-11 | NULL  |
| Puffball | Diane | hamster | f    | 1999-03-30 | NULL  |
+----------+-------+---------+------+------------+-------+
2 rows in set (0.00 sec)

 


5.6 根據組合條件選擇
5.6.1

mysql> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+
1 row in set (0.04 sec)

 

 

5.6.2

mysql> SELECT * FROM pet WHERE
    -> (species = 'cat' AND sex = 'm')
    -> OR (species = 'dog' AND sex = 'f');
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+
2 rows in set (0.00 sec)

注意:AND優先級高於OR。

5.7 選擇特定列
5.7.1

mysql> SELECT name, birth FROM pet;
+----------+------------+
| name     | birth      |
+----------+------------+
| Fluffy   | 1993-02-04 |
| Claws    | 1994-03-17 |
| Buffy    | 1989-05-13 |
| Fang     | 1990-08-27 |
| Bowser   | 1989-08-31 |
| Chirpy   | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim     | 1996-04-29 |
| Puffball | 1999-03-30 |
+----------+------------+
9 rows in set (0.00 sec)


5.7.2

mysql> SELECT owner FROM pet;
+--------+
| owner  |
+--------+
| Harold |
| Gwen   |
| Harold |
| Benny  |
| Diane  |
| Gwen   |
| Gwen   |
| Benny  |
| Diane  |
+--------+
9 rows in set (0.00 sec)


5.7.3 過濾重複項,使重複項僅出現一次

mysql> SELECT DISTINCT owner FROM pet;
+--------+
| owner  |
+--------+
| Harold |
| Gwen   |
| Benny  |
| Diane  |
+--------+
4 rows in set (0.05 sec)


5.8 選擇特定的行與列

mysql> SELECT name, species, birth FROM pet
    -> WHERE species = 'dog' OR species = 'cat';
+--------+---------+------------+
| name   | species | birth      |
+--------+---------+------------+
| Fluffy | cat     | 1993-02-04 |
| Claws  | cat     | 1994-03-17 |
| Buffy  | dog     | 1989-05-13 |
| Fang   | dog     | 1990-08-27 |
| Bowser | dog     | 1989-08-31 |
+--------+---------+------------+
5 rows in set (0.00 sec)


5.9 對輸出結果進行排序

mysql> SELECT name, birth FROM pet ORDER BY birth;
+----------+------------+
| name     | birth      |
+----------+------------+
| Buffy    | 1989-05-13 |
| Bowser   | 1989-08-31 |
| Fang     | 1990-08-27 |
| Fluffy   | 1993-02-04 |
| Claws    | 1994-03-17 |
| Slim     | 1996-04-29 |
| Whistler | 1997-12-09 |
| Chirpy   | 1998-09-11 |
| Puffball | 1999-03-30 |
+----------+------------+
9 rows in set (0.00 sec)

 

5.10 使用DESC關鍵字進行降序排列

mysql> SELECT name, birth FROM pet ORDER BY birth DESC;
+----------+------------+
| name     | birth      |
+----------+------------+
| Puffball | 1999-03-30 |
| Chirpy   | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim     | 1996-04-29 |
| Claws    | 1994-03-17 |
| Fluffy   | 1993-02-04 |
| Fang     | 1990-08-27 |
| Bowser   | 1989-08-31 |
| Buffy    | 1989-05-13 |
+----------+------------+
9 rows in set (0.00 sec)

 

5.11 對多個列使用不一樣的排序順序

mysql> SELECT name, species, birth FROM pet ORDER BY species, birth DESC;
+----------+---------+------------+
| name     | species | birth      |
+----------+---------+------------+
| Chirpy   | bird    | 1998-09-11 |
| Whistler | bird    | 1997-12-09 |
| Claws    | cat     | 1994-03-17 |
| Fluffy   | cat     | 1993-02-04 |
| Fang     | dog     | 1990-08-27 |
| Bowser   | dog     | 1989-08-31 |
| Buffy    | dog     | 1989-05-13 |
| Puffball | hamster | 1999-03-30 |
| Slim     | snake   | 1996-04-29 |
+----------+---------+------------+
9 rows in set (0.00 sec)

 



六 經常使用的計算函數
6.1 TIMESTAMPDIFF()函數
6.1.1 TIMESTAMPDIFF()

mysql> SELECT name, birth, CURDATE(),
    -> TIMESTAMPDIFF(YEAR, birth, CURDATE()) AS age
    -> FROM pet;
+----------+------------+------------+------+
| name     | birth      | CURDATE()  | age  |
+----------+------------+------------+------+
| Fluffy   | 1993-02-04 | 2018-02-15 |   25 |
| Claws    | 1994-03-17 | 2018-02-15 |   23 |
| Buffy    | 1989-05-13 | 2018-02-15 |   28 |
| Fang     | 1990-08-27 | 2018-02-15 |   27 |
| Bowser   | 1989-08-31 | 2018-02-15 |   28 |
| Chirpy   | 1998-09-11 | 2018-02-15 |   19 |
| Whistler | 1997-12-09 | 2018-02-15 |   20 |
| Slim     | 1996-04-29 | 2018-02-15 |   21 |
| Puffball | 1999-03-30 | 2018-02-15 |   18 |
+----------+------------+------------+------+
9 rows in set (0.05 sec)


6.1.2 排序

mysql> SELECT name, birth, CURDATE(),
    -> TIMESTAMPDIFF(YEAR, birth, CURDATE()) AS age
    -> FROM pet ORDER BY name;
+----------+------------+------------+------+
| name     | birth      | CURDATE()  | age  |
+----------+------------+------------+------+
| Bowser   | 1989-08-31 | 2018-02-15 |   28 |
| Buffy    | 1989-05-13 | 2018-02-15 |   28 |
| Chirpy   | 1998-09-11 | 2018-02-15 |   19 |
| Claws    | 1994-03-17 | 2018-02-15 |   23 |
| Fang     | 1990-08-27 | 2018-02-15 |   27 |
| Fluffy   | 1993-02-04 | 2018-02-15 |   25 |
| Puffball | 1999-03-30 | 2018-02-15 |   18 |
| Slim     | 1996-04-29 | 2018-02-15 |   21 |
| Whistler | 1997-12-09 | 2018-02-15 |   20 |
+----------+------------+------------+------+
9 rows in set (0.00 sec)


6.1.3 計算壽命

mysql> SELECT name, birth, death,
    -> TIMESTAMPDIFF(YEAR, birth, death) AS age
    -> FROM pet WHERE death IS NOT NULL ORDER BY age;
+--------+------------+------------+------+
| name   | birth      | death      | age  |
+--------+------------+------------+------+
| Bowser | 1989-08-31 | 1995-07-29 |    5 |
+--------+------------+------------+------+
1 row in set (0.00 sec)


注意:NULL是一個特殊的值,判斷值不爲空(即不爲NULL)須要使用death IS NOT NULL,而不能使用death <> NULL。

 

6.2 MONTH()函數
6.2.1 MONTH()

mysql> SELECT name, birth, MONTH(birth) FROM pet;
+----------+------------+--------------+
| name     | birth      | MONTH(birth) |
+----------+------------+--------------+
| Fluffy   | 1993-02-04 |            2 |
| Claws    | 1994-03-17 |            3 |
| Buffy    | 1989-05-13 |            5 |
| Fang     | 1990-08-27 |            8 |
| Bowser   | 1989-08-31 |            8 |
| Chirpy   | 1998-09-11 |            9 |
| Whistler | 1997-12-09 |           12 |
| Slim     | 1996-04-29 |            4 |
| Puffball | 1999-03-30 |            3 |
+----------+------------+--------------+
9 rows in set (0.00 sec)

注意:受上面結果的啓發,YEAR(birth),DAYOFMONTH(birth)的結果是什麼呢?

6.2.2 找到5月出生的動物

mysql> SELECT name, birth FROM pet WHERE MONTH(birth) = 5;
+-------+------------+
| name  | birth      |
+-------+------------+
| Buffy | 1989-05-13 |
+-------+------------+
1 row in set (0.05 sec)

 


6.2.3 DATE_ADD()

mysql> SELECT name, birth FROM pet
    -> WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(), INTERVAL 1 MONTH));
+----------+------------+
| name     | birth      |
+----------+------------+
| Claws    | 1994-03-17 |
| Puffball | 1999-03-30 |
+----------+------------+
2 rows in set (0.08 sec)

 


6.3 如何處理爲空值

mysql> SELECT 1 IS NULL, 1 IS NOT NULL;
+-----------+---------------+
| 1 IS NULL | 1 IS NOT NULL |
+-----------+---------------+
|         0 |             1 |
+-----------+---------------+
1 row in set (0.00 sec)

 



七 模式匹配
7.1 匹配規則
_(下劃線)- 表示單個字符
%(百分號) - 表示任意個字符

注意:模式匹配時是大小寫敏感的,匹配使用的運算符是LIKE和NOT LIKE。不能使用數學運算符。

7.2 小例子
7.2.1 找到以字母b開頭的寵物

mysql> SELECT * FROM pet WHERE name LIKE 'b%';
+--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
| Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
2 rows in set (0.00 sec)

 

7.2.2 找到以fy結尾的寵物

mysql> SELECT * FROM pet WHERE name LIKE '%fy';
+--------+--------+---------+------+------------+-------+
| name   | owner  | species | sex  | birth      | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |
+--------+--------+---------+------+------------+-------+
2 rows in set (0.00 sec)


7.2.3 找到名字中含有字母w的寵物

mysql> SELECT * FROM pet WHERE name LIKE '%w%';
+----------+-------+---------+------+------------+------------+
| name     | owner | species | sex  | birth      | death      |
+----------+-------+---------+------+------------+------------+
| Claws    | Gwen  | cat     | m    | 1994-03-17 | NULL       |
| Bowser   | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL       |
+----------+-------+---------+------+------------+------------+
3 rows in set (0.00 sec)

 


7.2.4 找到名字爲5個字符的寵物

mysql> SELECT * FROM pet WHERE name LIKE '_____';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+
2 rows in set (0.00 sec)

 



八 行計數
8.1 COUNT()函數
8.1.1 共有多少寵物

mysql> SELECT COUNT(*) FROM pet;
+----------+
| COUNT(*) |
+----------+
|        9 |
+----------+
1 row in set (0.04 sec)

 


8.1.2 每一個主人共有幾隻寵物

mysql> SELECT owner, COUNT(*) FROM pet GROUP BY owner;
+--------+----------+
| owner  | COUNT(*) |
+--------+----------+
| Benny  |        2 |
| Diane  |        2 |
| Gwen   |        3 |
| Harold |        2 |
+--------+----------+
4 rows in set (0.05 sec)

 


8.1.3 每一個物種共有幾隻寵物

mysql> SELECT species, COUNT(*) FROM pet GROUP BY species;
+---------+----------+
| species | COUNT(*) |
+---------+----------+
| bird    |        2 |
| cat     |        2 |
| dog     |        3 |
| hamster |        1 |
| snake   |        1 |
+---------+----------+
5 rows in set (0.01 sec)

 

 

8.1.4 每種性別共有幾隻寵物

mysql> SELECT sex, COUNT(*) FROM pet GROUP BY sex;
+------+----------+
| sex  | COUNT(*) |
+------+----------+
| NULL |        1 |
| f    |        4 |
| m    |        4 |
+------+----------+
3 rows in set (0.00 sec)

 

8.1.5 每種物種和性別共有幾隻寵物

mysql> SELECT species, sex, COUNT(*) FROM pet GROUP BY species, sex;
+---------+------+----------+
| species | sex  | COUNT(*) |
+---------+------+----------+
| bird    | NULL |        1 |
| bird    | f    |        1 |
| cat     | f    |        1 |
| cat     | m    |        1 |
| dog     | f    |        1 |
| dog     | m    |        2 |
| hamster | f    |        1 |
| snake   | m    |        1 |
+---------+------+----------+
8 rows in set (0.00 sec)

 


8.1.6 只考慮狗和貓的狀況下,每種物種和性別共有幾隻寵物

mysql> SELECT species, sex, COUNT(*) FROM pet
    -> WHERE species = 'dog' OR species = 'cat'
    -> GROUP BY species, sex;
+---------+------+----------+
| species | sex  | COUNT(*) |
+---------+------+----------+
| cat     | f    |        1 |
| cat     | m    |        1 |
| dog     | f    |        1 |
| dog     | m    |        2 |
+---------+------+----------+
4 rows in set (0.06 sec)

 


8.1.7 排除性別未知的寵物狀況下,每種物種和性別共有幾隻寵物

mysql> SELECT species, sex, COUNT(*) FROM pet
    -> WHERE sex IS NOT NULL
    -> GROUP BY species, sex;
+---------+------+----------+
| species | sex  | COUNT(*) |
+---------+------+----------+
| bird    | f    |        1 |
| cat     | f    |        1 |
| cat     | m    |        1 |
| dog     | f    |        1 |
| dog     | m    |        2 |
| hamster | f    |        1 |
| snake   | m    |        1 |
+---------+------+----------+
7 rows in set (0.00 sec)

 



九 使用多個表
9.1 建立一個event表以記錄寵物的其餘信息

 

mysql> CREATE TABLE event                                                           -> (
    -> name VARCHAR(20),
    -> date DATE,
    -> type VARCHAR(15),
    -> remark VARCHAR(255)
    -> );
Query OK, 0 rows affected (0.49 sec)

 


9.2 往表中插入數據

INSERT INTO event
VALUES ('Fluffy', '1995-05-15', 'litter', '4 kittens, 3 female, 1 male');

INSERT INTO event
VALUES ('Buffy', '1993-06-23', 'litter', '5 puppies, 2 female, 3 male');

INSERT INTO event
VALUES ('Buffy', '1994-06-19', 'litter', '3 puppies, 3 female');

INSERT INTO event
VALUES ('Chirpy', '1999-03-21', 'vet', 'needed beak straightened');

INSERT INTO event
VALUES ('Slim', '1997-08-03', 'vet', 'broken rib');

INSERT INTO event
VALUES ('Bowser', '1991-10-12', 'kennel', NULL);

INSERT INTO event
VALUES ('Fang', '1991-10-12', 'kennel', NULL);

INSERT INTO event
VALUES ('Fang', '1998-08-28', 'birthday', 'Gave him a new chew toy');

INSERT INTO event
VALUES ('Claws', '1998-03-17', 'birthday', 'Gave him a new flea collar');

INSERT INTO event
VALUES ('Whistler', '1998-12-09', 'birthday', 'First birthday');

 



9.3 多表鏈接
9.3.1

mysql> SELECT pet.name
    -> ,
    -> (YEAR(date)-YEAR(birth)) - (RIGHT(date, 5) < RIGHT(birth, 5)) AS age,
    -> remark
    -> FROM pet INNER JOIN event
    -> ON pet.name = event.name
    -> WHERE event.type = 'litter';
+--------+------+-----------------------------+
| name   | age  | remark                      |
+--------+------+-----------------------------+
| Fluffy |    2 | 4 kittens, 3 female, 1 male |
| Buffy  |    4 | 5 puppies, 2 female, 3 male |
| Buffy  |    5 | 3 puppies, 3 female         |
+--------+------+-----------------------------+
3 rows in set (0.16 sec)

 


9.3.2

mysql> SELECT p1.name, p1.sex, p2.name, p2.sex, p1.species
    -> FROM pet AS p1 INNER JOIN pet AS p2
    -> ON p1.species = p2.species AND p1.sex = 'f' AND p2.sex = 'm';
+--------+------+--------+------+---------+
| name   | sex  | name   | sex  | species |
+--------+------+--------+------+---------+
| Fluffy | f    | Claws  | m    | cat     |
| Buffy  | f    | Fang   | m    | dog     |
| Buffy  | f    | Bowser | m    | dog     |
+--------+------+--------+------+---------+
3 rows in set (0.00 sec)

 



十 獲取數據庫信息
10.1

語句 功能
SHOW DATABASES; 顯示MySQL Server所管理的數據庫
SELECT DATABASE(); 當前選擇的是哪一個數據庫
SHOW TABLES; 當前數據庫中有哪些表
DESCRIBE pet; 顯示pet表的表結構
相關文章
相關標籤/搜索