本文主要記錄一些常見的mysql知識, 能夠理解爲mysql掃碼貼。java
瞭解mysqlnode
數據庫: 一個保存着有組織的數據的容器。
數據庫軟件: DBMS, 數據庫是經過DBMS來操做的容器。
表: 某中特定數據類型的結構化清單
模式(schema): 關於數據庫和表的佈局以及特性的相關信息
列(column): 表由列組成, 列中保存着表中某部分信息, 每一列都有相應的數據類型
數據類型: 所允許的數據類型, 它限制該列所存儲的數據結構
行(row/record): 表中的一行記錄
主鍵(primary key): 惟一標識表中每行的這個列(這組列), 每一個表都應該擁有一個主鍵
複製代碼
知足主鍵的兩個條件:
1. 任意兩行都不會具備相同的主鍵值
2. 每行都要有一個主鍵值(不能爲空)
複製代碼
像mysql, oracle, sql server等數據庫都是基於客戶機-服務器的數控庫。 服務器部分是負責全部數據的訪問與處理, 通常是安裝在數據庫服務器上, 客戶機則是與用戶打交道的軟件。python
以mysql爲例:
服務器軟件: mysql DBMS
客戶機: 支持mysql的工具, 如java,node,python等編程語言
複製代碼
使用mysqlmysql
鏈接數據庫, 須要主機名,端口,一個合法的用戶名, 或者用戶口令。sql
// 以本地鏈接爲例
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語句bash
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)
複製代碼
過濾數據
經過select語句配合where子句進行數據過濾。
mysql> select name from person where sid > 4 order by name desc;
+-------+
| name |
+-------+
| sssw |
| se |
| jjs |
| jams |
| james |
| james |
| ass |
+-------+
7 rows in set (0.00 sec)
複製代碼
關於where子句的位置: 在同時使用where和order by子句時候, 咱們應該讓order by位於where 子句以後。
mysql> select name from person where sid <> 6 and name = 'kobe';
+------+
| name |
+------+
| kobe |
+------+
1 row in set (0.01 sec)
複製代碼
範圍值檢測 => between 5 and 10;表示5到10之間。
mysql> select * from person where sid between 5 and 7;
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 5 | se | 1 | 2013-09-12 |
| 6 | james | 1 | 2015-09-12 |
| 7 | sssw | 1 | 2013-09-12 |
+-----+-------+-----+------------+
3 rows in set (0.00 sec)
複製代碼
空值檢測 => is null
mysql> select * from person where sid is null;
Empty set (0.00 sec)
複製代碼
數據過濾
咱們能夠經過操做符來組合多個where子句。
mysql> select * from person where sex = 1 and sid <= 4;
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 1 | kobe | 1 | 2018-11-27 |
| 2 | wade | 1 | 2013-09-12 |
| 3 | cup | 1 | 2003-09-12 |
| 4 | james | 1 | 2013-09-12 |
+-----+-------+-----+------------+
4 rows in set (0.00 sec)
複製代碼
mysql> select * from person where sex = 1 and sid <= 4 and name = 'kobe';
+-----+------+-----+------------+
| sid | name | sex | birth |
+-----+------+-----+------------+
| 1 | kobe | 1 | 2018-11-27 |
+-----+------+-----+------------+
1 row in set (0.00 sec)
複製代碼
mysql> select * from person where sid <= 4 or sid >= 8;
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 1 | kobe | 1 | 2018-11-27 |
| 2 | wade | 1 | 2013-09-12 |
| 3 | cup | 1 | 2003-09-12 |
| 4 | james | 1 | 2013-09-12 |
| 8 | jjs | 1 | 2013-09-23 |
| 9 | ass | 1 | 2013-09-12 |
| 10 | james | 1 | 2013-11-12 |
| 11 | jams | 1 | 2011-09-12 |
+-----+-------+-----+------------+
8 rows in set (0.00 sec)
複製代碼
where能夠經過若干個and或者or進行鏈接, 這個時候咱們須要注意他們組合順序, 咱們能夠使用()明確的分組操做符組合。
mysql> select * from person where (sid = 4 or sid = 9) or name = 'james';
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 4 | james | 1 | 2013-09-12 |
| 6 | james | 1 | 2015-09-12 |
| 9 | ass | 1 | 2013-09-12 |
| 10 | james | 1 | 2013-11-12 |
+-----+-------+-----+------------+
4 rows in set (0.00 sec)
複製代碼
in操做符能夠用於指定操做範圍,範圍內每一個條件均可以進行匹配。
mysql> select * from person where name in ('james', 'ass') order by name desc;
+-----+-------+-----+------------+
| sid | name | sex | birth |
+-----+-------+-----+------------+
| 4 | james | 1 | 2013-09-12 |
| 6 | james | 1 | 2015-09-12 |
| 10 | james | 1 | 2013-11-12 |
| 9 | ass | 1 | 2013-09-12 |
+-----+-------+-----+------------+
4 rows in set (0.00 sec)
複製代碼
使用in操做符有哪些優點:
1. 使用長的合法選項清單時候, in操做符比較直觀。
2. in操做符計算的次序比較好管理
3. in操做符通常比or操做符效率快
4. in操做符能夠包括其餘select語句,可以更加動態的建立where子句
複製代碼
not操做符只有一個優勢, 就是否認它後面的任何條件。
mysql> select * from person where name not in ('james', 'ass');
+-----+------+-----+------------+
| sid | name | sex | birth |
+-----+------+-----+------------+
| 1 | kobe | 1 | 2018-11-27 |
| 2 | wade | 1 | 2013-09-12 |
| 3 | cup | 1 | 2003-09-12 |
| 5 | se | 1 | 2013-09-12 |
| 7 | sssw | 1 | 2013-09-12 |
| 8 | jjs | 1 | 2013-09-23 |
| 11 | jams | 1 | 2011-09-12 |
+-----+------+-----+------------+
7 rows in set (0.00 sec)
複製代碼
mysql支持not對in, between, exists子句取反。
用通配符進行過濾
通配符: 用來匹配值的一部分的特殊符號。
搜索模式: 由字面值, 通配符或者二者組合成的搜索條件。
在使用通配符時候咱們須要使用like操做符。
sql支持如下幾種通配符:
%符表示任何字符出現任意次數。
mysql> select name from person where name like 's%';
+------+
| name |
+------+
| se |
| sssw |
+------+
2 rows in set (0.00 sec)
複製代碼
能夠同時使用多個通配符。
mysql> select name from person where name like 'j%m%';
+-------+
| name |
+-------+
| james |
| james |
| james |
| jams |
+-------+
4 rows in set (0.00 sec)
複製代碼
%表示匹配0個,1個,多個字符,可是%號不能匹配null。
下劃線只能匹配單個字符, 不能多也不能少。
mysql> select name from person where name like '_ams';
+------+
| name |
+------+
| jams |
+------+
1 row in set (0.00 sec)
複製代碼
使用通配符的一些技巧:
1. 不過分使用通配符
2. 確實須要使用通配符時候, 不要將它們置於開始處, 把通配符置於搜索模式的開始處是最慢的。
3. 多個通配符使用時候格外注意它們的位置
複製代碼
note: 明天繼續更新, 晚安。