MySQL數據類型以及基本使用詳解


                      MySQL數據類型以及基本使用詳解

                                             作者:尹正傑

版權聲明:原創作品,謝絕轉載!否則將追究法律責任。

 

 

一.MySQL服務器的主要組件

  我們知道MySQL的主要組件主要是由服務端(mysqld)和客戶端(mysql)組成的。它們都共用一個配置文件(通常叫做my.cnf),這個配置文件很獨特,它需要使用中括號括起來標明是為哪種組件使用的,例如[mysql]下面的指令就表示為客戶端配置的參數,如果[mysqld]下面的指令就表示為服務端配置的參數。其實MySQL的客戶端組件有很多個,本篇博客是用的mysql命令只是MySQL客戶端之一,其他常見的客戶端如:Navicat for Mysql,EMSSQL ManagerforMySQL等等。

  在Linux操作系統中我還是推薦使用mysql客戶端的,如果在windows的話我推薦使用Navicat for Mysql,mysql在Linux連接服務器是很簡單,我們用mysql連接數據庫的時候通常會用到三個參數,分別是“-u(指定登錄數據庫的用戶名)”,“-h(指定登錄數據庫的主機名)”,“-p(指定登錄數據庫的密碼)”,“-e(指定SQL命令即可執行)”等等。

 

二.MySQL用戶的分類

  其實在MySQL初始化時,它會默認創建兩類賬戶,一種是管理員用戶(root),另外一種是匿名用戶。

1 root:                  //管理員用戶
2     127.0.0.1          //IPV4本地回環地址
3     localhost          //本機的主機名
4     Hostname         //主機名
5     ::1                //IPV6本地回環地址
6 " ":                   //匿名用戶
7     localhost        
8     hostname

  這兩類用戶是系統默認的,當然我們不推薦直接使用它們。因為它們很不安全(只要看了我這篇博客的人都能通過這種方式去連接你的MySQL,因為他們都知道mysql的用戶信息。),第一,匿名用戶訪問無法記錄來訪者,誰都可以使用,就類似於FTP服務器。第二,管理員用戶默認是沒有密碼的,這就更不全啦,就好像你的辦公電腦沒有設置密碼你很沒有安全感似的。因此,我們需要刪除不必要的用戶,比如MySQL服務默認的2個匿名用戶,給我們需要留存的用戶設置密碼[“SET PASSWORD FOR 'username'@'hostname/IP' = PASSWORD('yinzhengjie');”]。

 

三.MySQL客戶端

1.客戶端命令

  客戶端命令表示是在客戶端執行的命令,我們只需要成功連接數據庫之后輸入“help”,就可以看到mysql客戶端的命令啦。客戶端的命令有個特點就是不需要命令結束符,比如“;”。

 1 mysql> help
 2 
 3 For information about MySQL products and services, visit:
 4    http://www.mysql.com/
 5 For developer information, including the MySQL Reference Manual, visit:
 6    http://dev.mysql.com/
 7 To buy MySQL Enterprise support, training, or other products, visit:
 8    https://shop.mysql.com/
 9 
