必須懂的mysql知識
目的
本文主要記錄一些常見的mysql知識, 可以理解為mysql掃碼貼。
瞭解mysql
資料庫: 一個儲存著有組織的資料的容器。
資料庫軟體: DBMS, 資料庫是通過DBMS來操作的容器。
表: 某中特定資料型別的結構化清單
模式(schema): 關於資料庫和表的佈局以及特性的相關資訊
列(column): 表由列組成, 列中儲存著表中某部分資訊, 每一列都有相應的資料型別
資料型別: 所容許的資料型別, 它限制該列所儲存的資料結構
行(row/record): 表中的一行記錄
主鍵(primary key): 唯一標識表中每行的這個列(這組列), 每個表都應該擁有一個主鍵
複製程式碼
滿足主鍵的兩個條件:
1. 任意兩行都不會具有相同的主鍵值
2. 每行都要有一個主鍵值(不能為空)
複製程式碼
像mysql, oracle, sql server等資料庫都是基於客戶機-伺服器的數控庫。 伺服器部分是負責所有資料的訪問與處理, 一般是安裝在資料庫伺服器上, 客戶機則是與使用者打交道的軟體。
以mysql為例:
伺服器軟體: mysql DBMS
客戶機: 支援mysql的工具, 如java,node,python等程式語言
複製程式碼
使用mysql
連線資料庫, 需要主機名,埠,一個合法的使用者名稱, 或者使用者口令。
// 以本地連線為例
mysql -u root -p
複製程式碼
選擇資料庫使用USE關鍵字。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| s3 |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use s3;
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
複製程式碼
檢視一個數據庫中的列表: show tables;
mysql> show tables;
+--------------+
| Tables_in_s3 |
+--------------+
| person |
+--------------+
1 row in set (0.00 sec)
複製程式碼
查看錶中的列: show columns from dbName;
mysql> show columns from person;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| sid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| sex | int(11) | NO | | NULL | |
| birth | date | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
複製程式碼
上述檢視的方式還可以使用describe語句
mysql> describe person;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| sid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| sex | int(11) | NO | | NULL | |
| birth | date | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
複製程式碼
show status: 檢視伺服器狀態資訊。
show errors: 顯示伺服器錯誤資訊
show warnings: 顯示伺服器警告資訊
show grants: 顯示授予使用者的安全許可權
複製程式碼
檢視建立表的sql語句: show create table tableName;
mysql> show create table person;
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| person | CREATE TABLE `person` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`sex` int(11) NOT NULL,
`birth` date DEFAULT NULL,
PRIMARY KEY (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
複製程式碼
檢視建立資料庫的語句: show create database dbName;
mysql> show create database s3;
+----------+-------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------+
| s3 | CREATE DATABASE `s3` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-------------------------------------------------------------+
1 row in set (0.00 sec)
複製程式碼
檢索資料
主要介紹使用select語句從資料庫中檢索一個或者多個數據列。
- 檢索單個列
mysql> select name from person;
+-------+
| name |
+-------+
| kobe |
| james |
| james |
| james |
| james |
| james |
| james |
| james |
| james |
| james |
| james |
+-------+
11 rows in set (0.00 sec)
複製程式碼
- 檢索多個列
檢索多個列, 列名之間使用逗號隔開。
mysql> select name, sex from person;
+-------+-----+
| name | sex |
+-------+-----+
| kobe | 1 |
| james | 1 |
| james | 1 |
| james | 1 |
| james | 1 |
| james | 1 |
| james | 1 |
| james | 1 |
| james | 1 |
| james | 1 |
| james | 1 |
+-------+-----+
11 rows in set (0.00 sec)
複製程式碼
- 檢索所有列
使用萬用字元*來進行。
mysql> select * from person;
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 1 | kobe | 1 | 2018-11-27 |
| 2 | james | 1 | 2013-09-12 |
| 3 | james | 1 | 2013-09-12 |
| 4 | james | 1 | 2013-09-12 |
| 5 | james | 1 | 2013-09-12 |
| 6 | james | 1 | 2013-09-12 |
| 7 | james | 1 | 2013-09-12 |
| 8 | james | 1 | 2013-09-12 |
| 9 | james | 1 | 2013-09-12 |
| 10 | james | 1 | 2013-09-12 |
| 11 | james | 1 | 2013-09-12 |
+-----+-------+-----+------------+
11 rows in set (0.00 sec)
複製程式碼
- 檢索不同的值
如果檢索資料時候不想要出現重複的結果,可以使用distinct關鍵字. distinct關鍵字需要放在所有列的前面, 因此不能部分使用distinct。
mysql> select distinct name from person;
+-------+
| name |
+-------+
| kobe |
| james |
+-------+
2 rows in set (0.00 sec)
複製程式碼
- 限制結果
使用limit(不多於)關鍵字限制檢索出來的數量。
mysql> select * from person limit 5;
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 1 | kobe | 1 | 2018-11-27 |
| 2 | james | 1 | 2013-09-12 |
| 3 | james | 1 | 2013-09-12 |
| 4 | james | 1 | 2013-09-12 |
| 5 | james | 1 | 2013-09-12 |
+-----+-------+-----+------------+
複製程式碼
limit後面可以跟兩個值, 第一個為起始位置, 第二個是要檢索的行數。
mysql> select * from person limit 5, 5;
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 6 | james | 1 | 2013-09-12 |
| 7 | james | 1 | 2013-09-12 |
| 8 | james | 1 | 2013-09-12 |
| 9 | james | 1 | 2013-09-12 |
| 10 | james | 1 | 2013-09-12 |
+-----+-------+-----+------------+
5 rows in set (0.00 sec)
複製程式碼
mysql> select * from person limit 0,1;
+-----+------+-----+------------+
| sid | name | sex | birth |
+-----+------+-----+------------+
| 1 | kobe | 1 | 2018-11-27 |
+-----+------+-----+------------+
1 row in set (0.00 sec)
複製程式碼
limit可以配合offset使用, 如limit 4 offset 3(從行3開始後的四行)。
mysql> select * from person limit 3,4
-> ;
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 4 | james | 1 | 2013-09-12 |
| 5 | james | 1 | 2013-09-12 |
| 6 | james | 1 | 2013-09-12 |
| 7 | james | 1 | 2013-09-12 |
+-----+-------+-----+------------+
4 rows in set (0.00 sec)
mysql> select * from person limit 4 offset 3;
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 4 | james | 1 | 2013-09-12 |
| 5 | james | 1 | 2013-09-12 |
| 6 | james | 1 | 2013-09-12 |
| 7 | james | 1 | 2013-09-12 |
+-----+-------+-----+------------+
4 rows in set (0.00 sec)
複製程式碼
- 使用完全限定的表名
mysql> select distinct person.name from s3.person;
+-------+
| name |
+-------+
| kobe |
| james |
+-------+
2 rows in set (0.00 sec)
複製程式碼
排序檢索資料
講述使用select語句的order by子句,來對檢索出來的資料進行排序。 order by子句可以取一個或者多個列的名字。
- 普通的排序
mysql> select * from person;
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 1 | kobe | 1 | 2018-11-27 |
| 2 | wade | 1 | 2013-09-12 |
| 3 | cup | 1 | 2013-09-12 |
| 4 | james | 1 | 2013-09-12 |
| 5 | se | 1 | 2013-09-12 |
| 6 | james | 1 | 2013-09-12 |
| 7 | sssw | 1 | 2013-09-12 |
| 8 | jjs | 1 | 2013-09-12 |
| 9 | ass | 1 | 2013-09-12 |
| 10 | james | 1 | 2013-09-12 |
| 11 | jams | 1 | 2013-09-12 |
+-----+-------+-----+------------+
11 rows in set (0.00 sec)
mysql> select * from person order by name;
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 9 | ass | 1 | 2013-09-12 |
| 3 | cup | 1 | 2013-09-12 |
| 4 | james | 1 | 2013-09-12 |
| 6 | james | 1 | 2013-09-12 |
| 10 | james | 1 | 2013-09-12 |
| 11 | jams | 1 | 2013-09-12 |
| 8 | jjs | 1 | 2013-09-12 |
| 1 | kobe | 1 | 2018-11-27 |
| 5 | se | 1 | 2013-09-12 |
| 7 | sssw | 1 | 2013-09-12 |
| 2 | wade | 1 | 2013-09-12 |
+-----+-------+-----+------------+
11 rows in set (0.00 sec)
複製程式碼
- 按照多個列進行排序
下面這個例子標示僅在相同的name時候才對birth欄位進行排序。
mysql> select * from person order by name, birth;
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 9 | ass | 1 | 2013-09-12 |
| 3 | cup | 1 | 2003-09-12 |
| 4 | james | 1 | 2013-09-12 |
| 10 | james | 1 | 2013-11-12 |
| 6 | james | 1 | 2015-09-12 |
| 11 | jams | 1 | 2011-09-12 |
| 8 | jjs | 1 | 2013-09-23 |
| 1 | kobe | 1 | 2018-11-27 |
| 5 | se | 1 | 2013-09-12 |
| 7 | sssw | 1 | 2013-09-12 |
| 2 | wade | 1 | 2013-09-12 |
+-----+-------+-----+------------+
11 rows in set (0.00 sec)
複製程式碼
- 指定排序方向
資料排序不限於升序(a-z), 升序是預設的排序方式, 同樣我們也可以通過指定desc關鍵字來表示降序。
mysql> select name from person order by name desc;
+-------+
| name |
+-------+
| wade |
| sssw |
| se |
| kobe |
| jjs |
| jams |
| james |
| james |
| james |
| cup |
| ass |
+-------+
11 rows in set (0.00 sec)
複製程式碼
mysql> select * from person order by name desc, birth;
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 2 | wade | 1 | 2013-09-12 |
| 7 | sssw | 1 | 2013-09-12 |
| 5 | se | 1 | 2013-09-12 |
| 1 | kobe | 1 | 2018-11-27 |
| 8 | jjs | 1 | 2013-09-23 |
| 11 | jams | 1 | 2011-09-12 |
| 4 | james | 1 | 2013-09-12 |
| 10 | james | 1 | 2013-11-12 |
| 6 | james | 1 | 2015-09-12 |
| 3 | cup | 1 | 2003-09-12 |
| 9 | ass | 1 | 2013-09-12 |
+-----+-------+-----+------------+
11 rows in set (0.00 sec)
複製程式碼
desc關鍵字只能應用到它前面到列名。如果你想對多個列進行降序排序那麼你需要每個列指定desc。
order by 與 limit的順序問題: limit子句要放在order by 後面。
mysql> select name from person order by name desc limit 4;
+------+
| name |
+------+
| wade |
| sssw |
| se |
| kobe |
+------+
4 rows in set (0.00 sec)
``
note: 明天繼續更新。。。。複製程式碼