10 List of all MySQL commands:
11 Note that all text commands must be first on line and end with ';'
12 ?         (\?) Synonym for `help'.
13 clear     (\c) Clear the current input statement.
14 connect   (\r) Reconnect to the server. Optional arguments are db and host.
15 delimiter (\d) Set statement delimiter.
16 edit      (\e) Edit command with $EDITOR.
17 ego       (\G) Send command to mysql server, display result vertically.
18 exit      (\q) Exit mysql. Same as quit.
19 go        (\g) Send command to mysql server.
20 help      (\h) Display this help.
21 nopager   (\n) Disable pager, print to stdout.
22 notee     (\t) Don't write into outfile.
23 pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
24 print     (\p) Print current command.
25 prompt    (\R) Change your mysql prompt.
26 quit      (\q) Quit mysql.
27 rehash    (\#) Rebuild completion hash.
28 source    (\.) Execute an SQL script file. Takes a file name as an argument.
29 status    (\s) Get status information from the server.
30 system    (\!) Execute a system shell command.
31 tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
32 use       (\u) Use another database. Takes database name as argument.
33 charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
34 warnings  (\W) Show warnings after every statement.
35 nowarning (\w) Don't show warnings after every statement.
36 
37 For server side help, type 'help contents'
38 
39 mysql> 
 1 mysql> status
 2 --------------
 3 mysql  Ver 14.14 Distrib 5.5.54, for linux2.6 (x86_64) using readline 5.1
 4 
 5 Connection id:          4
 6 Current database:
 7 Current user:           root@localhost
 8 SSL:                    Not in use
 9 Current pager:          stdout
10 Using outfile:          ''
11 Using delimiter:        ;
12 Server version:         5.5.54-log MySQL Community Server (GPL)
13 Protocol version:       10
14 Connection:             Localhost via UNIX socket
15 Server characterset:    latin1
16 Db     characterset:    latin1
17 Client characterset:    utf8
18 Conn.  characterset:    utf8
19 UNIX socket:            /tmp/mysql.sock
20 Uptime:                 49 min 43 sec
21 
22 Threads: 1  Questions: 11  Slow queries: 0  Opens: 33  Flush tables: 1  Open tables: 26  Queries per second avg: 0.003
23 --------------
24 
25 mysql> 
stsauts   #查詢運行狀態
 1 [root@yinzhengjie ~]# mysql -u root -p123
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 6
 4 Server version: 5.5.54-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> \q
15 Bye
16 [root@yinzhengjie ~]# 
\q        #退出當前數據庫
 1 [root@yinzhengjie ~]# mysql -u root -p123
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 7
 4 Server version: 5.5.54-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> \?
15 
16 For information about MySQL products and services, visit:
17    http://www.mysql.com/
18 For developer information, including the MySQL Reference Manual, visit:
19    http://dev.mysql.com/
20 To buy MySQL Enterprise support, training, or other products, visit:
21    https://shop.mysql.com/
22 
23 List of all MySQL commands:
24 Note that all text commands must be first on line and end with ';'
25 ?         (\?) Synonym for `help'.
26 clear     (\c) Clear the current input statement.
27 connect   (\r) Reconnect to the server. Optional arguments are db and host.
28 delimiter (\d) Set statement delimiter.
29 edit      (\e) Edit command with $EDITOR.
30 ego       (\G) Send command to mysql server, display result vertically.
31 exit      (\q) Exit mysql. Same as quit.
32 go        (\g) Send command to mysql server.
33 help      (\h) Display this help.
34 nopager   (\n) Disable pager, print to stdout.
35 notee     (\t) Don't write into outfile.
36 pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
37 print     (\p) Print current command.
38 prompt    (\R) Change your mysql prompt.
39 quit      (\q) Quit mysql.
40 rehash    (\#) Rebuild completion hash.
41 source    (\.) Execute an SQL script file. Takes a file name as an argument.
42 status    (\s) Get status information from the server.
43 system    (\!) Execute a system shell command.
44 tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
45 use       (\u) Use another database. Takes database name as argument.
46 charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
47 warnings  (\W) Show warnings after every statement.
48 nowarning (\w) Don't show warnings after every statement.
49 
50 For server side help, type 'help contents'
51 
52 mysql> 
\?        #獲取幫助信息,相當於help指令。
 1 [root@yinzhengjie ~]# mysql -u root -p123
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 8
 4 Server version: 5.5.54-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> 
15 mysql> select user()\g
16 +----------------+
17 | user()         |
18 +----------------+
19 | root@localhost |
20 +----------------+
21 1 row in set (0.00 sec)
22 
23 mysql> 
\g        #直接將命令送到服務器端執行,這種情況適用於你不知道當前數據庫的命令結束符是什么,換句話說,默認的命令結束符“;”被修改的情況下可以使用它
 1 [root@yinzhengjie ~]# mysql -u root -p123
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 10
 4 Server version: 5.5.54-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> use mysql
15 Database changed
16 mysql> select User,Host,Password from user;
17 +------+-------------+-------------------------------------------+
18 | User | Host        | Password                                  |
19 +------+-------------+-------------------------------------------+
20 | root | localhost   | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
21 | root | yinzhengjie | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
22 | root | 127.0.0.1   | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
23 | root | ::1         | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
24 |      | localhost   |                                           |
25 |      | yinzhengjie |                                           |
26 +------+-------------+-------------------------------------------+
27 6 rows in set (0.00 sec)
28 
29 mysql> 
30 mysql> select User,Host,Password from user\G
31 *************************** 1. row ***************************
32     User: root
33     Host: localhost
34 Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
35 *************************** 2. row ***************************
36     User: root
37     Host: yinzhengjie
38 Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
39 *************************** 3. row ***************************
40     User: root
41     Host: 127.0.0.1
42 Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
43 *************************** 4. row ***************************
44     User: root
45     Host: ::1
46 Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
47 *************************** 5. row ***************************
48     User: 
49     Host: localhost
50 Password: 
51 *************************** 6. row ***************************
52     User: 
53     Host: yinzhengjie
54 Password: 
55 6 rows in set (0.00 sec)
56 
57 mysql> 
\G     #不再顯示為表格顯示,而是以列的方式顯示,這種情況適合在默認的表格顯示不下時(也就是說,字段量非常大時),這個參數就很有用喲。
 1 [root@yinzhengjie ~]# mysql -u root -p123
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 11
 4 Server version: 5.5.54-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> select \c
15 mysql> 
16 mysql> show databases;
17 +--------------------+
18 | Database           |
19 +--------------------+
20 | information_schema |
21 | mysql              |
22 | performance_schema |
23 | test               |
24 +--------------------+
25 4 rows in set (0.01 sec)
26 
27 mysql> 
28 mysql> sdas sad as \c
29 mysql> 
30 mysql> 
\c     #取消命令的執行,它很適合當你不想繼續執行該行代碼時使用,相當於Linux系統的ctrl+c喲。
 1 [root@yinzhengjie ~]# mysql -u root -p123
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 16
 4 Server version: 5.5.54-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> show databases;
15 +--------------------+
16 | Database           |
17 +--------------------+
18 | information_schema |
19 | mysql              |
20 | performance_schema |
21 | test               |
22 +--------------------+
23 4 rows in set (0.00 sec)
24 
25 mysql> \umysql
26 Database changed
27 mysql> 
28 mysql> use mysql
29 Database changed
30 mysql> 
\u #使用默認的數據庫,你可以理解稱切換數據庫目錄,相當於Linux的cd命令喲

 

2.服務器端命令

  服務器端命令簡直就時多了去了,比客戶端的也復雜的多,因此我在這里也不打算一一舉例,而是用一些常用的命令進行掃盲模式。執行服務端的命令需要語句終止符,通常默認為分號(;),當然這個命令結束符(;)是可以被修改的。

a>.SELECT命令的基本使用

  MySQL服務器端有很多內建函數( 簡稱BIF)。使用select命令去執行一個內建函數,並將該內建函數的執行結果返回給當前用戶。

 1 mysql> select user();            #查看數據庫的用戶信息。
 2 +----------------+
 3 | user()         |
 4 +----------------+
 5 | root@localhost |
 6 +----------------+
 7 1 row in set (0.02 sec)
 8 
 9 mysql> 
10 mysql> select current_time();      #查看當前操作系統的時間。
11 +----------------+
12 | current_time() |
13 +----------------+
14 | 00:25:33       |
15 +----------------+
16 1 row in set (0.00 sec)
17 
18 mysql>
select                 #執行內建函數案例
 1 mysql> select 100+200;        #利用select做算術運算。
 2 +---------+
 3 | 100+200 |
 4 +---------+
 5 |     300 |
 6 +---------+
 7 1 row in set (0.00 sec)
 8 
 9 mysql>
10 mysql> select 5*6;
11 +-----+
12 | 5*6 |
13 +-----+
14 |  30 |
15 +-----+
16 1 row in set (0.00 sec)
17 
18 mysql> 
select                 #可用於算術運算的案例

 b>.SHOW命令的基本使用

 1 mysql> show databases;
 2 +--------------------+
 3 | Database           |
 4 +--------------------+
 5 | information_schema |
 6 | mysql              |
 7 | performance_schema |
 8 | test               |
 9 +--------------------+
10 4 rows in set (0.00 sec)
11 
12 mysql> 
SHOW DATABASES            #顯示自己有權限可以查看所有庫
 1 mysql> use mysql      
 2 Database changed
 3 mysql> show tables;
 4 +---------------------------+
 5 | Tables_in_mysql           |
 6 +---------------------------+
 7 | columns_priv              |
 8 | db                        |
 9 | event                     |
10 | func                      |
11 | general_log               |
12 | help_category             |
13 | help_keyword              |
14 | help_relation             |
15 | help_topic                |
16 | host                      |
17 | ndb_binlog_index          |
18 | plugin                    |
19 | proc                      |
20 | procs_priv                |
21 | proxies_priv              |
22 | servers                   |
23 | slow_log                  |
24 | tables_priv               |
25 | time_zone                 |
26 | time_zone_leap_second     |
27 | time_zone_name            |
28 | time_zone_transition      |
29 | time_zone_transition_type |
30 | user                      |
31 +---------------------------+
32 24 rows in set (0.00 sec)
33 
34 mysql> 
SHOW TABLES              #顯示某庫的所有表
 c>.CRETARE命令的基本使用
 1 [root@yinzhengjie ~]# mysql -u root -p123
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 17
 4 Server version: 5.5.54-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> show databases;
15 +--------------------+
16 | Database           |
17 +--------------------+
18 | information_schema |
19 | mysql              |
20 | performance_schema |
21 | test               |
22 +--------------------+
23 4 rows in set (0.00 sec)
24 
25 mysql> create database yinzhengjie;
26 Query OK, 1 row affected (0.01 sec)
27 
28 mysql> show databases;
29 +--------------------+
30 | Database           |
31 +--------------------+
32 | information_schema |
33 | mysql              |
34 | performance_schema |
35 | test               |
36 | yinzhengjie        |
37 +--------------------+
38 5 rows in set (0.00 sec)
39 
40 mysql> 
CRETARE DATABASE database_name  #創建數據庫,相當於Linux的mkdir命令喲~因此,你可以去你的數據庫目錄去創建目錄,不過要注意的是,你創建的目錄權限要進行修改喲,如果你用root創建的目錄,在數據庫中mysql用戶可能無權操作該目錄喲
d>.DROP命令的基本使用
 1 [root@yinzhengjie ~]# mysql -u root -p123
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 18
 4 Server version: 5.5.54-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> show databases;
15 +--------------------+
16 | Database           |
17 +--------------------+
18 | information_schema |
19 | mysql              |
20 | performance_schema |
21 | test               |
22 | yinzhengjie        |
23 +--------------------+
24 5 rows in set (0.00 sec)
25 
26 mysql> drop database yinzhengjie;
27 Query OK, 0 rows affected (0.01 sec)
28 
29 mysql> show databases;
30 +--------------------+
31 | Database           |
32 +--------------------+
33 | information_schema |
34 | mysql              |
35 | performance_schema |
36 | test               |
37 +--------------------+
38 4 rows in set (0.00 sec)
39 
40 mysql> 
DROP DATABASE database_name; 刪除數據庫

 

3.命令幫助的獲取

  不管是客戶端還是服務端命令都是可以通過關鍵字“help”來獲取幫助信息,當然這得需要MySQL展開其內部的幫助文檔才能獲取到相應的信息的。可能有的童鞋會問,如果展開它的幫助文檔呢?其實在我們安裝完MySQL的時候就以及默認展開啦。命令本身不區分字符大小寫,但與文件系統相關的部分則根據os的不同,可能區分大小寫(比如數據庫名稱和表名等等。)

 1 mysql> help select
 2 Name: 'SELECT'
 3 Description:
 4 Syntax:
 5 SELECT
 6     [ALL | DISTINCT | DISTINCTROW ]
 7       [HIGH_PRIORITY]
 8       [STRAIGHT_JOIN]
 9       [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
10       [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
11     select_expr [, select_expr ...]
12     [FROM table_references
13     [WHERE where_condition]
14     [GROUP BY {col_name | expr | position}
15       [ASC | DESC], ... [WITH ROLLUP]]
16     [HAVING where_condition]
17     [ORDER BY {col_name | expr | position}
18       [ASC | DESC], ...]
19     [LIMIT {[offset,] row_count | row_count OFFSET offset}]
20     [PROCEDURE procedure_name(argument_list)]
21     [INTO OUTFILE 'file_name'
22         [CHARACTER SET charset_name]
23         export_options
24       | INTO DUMPFILE 'file_name'
25       | INTO var_name [, var_name]]
26     [FOR UPDATE | LOCK IN SHARE MODE]]
27 
28 SELECT is used to retrieve rows selected from one or more tables, and
29 can include UNION statements and subqueries. See [HELP UNION], and
30 http://dev.mysql.com/doc/refman/5.5/en/subqueries.html.
31 
32 The most commonly used clauses of SELECT statements are these:
33 
34 o Each select_expr indicates a column that you want to retrieve. There
35   must be at least one select_expr.
36 
37 o table_references indicates the table or tables from which to retrieve
38   rows. Its syntax is described in [HELP JOIN].
39 
40 o The WHERE clause, if given, indicates the condition or conditions
41   that rows must satisfy to be selected. where_condition is an
42   expression that evaluates to true for each row to be selected. The
43   statement selects all rows if there is no WHERE clause.
44 
45   In the WHERE expression, you can use any of the functions and
46   operators that MySQL supports, except for aggregate (summary)
47   functions. See
48   http://dev.mysql.com/doc/refman/5.5/en/expressions.html, and
49   http://dev.mysql.com/doc/refman/5.5/en/functions.html.
50 
51 SELECT can also be used to retrieve rows computed without reference to
52 any table.
53 
54 URL: http://dev.mysql.com/doc/refman/5.5/en/select.html
55 
56 
57 mysql> 
mysql> help KEYWORD    獲取幫助信息案例展示

 

4.MySQL的執行模式

a>.交互式執行模式

  交互式執行模式,顧名思義,你需要登錄數據庫界面,然后執行相應的操作。

 1 [root@yinzhengjie ~]# mysql -u root -p123
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 17
 4 Server version: 5.5.54-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> show databases;
15 +--------------------+
16 | Database           |
17 +--------------------+
18 | information_schema |
19 | mysql              |
20 | performance_schema |
21 | test               |
22 +--------------------+
23 4 rows in set (0.00 sec)
24 
25 mysql> create database yinzhengjie;
26 Query OK, 1 row affected (0.01 sec)
27 
28 mysql> show databases;
29 +--------------------+
30 | Database           |
31 +--------------------+
32 | information_schema |
33 | mysql              |
34 | performance_schema |
35 | test               |
36 | yinzhengjie        |
37 +--------------------+
38 5 rows in set (0.00 sec)
39 
40 mysql> 
MySQL交互式執行模式案例

b>.命令行執行模式

 1 [root@yinzhengjie ~]# mysql -pyinzhengjie -e "show databases;"
 2 +--------------------+
 3 | Database           |
 4 +--------------------+
 5 | information_schema |
 6 | mysql              |
 7 | test               |
 8 +--------------------+
 9 [root@yinzhengjie ~]# mysql -pyinzhengjie -e "create database yinzhengjie;"
10 [root@yinzhengjie ~]# mysql -pyinzhengjie -e "show databases;"
11 +--------------------+
12 | Database           |
13 +--------------------+
14 | information_schema |
15 | mysql              |
16 | test               |
17 | yinzhengjie        |
18 +--------------------+
19 [root@yinzhengjie ~]# 
MySQL令行執行模式案例

c>.批處理執行模式

 1 [root@yinzhengjie ~]# more yinzhengjie.sql 
 2 select user();
 3 create database yinzhengjie;
 4 show databases;
 5 [root@yinzhengjie ~]# 
 6 [root@yinzhengjie ~]# mysql -uroot -p123 < yinzhengjie.sql 
 7 user()
 8 root@localhost
 9 Database
10 information_schema
11 mysql
12 performance_schema
13 test
14 yinzhengjie
15 [root@yinzhengjie ~]# 
批處理模式執行命令案例

 

四.MySQL的數據類型

1.約束(constraint)

  約束就是如果你填寫了違反了某種法則的數據時(比如人的年齡不能超過200歲等等。)就會拒絕寫入,而這種限定就叫做約束。對於關系型數據庫來講,約束通常有以下幾種:

a>.主鍵約束

  主鍵約束就是當你把某個字段或者某些字段合並起來當做主鍵來用了,這就意味着你往里面填寫數據的時候就不能出現重復了。主鍵還有一個要求,即主鍵不能為空值(NULL)。對於一張表來講,主鍵只能有一個。

b>.外鍵約束

  假如在第一張表中有一個Name字段,而在第二張表中的第一個字段也有Name字段。那么很顯然所有填入第一張表中的Name字段的數據都應該是參照了第二張表中的屬性(換句話說,第一張表中的Name字段的填入規則必須和第二張表中的Name字段保持一致!)。這種參照(引用)方式叫做參照完整性約束,也叫做外鍵約束。

c>.惟一鍵約束

  惟一鍵約束和主鍵類似,只不過它的主鍵是可以為空的(NULL),並且對於一張表來講,主鍵可以有多個。

d>.檢查式約束

   無論是主鍵還是惟一鍵都沒有辦法實現把某一個字段的取值限制在合理的范圍內。比如,要求用戶輸入年齡,某個用戶手一哆嗦,本來想寫40,結果寫成了400,那么它真的有400歲嗎?很顯然並不是。因此就有了檢查式約束,即用戶自定義有效取值范圍的,它通常是一個布爾(bool)表達式,比較能符合條件就允許你往數據庫填寫數據,如果不能符合條件就禁止填入。

 

2.鍵

  鍵就是選取出來某個具有特殊意義的字段。對於關系型數據庫(簡稱RDBMS)來講,鍵指的是字段,因為它是用來當做查找標准或者處理標准所使用的。

a>.主鍵

  主鍵就是能夠惟一標識每一個記錄的字段或字段的組合。主鍵是從候選鍵中挑選出來的一種方案。是需要被真正使用的。

  打個比方,要求你在數據庫存放中國排名前十的大學從建立到現在的學生信息,我們能用名字來做主鍵嗎?顯然是不能的,同理,我們也不能拿年齡當唯一鍵,更不能拿性別當唯一鍵啦。甚至學號也可能出現重復的情況(比如清華大學的學生和北大的學生的學號是一樣的)。因此,我們可以將學校,姓名,年齡,性別以及學號組合起來當主鍵使用,這樣出現重復的概念就極小啦。因此鍵未必是一個字段。

b>.候選鍵

  做DBA的童鞋應該知道在數據庫的一張表當中,能夠唯一標識每一個記錄的字段的組合可能不止一個,我們還以上面的例子來說,統計全國排名前十的大學的學員信息,我們可以用學校,姓名,年齡,性別學號來做唯一主鍵,當然也可以只用學校,姓名和學號來做唯一主鍵。也就是說選擇主鍵的方式有很多種。因此,所有這些能夠用於唯一標識每一個記錄的鍵就叫做候選鍵。也就是說這些候選鍵都是可以拿來當主鍵用的,候選鍵只是一種概念,並沒有被真正使用。

c>.外鍵

   如果公共關鍵字在一個關系中是主關鍵字,那么這個公共關鍵字被稱為另一個關系的外鍵。由此可見,外鍵表示了兩個關系之間的相關聯系。以另一個關系的外鍵作主關鍵字的表被稱為主表,具有此外鍵的表被稱為主表的從表。外鍵又稱作外關鍵字

  打個比方,假如在第一張表中有一個Name字段,而在第二張表中的第一個字段也有Name字段。這個時候我們就可以說第一張表中的Name字段就是一個外鍵。也可以說第二張表中的Name字段是第一張表的一個外鍵。

 

3.數據類型

  數據類型是用來比較方式(排序方式),存儲空間(取值范圍),能夠參與的運算等等,這些功能都是由數據類型來確定的。

a>.字符型

  1>.固定長度的字符類型,換句話說就是定義了一個字符長度,如果你存儲的字節長度不足時,系統會自動給你用“0”進行占位操作,當字符類型的長度超過我們預定義的長度時,就會報錯。用char(#number[最多存儲255個字符])關鍵字來定義。(也就是說,char(10),就表示你每個字符串存儲的長度應該是10,比如你的單個字符串長度是7,那么還有3個空位系統自動會用“0”進行占位。也就沒有字符存儲的大小是一致的。)

  2>.自動變化長度的字符類型,即varchar(#bumber[最多存儲65535個字符,即2個字節])。和上面的char(#number)用法相似,只不過唯一不同的是存儲的字符的長度是可變化的。也就是說,varchar(10),就表示你每個字符串的長度應該是10,如果你的單個字符串長度是6,那么它就會以6個長度來存取的你的數據,而不會去可以給你填滿其他的占位符,它是以你實際長度為准的。注意,char(#number)和varchar(#num)存儲數據的時候都不區分大小寫。如果你強烈要求要區分大消息的話也是可以的,和char(#number)類似的一種方法叫做binary(#number),他們唯一不同就是char(#number)不區分大小寫,而binary(#number)是區分大小寫的。同理,和varchar(#number)對應的就是varbinary(#number)。

  3>.其實char(#number),var(#number),binary(#number),varbinary(#number)它們的存儲數據的大小是有上限的。如果你寫博客,里面的正文數據長度一定是很大的。這個時候這四種定義類型就不再合適了,這個時候就有了面向對象存儲的概念,就是不講數據放在一個表中了,而是將數據放在一個指定的文件,而在表中存儲的只是一個指針。當用戶用到該數據時,就會去調用這個指針所對應的文件。而這種存儲方式也是區分大小寫了,相比之前學習的四種定義類型,test和blob數據類型存儲的容量更大,test的類型擁有char(#number),binary(#number)的特點,而blob數據類型擁有varchar(#number),varbinary(#number)。而test和blob又被細分了4類,例如blob分為TinyBlob(最多存儲255個字符,即一個字節),Blob(最多存儲64kb,需要減去2個字節的空間),MediumBlob(最多存儲16MB的空間,需要減去3個字節),LongBlob(最多存儲4G的空間,需要減去4個字節)。test分為TinyText(最多存儲255個字符,即一個字節與char索引方式是不同的,TinyText不能使用全字段索引而char確實可以的),Text(最多存儲65535個字符,需要減去2個字節),MediumText(最多存儲16777215個字符,需要減去3個字節),LongText(最多存儲4294967295個字符,表示可以存儲4個G,不過一般沒人那么干,再問誰會將一個表中的一行字段占用4G的空間呢)等等。

  4>.字符型(char、varchar和text)常用的屬性修飾符:

    A>.NOT NULL(非空約束)
    B>.NULL(允許為空)
    C>.default ‘string’(默認值,不適用於text類型)
    D>.CHARACTER SET 字符集
      mysql>show variables like '%char%'; #查看默認的字符集
      mysql>show character set; #查看數據庫支持的字符集
    E>.collation '規則':排序規則
      msyql>show collation; #查看數據庫支持的排序規則

  5>.binary,varbinaray和blob字符常用的屬性修飾符

    A>.NOT NULL
    B>.NULL
    C>.default 不適用與blob

b>.數值型

  1>.精確數值型

    精確數值型有兩類,即整形(integer)和十進制(decimal)。在金融領域中,一般都用十進制存儲數值型。而integer類型來講,它有好幾種變化形式分別用來表示所能夠存儲的范圍大小的,比如:

        A>.只占用一個字節的tinyint(微整型,取值范圍是-128~127或者0~255);

        B>.占用兩個字節的samllint(小整型,取值范圍是-32768~32767或者0~65535);

        C>.占用三個字節的mediuint(中等整型,取值范圍-8388608~8388607或者0~16777215);

        D>.占用四個字節的int(整型,取值范圍-2147483648~2147483647或者0~4294967295);

        E>.占用八個字節的bingint(大整形,取值范圍是-9223372036854775808~9223372036854775807或者0~18446744073709551615)等等。 

  

  整形的常用屬性修飾符:
     A>.AUTO_INCREMENT:自動增長(前提:非空,且唯一,支持索引,非負值[UNSIGNED],注意:TRUNCATE 用來清空表中數據)
     B>.LEST_INSERT_ID():可以查看上次增長的數值,當插入多行時,只記錄第一行
     C>.UNSIGNED:無符號
     D>.NULL
     E>.NOT NULL
     F>.DEFAULT

  2>.近似數值型

     近似數值型又分為單精度浮點型(float)和雙精度浮點型(double)。  

    浮點型常用修飾符:(使用g,f來定義總共有多少數字和小數點后有多少數字)
      A>.NULL
      B>.NOT NULL
      C>.UNSIGNSD
      D>.DEFAULT

  3>.位

    按位(bit)實現數據存儲的。這種方式不建議使用。

c>.日期時間型

   1>我們在數據庫中可能需要存儲日期或時間,日期時間型其實本質上被我們存儲為數值或字符。

    A>.其中我們可以用占用三個字節的date(日期型)來存儲時間;

    B>.用占用三個字節的time(時間型)來存儲時間;

    C>.用占用八個字節的datetime(日期時間型)來存儲日期和時間;

    D>.當然,你也可以存儲在四個字節的timestamp(時間戳,存儲自1970年1月1號0點0分0秒至你所指定的時間為止所經過的秒數。);

    E>.對了還有一個字節來記錄年的year類型。即Year(2)和year(4),前者表示方法是:“00~99”,默認值是:“00”;后者表示方法是:“1901~2155”,默認為“0000”。

  2>.日期時間型常用的修飾符
    A>.NULL
    B>.NOT NULL
    C>.DEFAULT

d>.布爾型

  MySQL其實沒有真正意義上的布爾型,而是使用的tinyint(微整型),而且只顯示一位來表示的,要么是0,要么是1.

e>NULL

  表示什么也沒有存,注意空白字符不等於空喲,數字0也不代表空。

f>.內置類型(事實上ENUM和SET也是屬於字符型喲)

  1>.MySQL也有兩種常見的內置類型,即ENUM(枚舉)和SET(集合)。

  2>.枚舉類型是指將所有的情況的列舉出來,我們從中挑選處一種即可。比如今天是星期幾,我們通過枚舉方法列出所有的可能性,將星期一至星期日的其中情況都枚舉出來,這樣用戶輸入的星期八則是不合法的。集合類型是指在一個范圍內有很多元素,你可以把這些元素任意拼湊,怎么拼都行,但是每個元素必須是在給定范圍內的,不能超出限定范圍。就好比你是某學校的大一新生的班主任,現在你要對你的學生進行分組處理。其中每個組的每一個成員必須都是你的班級的學生,而不能講其他班級的學生拉倒你的班級進行分組。

  3>.ENUM和SET的修飾符
    A>.NULL
    B>.NOT NULL
    C>.DEFAULT ‘’

4.表(table)

  由行和列組成的二維關系,這也是他被稱為關系型數據庫類型的原因。

a>.表的創建(CREATE)

  用法格式:“create table TabelName(VariableName VariableType,......);”

 1 [root@yinzhengjie ~]# mysql -uroot -pyinzhengjie 
 2 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
 3 [root@yinzhengjie ~]# 
 4 [root@yinzhengjie ~]# mysql -uroot -p123
 5 Welcome to the MySQL monitor.  Commands end with ; or \g.
 6 Your MySQL connection id is 39
 7 Server version: 5.5.54-log MySQL Community Server (GPL)
 8 
 9 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
10 
11 Oracle is a registered trademark of Oracle Corporation and/or its
12 affiliates. Other names may be trademarks of their respective
13 owners.
14 
15 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
16 
17 mysql> show databases;
18 +--------------------+
19 | Database           |
20 +--------------------+
21 | information_schema |
22 | mysql              |
23 | performance_schema |
24 | test               |
25 | yinzhengjie        |
26 +--------------------+
27 5 rows in set (0.01 sec)
28 
29 mysql> use yinzhengjie
30 Database changed
31 mysql> create table  Student (Name varchar(30),Age tinyint,Gender ENUM('boy','girl'));        ----->這里是創建一個表,該表有3個字段,分別是姓名(Name),年齡(Age)和性別(Gender),定義變量名稱和Golang很相似。
32 Query OK, 0 rows affected (0.01 sec)
33 
34 mysql>     
創建一個表,格式參考:“ create table TabelName(VariableName VariableType,......);”
 1 [root@yinzhengjie ~]# mysql -uroot -p123
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 42
 4 Server version: 5.5.54-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> show databases;
15 +--------------------+
16 | Database           |
17 +--------------------+
18 | information_schema |
19 | mysql              |
20 | performance_schema |
21 | test               |
22 | yinzhengjie        |
23 +--------------------+
24 5 rows in set (0.00 sec)
25 
26 mysql> use yinzhengjie
27 Database changed
28 mysql> show tables;
29 +-----------------------+
30 | Tables_in_yinzhengjie |
31 +-----------------------+
32 | Student               |
33 +-----------------------+
34 1 row in set (0.00 sec)
35 
36 mysql> desc Student;
37 +--------+--------------------+------+-----+---------+-------+
38 | Field  | Type               | Null | Key | Default | Extra |
39 +--------+--------------------+------+-----+---------+-------+
40 | Name   | varchar(30)        | YES  |     | NULL    |       |
41 | Age    | tinyint(4)         | YES  |     | NULL    |       |
42 | Gender | enum('boy','girl') | YES  |     | NULL    |       |
43 +--------+--------------------+------+-----+---------+-------+
44 3 rows in set (0.00 sec)
45 
46 mysql> 
查看表結構 ,格式參考:“desc TableName”
 1 [root@yinzhengjie ~]# mysql -uroot -p123
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 44
 4 Server version: 5.5.54-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> show databases;
15 +--------------------+
16 | Database           |
17 +--------------------+
18 | information_schema |
19 | mysql              |
20 | performance_schema |
21 | test               |
22 | yinzhengjie        |
23 +--------------------+
24 5 rows in set (0.00 sec)
25 
26 mysql> use yinzhengjie
27 Database changed
28 mysql> show tables;
29 +-----------------------+
30 | Tables_in_yinzhengjie |
31 +-----------------------+
32 | Student               |
33 +-----------------------+
34 1 row in set (0.00 sec)
35 
36 mysql> drop table Student;
37 Query OK, 0 rows affected (0.00 sec)
38 
39 mysql> show tables;
40 Empty set (0.00 sec)
41 
42 mysql> 
刪除一個表,格式參考:“drop table TableName”
1    注意,字段或字段類型還可以有修飾符 2 
3     1>.NOT NULL #表示插入數據時不允許為空 4     2>.NULL          #表示插入的數據可以為空 5     3>.DEDAULT        #字符要加引號,數字不能加引號 6     4>.UNSIGNED        #無符號(只能用在整型上) 7     5>.AUTO_INCREMENT   #自動增長類型的字段必須為主鍵或唯一鍵,即使你清空了一個表格的數據,它默認情況下是不會重置的喲。 8     6>.PRIMARY KEY     #定義主鍵 9     7>.UNIQUE KEY         #唯一鍵
 1 [root@yinzhengjie ~]# mysql -uroot -p123
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 45
 4 Server version: 5.5.54-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> use yinzhengjie
15 Database changed
16 mysql> create table  Student (StuID int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,Name varchar(30) NOT NULL,Age tinyint UNSIGNED NOT NULL,Gender ENUM('boy','girl') NOT NULL DEFAULT 'boy');             ----->和之前創建的表名稱一樣,只不過在每個變量類型后面加了修飾符,來限定變量是否可用為空,並且新添加了一個INT類型的StuID字段,並且讓該INT類型自動增長。
17 Query OK, 0 rows affected (0.01 sec)
18 
19 mysql> 
20 mysql> desc Student;
21 +--------+---------------------+------+-----+---------+----------------+
22 | Field  | Type                | Null | Key | Default | Extra          |
23 +--------+---------------------+------+-----+---------+----------------+
24 | StuID  | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
25 | Name   | varchar(30)         | NO   |     | NULL    |                |
26 | Age    | tinyint(3) unsigned | NO   |     | NULL    |                |
27 | Gender | enum('boy','girl')  | NO   |     | boy     |                |
28 +--------+---------------------+------+-----+---------+----------------+
29 4 rows in set (0.00 sec)
30 
31 mysql> 
定義有修飾符的表案例

 b>.表的插入(INSERT)

  用法格式:“INSERT INTO TableName(VariableName1,VariableName2,...) VALUE|VALUES (VariableValue1,VariableValue2.....);”

 1 mysql> desc Student;
 2 +--------+---------------------+------+-----+---------+----------------+
 3 | Field  | Type                | Null | Key | Default | Extra          |
 4 +--------+---------------------+------+-----+---------+----------------+
 5 | StuID  | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
 6 | Name   | varchar(30)         | NO   |     | NULL    |                |
 7 | Age    | tinyint(3) unsigned | NO   |     | NULL    |                |
 8 | Gender | enum('boy','girl')  | NO   |     | boy     |                |
 9 +--------+---------------------+------+-----+---------+----------------+
10 4 rows in set (0.00 sec)
11 
12 mysql> insert into Student(Name,Age,Gender) values ( "yinzihengjie",17,'boy');
13 Query OK, 1 row affected (0.01 sec)
14 mysql> select * from Student;
15 +-------+--------------+-----+--------+
16 | StuID | Name         | Age | Gender |
17 +-------+--------------+-----+--------+
18 |     1 | yinzihengjie |  17 | boy    |
19 +-------+--------------+-----+--------+
20 1 row in set (0.00 sec)
21 
22 mysql> 
只插入一條數據案例展示
 1 mysql> select * from Student;
 2 +-------+--------------+-----+--------+
 3 | StuID | Name         | Age | Gender |
 4 +-------+--------------+-----+--------+
 5 |     1 | yinzihengjie |  17 | boy    |
 6 +-------+--------------+-----+--------+
 7 1 row in set (0.00 sec)
 8 
 9 mysql> insert into Student(Name,Age,Gender) values ( "linus",48,'boy'),("dengziqi",26,'girl');
10 Query OK, 2 rows affected (0.02 sec)
11 Records: 2  Duplicates: 0  Warnings: 0
12 
13 mysql> select * from Student;
14 +-------+--------------+-----+--------+
15 | StuID | Name         | Age | Gender |
16 +-------+--------------+-----+--------+
17 |     1 | yinzihengjie |  17 | boy    |
18 |     2 | linus        |  48 | boy    |
19 |     3 | dengziqi     |  26 | girl   |
20 +-------+--------------+-----+--------+
21 3 rows in set (0.00 sec)
22 
23 mysql> 
同時插入(批量插入)多條數據用法展示
 1 mysql> select * from Student;
 2 +-------+--------------+-----+--------+
 3 | StuID | Name         | Age | Gender |
 4 +-------+--------------+-----+--------+
 5 |     1 | yinzihengjie |  17 | boy    |
 6 |     2 | linus        |  48 | boy    |
 7 |     3 | dengziqi     |  26 | girl   |
 8 +-------+--------------+-----+--------+
 9 3 rows in set (0.00 sec)
10 
11 mysql> insert into Student(Name,Age) values ( "jay Chou",38),("joker Xue",34),("zhangweijian",52);
12 Query OK, 3 rows affected (0.00 sec)
13 Records: 3  Duplicates: 0  Warnings: 0
14 
15 mysql> select * from Student;
16 +-------+--------------+-----+--------+
17 | StuID | Name         | Age | Gender |
18 +-------+--------------+-----+--------+
19 |     1 | yinzihengjie |  17 | boy    |
20 |     2 | linus        |  48 | boy    |
21 |     3 | dengziqi     |  26 | girl   |
22 |     4 | jay Chou     |  38 | boy    |
23 |     5 | joker Xue    |  34 | boy    |
24 |     6 | zhangweijian |  52 | boy    |
25 +-------+--------------+-----+--------+
26 6 rows in set (0.00 sec)
27 
28 mysql> 
如果批量插入的某個字段是都是一致的情況可以省去,點我看用法展示

c>.表的查詢

 1   用法格式:“select VariableName1,VariableName2 from TableName where ConditionalExpression;”  2 
 3   當然查詢是可以用,刪除,修改都是可以用like,rlike以及組合條件進行過濾,從而得到我們想要的字段。  4 
 5 
 6   like通配符:  7     %:任意長度的任意字符  8     _:匹配任意單個字符  9   rlink 10     正則表達式 11 
12 
13   組合條件 14     and 15     or 16     not
 1 mysql> select * from Student;
 2 +-------+--------------+-----+--------+
 3 | StuID | Name         | Age | Gender |
 4 +-------+--------------+-----+--------+
 5 |     1 | yinzihengjie |  17 | boy    |
 6 |     2 | linus        |  48 | boy    |
 7 |     3 | dengziqi     |  26 | girl   |
 8 |     4 | jay Chou     |  38 | boy    |
 9 |     5 | joker Xue    |  34 | boy    |
10 |     6 | zhangweijian |  52 | boy    |
11 +-------+--------------+-----+--------+
12 6 rows in set (0.00 sec)
13 mysql> select Name,Age,Gender from Student where Name = 'yinzihengjie';
14 +--------------+-----+--------+
15 | Name         | Age | Gender |
16 +--------------+-----+--------+
17 | yinzihengjie |  17 | boy    |
18 +--------------+-----+--------+
19 1 row in set (0.00 sec)
20 
21 mysql> 
select中的where用法展示
 1 mysql> select * from Student;
 2 +-------+--------------+-----+--------+
 3 | StuID | Name         | Age | Gender |
 4 +-------+--------------+-----+--------+
 5 |     1 | yinzihengjie |  17 | boy    |
 6 |     2 | linus        |  48 | boy    |
 7 |     3 | dengziqi     |  26 | girl   |
 8 |     4 | jay Chou     |  38 | boy    |
 9 |     5 | joker Xue    |  34 | boy    |
10 |     6 | zhangweijian |  52 | boy    |
11 +-------+--------------+-----+--------+
12 6 rows in set (0.00 sec)
13 
14 mysql> select Name,Age,Gender from Student where Gender = 'boy' and Age > 35;
15 +--------------+-----+--------+
16 | Name         | Age | Gender |
17 +--------------+-----+--------+
18 | linus        |  48 | boy    |
19 | jay Chou     |  38 | boy    |
20 | zhangweijian |  52 | boy    |
21 +--------------+-----+--------+
22 3 rows in set (0.00 sec)
23 
24 mysql> 
where的組合用法展示
  Mysql服務器的工作特性的定義是通過服務器變量實現的,而MySQL服務器運行中的狀態是通過狀態變量輸出的。
 1 mysql> show create table Student;
 2 +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 3 | Table   | Create Table                                                                                                                                                                                                                                                                            |
 4 +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 5 | Student | CREATE TABLE `Student` (
 6   `StuID` int(10) unsigned NOT NULL AUTO_INCREMENT,
 7   `Name` varchar(30) NOT NULL,
 8   `Age` tinyint(3) unsigned NOT NULL,
 9   `Gender` enum('boy','girl') NOT NULL DEFAULT 'boy',
10   PRIMARY KEY (`StuID`)
11 ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 |
12 +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
13 1 row in set (0.00 sec)
14 
15 mysql> 
查看類命令-查詢創建表時使用的命令
 1 mysql> show engines;
 2 +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
 3 | Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
 4 +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
 5 | PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
 6 | MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
 7 | CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
 8 | BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
 9 | MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
10 | InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
11 | ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
12 | MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
13 | FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
14 +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
15 9 rows in set (0.00 sec)
16 
17 mysql> 
查看類命令-查詢數據庫支持的存儲類型
 1 mysql> show table status\G
 2 *************************** 1. row ***************************
 3            Name: Student
 4          Engine: InnoDB
 5         Version: 10
 6      Row_format: Compact
 7            Rows: 4
 8  Avg_row_length: 4096
 9     Data_length: 16384
10 Max_data_length: 0
11    Index_length: 0
12       Data_free: 10485760
13  Auto_increment: 7
14     Create_time: 2017-11-11 08:14:12
15     Update_time: NULL
16      Check_time: NULL
17       Collation: latin1_swedish_ci
18        Checksum: NULL
19  Create_options: 
20         Comment: 
21 1 row in set (0.00 sec)
22 
23 mysql> 
查看類命令-查詢當前數據庫的存儲類型
 1 mysql> show character set;
 2 +----------+-----------------------------+---------------------+--------+
 3 | Charset  | Description                 | Default collation   | Maxlen |
 4 +----------+-----------------------------+---------------------+--------+
 5 | big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |
 6 | dec8     | DEC West European           | dec8_swedish_ci     |      1 |
 7 | cp850    | DOS West European           | cp850_general_ci    |      1 |
 8 | hp8      | HP West European            | hp8_english_ci      |      1 |
 9 | koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    |      1 |
10 | latin1   | cp1252 West European        | latin1_swedish_ci   |      1 |
11 | latin2   | ISO 8859-2 Central European | latin2_general_ci   |      1 |
12 | swe7     | 7bit Swedish                | swe7_swedish_ci     |      1 |
13 | ascii    | US ASCII                    | ascii_general_ci    |      1 |
14 | ujis     | EUC-JP Japanese             | ujis_japanese_ci    |      3 |
15 | sjis     | Shift-JIS Japanese          | sjis_japanese_ci    |      2 |
16 | hebrew   | ISO 8859-8 Hebrew           | hebrew_general_ci   |      1 |
17 | tis620   | TIS620 Thai                 | tis620_thai_ci      |      1 |
18 | euckr    | EUC-KR Korean               | euckr_korean_ci     |      2 |
19 | koi8u    | KOI8-U Ukrainian            | koi8u_general_ci    |      1 |
20 | gb2312   | GB2312 Simplified Chinese   | gb2312_chinese_ci   |      2 |
21 | greek    | ISO 8859-7 Greek            | greek_general_ci    |      1 |
22 | cp1250   | Windows Central European    | cp1250_general_ci   |      1 |
23 | gbk      | GBK Simplified Chinese      | gbk_chinese_ci      |      2 |
24 | latin5   | ISO 8859-9 Turkish          | latin5_turkish_ci   |      1 |
25 | armscii8 | ARMSCII-8 Armenian          | armscii8_general_ci |      1 |
26 | utf8     | UTF-8 Unicode               | utf8_general_ci     |      3 |
27 | ucs2     | UCS-2 Unicode               | ucs2_general_ci     |      2 |
28 | cp866    | DOS Russian                 | cp866_general_ci    |      1 |
29 | keybcs2  | DOS Kamenicky Czech-Slovak  | keybcs2_general_ci  |      1 |
30 | macce    | Mac Central European        | macce_general_ci    |      1 |
31 | macroman | Mac West European           | macroman_general_ci |      1 |
32 | cp852    | DOS Central European        | cp852_general_ci    |      1 |
33 | latin7   | ISO 8859-13 Baltic          | latin7_general_ci   |      1 |
34 | utf8mb4  | UTF-8 Unicode               | utf8mb4_general_ci  |      4 |
35 | cp1251   | Windows Cyrillic            | cp1251_general_ci   |      1 |
36 | utf16    | UTF-16 Unicode              | utf16_general_ci    |      4 |
37 | cp1256   | Windows Arabic              | cp1256_general_ci   |      1 |
38 | cp1257   | Windows Baltic              | cp1257_general_ci   |      1 |
39 | utf32    | UTF-32 Unicode              | utf32_general_ci    |      4 |
40 | binary   | Binary pseudo charset       | binary              |      1 |
41 | geostd8  | GEOSTD8 Georgian            | geostd8_general_ci  |      1 |
42 | cp932    | SJIS for Windows Japanese   | cp932_japanese_ci   |      2 |
43 | eucjpms  | UJIS for Windows Japanese   | eucjpms_japanese_ci |      3 |
44 +----------+-----------------------------+---------------------+--------+
45 39 rows in set (0.03 sec)
46 
47 mysql> 
查看類命令-查詢字符集(字符集是通過二進制轉換為漢字的結果)
  1 mysql> show collation;
  2 +--------------------------+----------+-----+---------+----------+---------+
  3 | Collation                | Charset  | Id  | Default | Compiled | Sortlen |
  4 +--------------------------+----------+-----+---------+----------+---------+
  5 | big5_chinese_ci          | big5     |   1 | Yes     | Yes      |       1 |
  6 | big5_bin                 | big5     |  84 |         | Yes      |       1 |
  7 | dec8_swedish_ci          | dec8     |   3 | Yes     | Yes      |       1 |
  8 | dec8_bin                 | dec8     |  69 |         | Yes      |       1 |
  9 | cp850_general_ci         | cp850    |   4 | Yes     | Yes      |       1 |
 10 | cp850_bin                | cp850    |  80 |         | Yes      |       1 |
 11 | hp8_english_ci           | hp8      |   6 | Yes     | Yes      |       1 |
 12 | hp8_bin                  | hp8      |  72 |         | Yes      |       1 |
 13 | koi8r_general_ci         | koi8r    |   7 | Yes     | Yes      |       1 |
 14 | koi8r_bin                | koi8r    |  74 |         | Yes      |       1 |
 15 | latin1_german1_ci        | latin1   |   5 |         | Yes      |       1 |
 16 | latin1_swedish_ci        | latin1   |   8 | Yes     | Yes      |       1 |
 17 | latin1_danish_ci         | latin1   |  15 |         | Yes      |       1 |
 18 | latin1_german2_ci        | latin1   |  31 |         | Yes      |       2 |
 19 | latin1_bin               | latin1   |  47 |         | Yes      |       1 |
 20 | latin1_general_ci        | latin1   |  48 |         | Yes      |       1 |
 21 | latin1_general_cs        | latin1   |  49 |         | Yes      |       1 |
 22 | latin1_spanish_ci        | latin1   |  94 |         | Yes      |       1 |
 23 | latin2_czech_cs          | latin2   |   2 |         | Yes      |       4 |
 24 | latin2_general_ci        | latin2   |   9 | Yes     | Yes      |       1 |
 25 | latin2_hungarian_ci      | latin2   |  21 |         | Yes      |       1 |
 26 | latin2_croatian_ci       | latin2   |  27 |         | Yes      |       1 |
 27 | latin2_bin               | latin2   |  77 |         | Yes      |       1 |
 28 | swe7_swedish_ci          | swe7     |  10 | Yes     | Yes      |       1 |
 29 | swe7_bin                 | swe7     |  82 |         | Yes      |       1 |
 30 | ascii_general_ci         | ascii    |  11 | Yes     | Yes      |       1 |
 31 | ascii_bin                | ascii    |  65 |         | Yes      |       1 |
 32 | ujis_japanese_ci         | ujis     |  12 | Yes     | Yes      |       1 |
 33 | ujis_bin                 | ujis     |  91 |         | Yes      |       1 |
 34 | sjis_japanese_ci         | sjis     |  13 | Yes     | Yes      |       1 |
 35 | sjis_bin                 | sjis     |  88 |         | Yes      |       1 |
 36 | hebrew_general_ci        | hebrew   |  16 | Yes     | Yes      |       1 |
 37 | hebrew_bin               | hebrew   |  71 |         | Yes      |       1 |
 38 | tis620_thai_ci           | tis620   |  18 | Yes     | Yes      |       4 |
 39 | tis620_bin               | tis620   |  89 |         | Yes      |       1 |
 40 | euckr_korean_ci          | euckr    |  19 | Yes     | Yes      |       1 |
 41 | euckr_bin                | euckr    |  85 |         | Yes      |       1 |
 42 | koi8u_general_ci         | koi8u    |  22 | Yes     | Yes      |       1 |
 43 | koi8u_bin                | koi8u    |  75 |         | Yes      |       1 |
 44 | gb2312_chinese_ci        | gb2312   |  24 | Yes     | Yes      |       1 |
 45 | gb2312_bin               | gb2312   |  86 |         | Yes      |       1 |
 46 | greek_general_ci         | greek    |  25 | Yes     | Yes      |       1 |
 47 | greek_bin                | greek    |  70 |         | Yes      |       1 |
 48 | cp1250_general_ci        | cp1250   |  26 | Yes     | Yes      |       1 |
 49 | cp1250_czech_cs          | cp1250   |  34 |         | Yes      |       2 |
 50 | cp1250_croatian_ci       | cp1250   |  44 |         | Yes      |       1 |
 51 | cp1250_bin               | cp1250   |  66 |         | Yes      |       1 |
 52 | cp1250_polish_ci         | cp1250   |  99 |         | Yes      |       1 |
 53 | gbk_chinese_ci           | gbk      |  28 | Yes     | Yes      |       1 |
 54 | gbk_bin                  | gbk      |  87 |         | Yes      |       1 |
 55 | latin5_turkish_ci        | latin5   |  30 | Yes     | Yes      |       1 |
 56 | latin5_bin               | latin5   |  78 |         | Yes      |       1 |
 57 | armscii8_general_ci      | armscii8 |  32 | Yes     | Yes      |       1 |
 58 | armscii8_bin             | armscii8 |  64 |         | Yes      |       1 |
 59 | utf8_general_ci          | utf8     |  33 | Yes     | Yes      |       1 |
 60 | utf8_bin                 | utf8     |  83 |         | Yes      |       1 |
 61 | utf8_unicode_ci          | utf8     | 192 |         | Yes      |       8 |
 62 | utf8_icelandic_ci        | utf8     | 193 |         | Yes      |       8 |
 63 | utf8_latvian_ci          | utf8     | 194 |         | Yes      |       8 |
 64 | utf8_romanian_ci         | utf8     | 195 |         | Yes      |       8 |
 65 | utf8_slovenian_ci        | utf8     | 196 |         | Yes      |       8 |
 66 | utf8_polish_ci           | utf8     | 197 |         | Yes      |       8 |
 67 | utf8_estonian_ci         | utf8     | 198 |         | Yes      |       8 |
 68 | utf8_spanish_ci          | utf8     | 199 |         | Yes      |       8 |
 69 | utf8_swedish_ci          | utf8     | 200 |         | Yes      |       8 |
 70 | utf8_turkish_ci          | utf8     | 201 |         | Yes      |       8 |
 71 | utf8_czech_ci            | utf8     | 202 |         | Yes      |       8 |
 72 | utf8_danish_ci           | utf8     | 203 |         | Yes      |       8 |
 73 | utf8_lithuanian_ci       | utf8     | 204 |         | Yes      |       8 |
 74 | utf8_slovak_ci           | utf8     | 205 |         | Yes      |       8 |
 75 | utf8_spanish2_ci         | utf8     | 206 |         | Yes      |       8 |
 76 | utf8_roman_ci            | utf8     | 207 |         | Yes      |       8 |
 77 | utf8_persian_ci          | utf8     | 208 |         | Yes      |       8 |
 78 | utf8_esperanto_ci        | utf8     | 209 |         | Yes      |       8 |
 79 | utf8_hungarian_ci        | utf8     | 210 |         | Yes      |       8 |
 80 | utf8_sinhala_ci          | utf8     | 211 |         | Yes      |       8 |
 81 | utf8_general_mysql500_ci | utf8     | 223 |         | Yes      |       1 |
 82 | ucs2_general_ci          | ucs2     |  35 | Yes     | Yes      |       1 |
 83 | ucs2_bin                 | ucs2     |  90 |         | Yes      |       1 |
 84 | ucs2_unicode_ci          | ucs2     | 128 |         | Yes      |       8 |
 85 | ucs2_icelandic_ci        | ucs2     | 129 |         | Yes      |       8 |
 86 | ucs2_latvian_ci          | ucs2     | 130 |         | Yes      |       8 |
 87 | ucs2_romanian_ci         | ucs2     | 131 |         | Yes      |       8 |
 88 | ucs2_slovenian_ci        | ucs2     | 132 |         | Yes      |       8 |
 89 | ucs2_polish_ci           | ucs2     | 133 |         | Yes      |       8 |
 90 | ucs2_estonian_ci         | ucs2     | 134 |         | Yes      |       8 |
 91 | ucs2_spanish_ci          | ucs2     | 135 |         | Yes      |       8 |
 92 | ucs2_swedish_ci          | ucs2     | 136 |         | Yes      |       8 |
 93 | ucs2_turkish_ci          | ucs2     | 137 |         | Yes      |       8 |
 94 | ucs2_czech_ci            | ucs2     | 138 |         | Yes      |       8 |
 95 | ucs2_danish_ci           | ucs2     | 139 |         | Yes      |       8 |
 96 | ucs2_lithuanian_ci       | ucs2     | 140 |         | Yes      |       8 |
 97 | ucs2_slovak_ci           | ucs2     | 141 |         | Yes      |       8 |
 98 | ucs2_spanish2_ci         | ucs2     | 142 |         | Yes      |       8 |
 99 | ucs2_roman_ci            | ucs2     | 143 |         | Yes      |       8 |
100 | ucs2_persian_ci          | ucs2     | 144 |         | Yes      |       8 |
101 | ucs2_esperanto_ci        | ucs2     | 145 |         | Yes      |       8 |
102 | ucs2_hungarian_ci        | ucs2     | 146 |         | Yes      |       8 |
103 | ucs2_sinhala_ci          | ucs2     | 147 |         | Yes      |       8 |
104 | ucs2_general_mysql500_ci | ucs2     | 159 |         | Yes      |       1 |
105 | cp866_general_ci         | cp866    |  36 | Yes     | Yes      |       1 |
106 | cp866_bin                | cp866    |  68 |         | Yes      |       1 |
107 | keybcs2_general_ci       | keybcs2  |  37 | Yes     | Yes      |       1 |
108 | keybcs2_bin              | keybcs2  |  73 |         | Yes      |       1 |
109 | macce_general_ci         | macce    |  38 | Yes     | Yes      |       1 |
110 | macce_bin                | macce    |  43 |         | Yes      |       1 |
111 | macroman_general_ci      | macroman |  39 | Yes     | Yes      |       1 |
112 | macroman_bin             | macroman |  53 |         | Yes      |       1 |
113 | cp852_general_ci         | cp852    |  40 | Yes     | Yes      |       1 |
114 | cp852_bin                | cp852    |  81 |         | Yes      |       1 |
115 | latin7_estonian_cs       | latin7   |  20 |         | Yes      |       1 |
116 | latin7_general_ci        | latin7   |  41 | Yes     | Yes      |       1 |
117 | latin7_general_cs        | latin7   |  42 |         | Yes      |       1 |
118 | latin7_bin               | latin7   |  79 |         | Yes      |       1 |
119 | utf8mb4_general_ci       | utf8mb4  |  45 | Yes     | Yes      |       1 |
120 | utf8mb4_bin              | utf8mb4  |  46 |         | Yes      |       1 |
121 | utf8mb4_unicode_ci       | utf8mb4  | 224 |         | Yes      |       8 |
122 | utf8mb4_icelandic_ci     | utf8mb4  | 225 |         | Yes      |       8 |
123 | utf8mb4_latvian_ci       | utf8mb4  | 226 |         | Yes      |       8 |
124 | utf8mb4_romanian_ci      | utf8mb4  | 227 |         | Yes      |       8 |
125 | utf8mb4_slovenian_ci     | utf8mb4  | 228 |         | Yes      |       8 |
126 | utf8mb4_polish_ci        | utf8mb4  | 229 |         | Yes      |       8 |
127 | utf8mb4_estonian_ci      | utf8mb4  | 230 |         | Yes      |       8 |
128 | utf8mb4_spanish_ci       | utf8mb4  | 231 |         | Yes      |       8 |
129 | utf8mb4_swedish_ci       | utf8mb4  | 232 |         | Yes      |       8 |
130 | utf8mb4_turkish_ci       | utf8mb4  | 233 |         | Yes      |       8 |
131 | utf8mb4_czech_ci         | utf8mb4  | 234 |         | Yes      |       8 |
132 | utf8mb4_danish_ci        | utf8mb4  | 235 |         | Yes      |       8 |
133 | utf8mb4_lithuanian_ci    | utf8mb4  | 236 |         | Yes      |       8 |
134 | utf8mb4_slovak_ci        | utf8mb4  | 237 |         | Yes      |       8 |
135 | utf8mb4_spanish2_ci      | utf8mb4  | 238 |         | Yes      |       8 |
136 | utf8mb4_roman_ci         | utf8mb4  | 239 |         | Yes      |       8 |
137 | utf8mb4_persian_ci       | utf8mb4  | 240 |         | Yes      |       8 |
138 | utf8mb4_esperanto_ci     | utf8mb4  | 241 |         | Yes      |       8 |
139 | utf8mb4_hungarian_ci     | utf8mb4  | 242 |         | Yes      |       8 |
140 | utf8mb4_sinhala_ci       | utf8mb4  | 243 |         | Yes      |       8 |
141 | cp1251_bulgarian_ci      | cp1251   |  14 |         | Yes      |       1 |
142 | cp1251_ukrainian_ci      | cp1251   |  23 |         | Yes      |       1 |
143 | cp1251_bin               | cp1251   |  50 |         | Yes      |       1 |
144 | cp1251_general_ci        | cp1251   |  51 | Yes     | Yes      |       1 |
145 | cp1251_general_cs        | cp1251   |  52 |         | Yes      |       1 |
146 | utf16_general_ci         | utf16    |  54 | Yes     | Yes      |       1 |
147 | utf16_bin                | utf16    |  55 |         | Yes      |       1 |
148 | utf16_unicode_ci         | utf16    | 101 |         | Yes      |       8 |
149 | utf16_icelandic_ci       | utf16    | 102 |         | Yes      |       8 |
150 | utf16_latvian_ci         | utf16    | 103 |         | Yes      |       8 |
151 | utf16_romanian_ci        | utf16    | 104 |         | Yes      |       8 |
152 | utf16_slovenian_ci       | utf16    | 105 |         | Yes      |       8 |
153 | utf16_polish_ci          | utf16    | 106 |         | Yes      |       8 |
154 | utf16_estonian_ci        | utf16    | 107 |         | Yes      |       8 |
155 | utf16_spanish_ci         | utf16    | 108 |         | Yes      |       8 |
156 | utf16_swedish_ci         | utf16    | 109 |         | Yes      |       8 |
157 | utf16_turkish_ci         | utf16    | 110 |         | Yes      |       8 |
158 | utf16_czech_ci           | utf16    | 111 |         | Yes      |       8 |
159 | utf16_danish_ci          | utf16    | 112 |         | Yes      |       8 |
160 | utf16_lithuanian_ci      | utf16    | 113 |         | Yes      |       8 |
161 | utf16_slovak_ci          | utf16    | 114 |         | Yes      |       8 |
162 | utf16_spanish2_ci        | utf16    | 115 |         | Yes      |       8 |
163 | utf16_roman_ci           | utf16    | 116 |         | Yes      |       8 |
164 | utf16_persian_ci         | utf16    | 117 |         | Yes      |       8 |
165 | utf16_esperanto_ci       | utf16    | 118 |         | Yes      |       8 |
166 | utf16_hungarian_ci       | utf16    | 119 |         | Yes      |       8 |
167 | utf16_sinhala_ci         | utf16    | 120 |         | Yes      |       8 |
168 | cp1256_general_ci        | cp1256   |  57 | Yes     | Yes      |       1 |
169 | cp1256_bin               | cp1256   |  67 |         | Yes      |       1 |
170 | cp1257_lithuanian_ci     | cp1257   |  29 |         | Yes      |       1 |
171 | cp1257_bin               | cp1257   |  58 |         | Yes      |       1 |
172 | cp1257_general_ci        | cp1257   |  59 | Yes     | Yes      |       1 |
173 | utf32_general_ci         | utf32    |  60 | Yes     | Yes      |       1 |
174 | utf32_bin                | utf32    |  61 |         | Yes      |       1 |
175 | utf32_unicode_ci         | utf32    | 160 |         | Yes      |       8 |
176 | utf32_icelandic_ci       | utf32    | 161 |         | Yes      |       8 |
177 | utf32_latvian_ci         | utf32    | 162 |         | Yes      |       8 |
178 | utf32_romanian_ci        | utf32    | 163 |         | Yes      |       8 |
179 | utf32_slovenian_ci       | utf32    | 164 |         | Yes      |       8 |
180 | utf32_polish_ci          | utf32    | 165 |         | Yes      |       8 |
181 | utf32_estonian_ci        | utf32    | 166 |         | Yes      |       8 |
182 | utf32_spanish_ci         | utf32    | 167 |         | Yes      |       8 |
183 | utf32_swedish_ci         | utf32    | 168 |         | Yes      |       8 |
184 | utf32_turkish_ci         | utf32    | 169 |         | Yes      |       8 |
185 | utf32_czech_ci           | utf32    | 170 |         | Yes      |       8 |
186 | utf32_danish_ci          | utf32    | 171 |         | Yes      |       8 |
187 | utf32_lithuanian_ci      | utf32    | 172 |         | Yes      |       8 |
188 | utf32_slovak_ci          | utf32    | 173 |         | Yes      |       8 |
189 | utf32_spanish2_ci        | utf32    | 174 |         | Yes      |       8 |
190 | utf32_roman_ci           | utf32    | 175 |         | Yes      |       8 |
191 | utf32_persian_ci         | utf32    | 176 |         | Yes      |       8 |
192 | utf32_esperanto_ci       | utf32    | 177 |         | Yes      |       8 |
193 | utf32_hungarian_ci       | utf32    | 178 |         | Yes      |       8 |
194 | utf32_sinhala_ci         | utf32    | 179 |         | Yes      |       8 |
195 | binary                   | binary   |  63 | Yes     | Yes      |       1 |
196 | geostd8_general_ci       | geostd8  |  92 | Yes     | Yes      |       1 |
197 | geostd8_bin              | geostd8  |  93 |         | Yes      |       1 |
198 | cp932_japanese_ci        | cp932    |  95 | Yes     | Yes      |       1 |
199 | cp932_bin                | cp932    |  96 |         | Yes      |       1 |
200 | eucjpms_japanese_ci      | eucjpms  |  97 | Yes     | Yes      |       1 |
201 | eucjpms_bin              | eucjpms  |  98 |         | Yes      |       1 |
202 +--------------------------+----------+-----+---------+----------+---------+
203 197 rows in set (0.00 sec)
204 
205 mysql> 
查看類命令-查詢字符集的排序方式
1 mysql> show variables like 'data%';
2 +---------------+---------------+
3 | Variable_name | Value         |
4 +---------------+---------------+
5 | datadir       | /data/mydata/ |
6 +---------------+---------------+
7 1 row in set (0.00 sec)
8 
9 mysql> 
查看類命令-查看服務器變量,可以通過like命令進行過濾。
1 mysql> show global variables like 'version';
2 +---------------+------------+
3 | Variable_name | Value      |
4 +---------------+------------+
5 | version       | 5.5.54-log |
6 +---------------+------------+
7 1 row in set (0.00 sec)
8 
9 mysql> 
查看類命令-查看全局變量
1 mysql> show session variables like 'tmp_table_size';
2 +----------------+----------+
3 | Variable_name  | Value    |
4 +----------------+----------+
5 | tmp_table_size | 16777216 |
6 +----------------+----------+
7 1 row in set (0.00 sec)
8 
9 mysql> 
查看類命令-查看會話的變量
1 mysql> show status like 'Com_select';
2 +---------------+-------+
3 | Variable_name | Value |
4 +---------------+-------+
5 | Com_select    | 4     |
6 +---------------+-------+
7 1 row in set (0.00 sec)
8 
9 mysql> 
查看類命令-服務器運行狀態

d>.刪除數據(delete)

   用法格式:“ delete from TableName where ConditionalExpression;”

 1 mysql> select * from Student;
 2 +-------+--------------+-----+--------+
 3 | StuID | Name         | Age | Gender |
 4 +-------+--------------+-----+--------+
 5 |     1 | yinzihengjie |  17 | boy    |
 6 |     2 | linus        |  48 | boy    |
 7 |     3 | dengziqi     |  26 | girl   |
 8 |     4 | jay Chou     |  38 | boy    |
 9 |     5 | joker Xue    |  34 | boy    |
10 |     6 | zhangweijian |  52 | boy    |
11 +-------+--------------+-----+--------+
12 6 rows in set (0.00 sec)
13 
14 mysql> delete from Student where name like 'z%';
15 Query OK, 1 row affected (0.00 sec)
16 
17 mysql> select * from Student;
18 +-------+--------------+-----+--------+
19 | StuID | Name         | Age | Gender |
20 +-------+--------------+-----+--------+
21 |     1 | yinzihengjie |  17 | boy    |
22 |     2 | linus        |  48 | boy    |
23 |     3 | dengziqi     |  26 | girl   |
24 |     4 | jay Chou     |  38 | boy    |
25 |     5 | joker Xue    |  34 | boy    |
26 +-------+--------------+-----+--------+
27 5 rows in set (0.00 sec)
28 
29 mysql> 
模糊匹配(like)用法展示
 1 mysql> select * from Student;
 2 +-------+--------------+-----+--------+
 3 | StuID | Name         | Age | Gender |
 4 +-------+--------------+-----+--------+
 5 |     1 | yinzihengjie |  17 | boy    |
 6 |     2 | linus        |  48 | boy    |
 7 |     3 | dengziqi     |  26 | girl   |
 8 |     4 | jay Chou     |  38 | boy    |
 9 |     5 | joker Xue    |  34 | boy    |
10 +-------+--------------+-----+--------+
11 5 rows in set (0.00 sec)
12 
13 mysql> delete from Student where Name rlike '^l.*';
14 Query OK, 1 row affected (0.00 sec)
15 
16 mysql> select * from Student;
17 +-------+--------------+-----+--------+
18 | StuID | Name         | Age | Gender |
19 +-------+--------------+-----+--------+
20 |     1 | yinzihengjie |  17 | boy    |
21 |     3 | dengziqi     |  26 | girl   |
22 |     4 | jay Chou     |  38 | boy    |
23 |     5 | joker Xue    |  34 | boy    |
24 +-------+--------------+-----+--------+
25 4 rows in set (0.00 sec)
26 
27 mysql> 
模糊匹配(rlike)用法展示 

e>.更新數據(update)

  用法格式:“update TableName set VariableType = VariableValue where ConditionalExpression;”

 1 mysql> select * from Student;
 2 +-------+--------------+-----+--------+
 3 | StuID | Name         | Age | Gender |
 4 +-------+--------------+-----+--------+
 5 |     1 | yinzihengjie |  17 | boy    |
 6 |     3 | dengziqi     |  26 | girl   |
 7 |     4 | jay Chou     |  38 | boy    |
 8 |     5 | joker Xue    |  34 | boy    |
 9 +-------+--------------+-----+--------+
10 4 rows in set (0.00 sec)
11 
12 mysql> update Student set Age=20 where Name = 'yinzihengjie';
13 Query OK, 1 row affected (0.04 sec)
14 Rows matched: 1  Changed: 1  Warnings: 0
15 
16 mysql> select * from Student;
17 +-------+--------------+-----+--------+
18 | StuID | Name         | Age | Gender |
19 +-------+--------------+-----+--------+
20 |     1 | yinzihengjie |  20 | boy    |
21 |     3 | dengziqi     |  26 | girl   |
22 |     4 | jay Chou     |  38 | boy    |
23 |     5 | joker Xue    |  34 | boy    |
24 +-------+--------------+-----+--------+
25 4 rows in set (0.00 sec)
26 
27 mysql> 
update用法展示

 

五.MySQL內置的SQL模型(SQL_MODE)

1.MySQL內置的SQL模型常用的模式

  MySQL有內置的SQL模型SQL_MODE:用來定義字符超出的操作和模仿別的數據庫的類型,通過修改全局變量。 常用的模式有:

    A>.TRADITIONAL 使用傳統模式
    B>.STRICT_TRANS_TABLES 僅對支持事務的表的嚴格模式
    C>.STRICT_ALL_TABLES 對所有表使用嚴格模式

2.設定服務器變量的值:通常僅用於支持動態的變量

 

  A>.支持修改的服務器變量
    動態變量:可以在MySQL運行時修改
    靜態變量:與配置文件中修改其值,並重啟后方能生效
  B>.服務器變量從其生效范圍來講,分兩類
    全局變量:服務器級別,修改之后僅對新建立的會話生效
    回話變量:會話級別,僅對當前會話有效
    會話建立時,從全局繼承各變量
  C>.查看服務器變量
    mysql> show [{global|session}] variables [like ''];
    mysql> select @@{globa|session}.variable_name
    mysql> mysql> select * from information_schema.GLOBAL_VARIABLES where VARIABLE_NAME='SOME_VARIABLE_NAME'; 查看全局變量
    mysql> mysql> select * from information_schema.SESSION_VARIABLES where VARIABLE_NAME='SOME_VARIABLE_NAME'; 查看會話變量

  D>.修改變量
    前提:默認僅管理員有權限修改全局變量
    mysql> set {global|session} variable_name='value';
    注意:無論是全局還是會話級別的動態變量修改,在重啟mysql后都會失效,想永久有效,可以定義在配置文件中的響應段中[mysqld]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM