mysql-5.7.20基本用法


 

第1章 安裝mysql-5.7.20

1.1 系統環境

[root@mysql ~]# cat /etc/redhat-release

CentOS Linux release 7.2.1511 (Core)

[root@mysql ~]# una

unalias  uname   

[root@mysql ~]# uname -r

3.10.0-327.el7.x86_64

[root@mysql ~]# getenforce

Disabled

[root@mysql ~]# systemctl status firewalld.service

● firewalld.service - firewalld - dynamic firewall daemon

   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)

   Active: inactive (dead)

卸載自帶mariadb

[root@mysql mysql]# rpm -qa|grep mariadb

mariadb-libs-5.5.44-2.el7.centos.x86_64

[root@mysql mysql]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

[root@mysql mysql]# rpm -qa|grep mariadb

1.2 下載軟件(二進制包)

[root@mysql ~]# mkdir  /tar

[root@mysql tar]# mkdir /app/mysql-5.7.20 -p

[root@mysql ~]# wget http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar

#可以查找自己想要的版本。此處用的是中國科學技術大學的源。

解壓到指定目錄

[root@mysql tar]# tar zxf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

[root@mysql tar]# mv mysql-5.7.20-linux-glibc2.12-x86_64 /app/mysql-5.7.20

做軟鏈接

[root@mysql tar]# ln -s /app/mysql-5.7.20/ /app/mysql

添加mysql用戶,授權

[root@mysql tar]# useradd -M -s /sbin/nologin mysql

[root@mysql tar]# chown -R mysql.mysql /app/mysql-5.7.20/

mysql初始化

[root@mysql ~]# /app/mysql/bin/mysqld --initialize --user=mysql --basedir=/app/mysql --datadir=/app/mysql/data

2018-08-09T19:21:34.385804Z 1 [Note] A temporary password is generated for root@localhost: /gevck>>s07F

初始化密碼

將mysql放到本地系統服務中

[root@mysql ~]# cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld

如果安裝到/usr/local目錄下就省去以下操作

sed -i 's#/usr/local#/app#g' /app/mysql-5.7.20/bin/mysqld_safe /etc/init.d/mysqld

啟動

[root@mysql ~]# /etc/init.d/mysqld start

Starting MySQL. SUCCESS!

添加環境變量

[root@mysql ~]# tail -1 /etc/profile

PATH=$PATH:/app/mysql/bin

[root@mysql ~]# source /etc/profile

修改密碼

用初始密碼登錄

mysql> SET PASSWORD = PASSWORD('123456');

Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;

Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

修改密碼

[root@mysql ~]# mysqladmin  -uroot -p123456 password 123

第2章 基本操作

2.1 用戶管理

權限詳解

'10.0.0.200'   ---->只允許200地址訪問我mysql

'10.0.0.%'     -----》允許這個網段的所有服務器都能訪問我

'10.0.0.5%'    ----》 允許50-59

 '%'          ----> 允許所有人

2.1.1 創建用戶

mysql> create user name2@'%' identified by "123";

mysql> create user test1@'%' identified by "123";

mysql> select user,host from mysql.user;

+---------------+------------+

| user          | host       |

+---------------+------------+

| name2         | %          |

| test1         | %          |

2.1.2 刪除用戶

mysql> select user,host from mysql.user;

mysql> select user,host from  mysql.user;

+---------------+------------+

| user          | host       |

+---------------+------------+

| name2         | %          |

| name1         | 172.16.1.% |

2.2 權限管理

2.2.1 查看當前用戶和數據庫

mysql> select user();

查看當前用戶

mysql> select database();

查看當前數據庫

mysql> grant all on  *.*  to name2@'%' identified by '123';

2.2.2 授權

給name2所有庫所有表的權限

mysql> show grants for name2;

+--------------------------------------------+

| Grants for name2@%                         |

+--------------------------------------------+

| GRANT ALL PRIVILEGES ON *.* TO 'name2'@'%' |

+--------------------------------------------+

mysql> grant all on *.* to root@'localhost' identified by '123456';

mysql> grant all on *.* to root@'10.0.0.%' identified by '123456';

mysql> grant all on *.* to root@'%' identified by "123456";

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select user.host from mysql.user where user like 'root';

+-----------+

| host      |

+-----------+

| %         |

| 10.0.0.%  |

| localhost |

2.2.3 創建用戶的同時授權

mysql> grant all on *.* to name3@'%' identified by "123";

mysql> select user,host from mysql.user;

+---------------+------------+

| user          | host       |

+---------------+------------+

| name2         | %          |

| name3         | %          |

2.2.4 授權用戶name3 select,create權限

mysql> grant select,create on *.* to name3@'%' identified by "123";

2.2.5 回收權限

mysql> revoke update,delete on *.* from name3@'%';

mysql> revoke all on *.* from name3@'%';

2.2.6 創建庫database 帶字符編碼

創建database

mysql> create database test1 charset utf8;   

mysql> show create database test1;

+----------+----------------------------------------------------------------+

| Database | Create Database                                                |

+----------+----------------------------------------------------------------+

| test1    | CREATE DATABASE `test1` /*!40100 DEFAULT CHARACTER SET utf8 */ |

+----------+----------------------------------------------------------------+

2.3 基礎知識

2.3.1 接口命令

\h 或 help 或 ?                  獲取幫助

\G                               格式化輸出(行轉列)

\T 或 tee                       記錄操作日志  tee /tmp/mysql.log

\c 或 CTRL+d                   退出mysql

\s 或 status                     查看數據庫狀態信息

\. 或 source                      mysql> source /tmp/world.sql

\!     使用shell中的命令                mysql> \! cat /etc/redhat-release    CentOS release 6.9 (Final)

2.3.2 mysqladmin用法

mysqladmin -u用戶 -p密碼 ping                         “強制回應 (Ping)”服務器。

mysqladmin -u用戶 -p密碼 shutdown                       關閉服務器。

mysqladmin -u用戶 -p密碼 create databasename            創建數據庫。

mysqladmin -u用戶 -p密碼drop databasename               刪除數據庫

mysqladmin -u用戶 -p密碼 version                        顯示服務器和版本信息

mysqladmin -u用戶 -p密碼 status                         顯示或重置服務器狀態變量

mysqladmin -u用戶 -p密碼 password                       設置口令

mysqladmin -u用戶 -p密碼 flush-privileges               重新刷新授權表。

mysqladmin -u用戶 -p密碼 flush-logs                     刷新日志文件和高速緩存。

 

第3章 基本語句

3.1 創建數據庫

mysql> create database test charset utf8;

3.2 修改存在的字符編碼

mysql> alter database test charset gbk;

3.3 查看支持的字符集和校對規則

mysql> show character set;

3.4 切換數據庫查看表

mysql> use world;

Database changed

mysql> show tables;

3.5 查看當前登錄用戶

mysql> select user();

+----------------+

| user()         |

+----------------+

| root@localhost |

+----------------+

3.6 創建表

mysql> create table t1(id int,name char(30),sex char(4));

                       id號碼  名字          性別

mysql> desc t1;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| id    | int(11)  | YES  |     | NULL    |       |

| name  | char(30) | YES  |     | NULL    |       |

| sex   | char(4)  | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

3.7 查看創建表語句

mysql> show create table t1;

| Table | Create Table                                                                                                                                     

| t1    | CREATE TABLE `t1` (

  `id` int(11) DEFAULT NULL,

  `name` char(30) DEFAULT NULL,

  `sex` char(4) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=gbk |

3.8 修改表的相關信息

3.8.1 修改表的名字

mysql> rename table t1 to t2;

Query OK, 0 rows affected (0.00 sec)

方法二。

mysql> alter table t2 rename to t1;

Query OK, 0 rows affected (0.00 sec)

3.8.2 修改表結構

添加地址一列到table t1中非

mysql> alter table t1 add addr char(40) not null;

| id    | int(11)  | YES  |     |空

mysql> desc t1; NULL    |       |

| name  | char(30) | YES  |     | NULL    |       |

| sex   | char(4)  | YES  |     | NULL    |       |

| addr  | char(40) | NO   |     | NULL    |       |

3.8.3 指定添加年齡列到name列后面的位置

mysql> alter table t1 add age int(4) after name;

mysql> desc t1;

| Field | Type     | Null | Key | Default | Extra |

| id    | int(11)  | YES  |     | NULL    |       |

| name  | char(30) | YES  |     | NULL    |       |

| age   | int(4)   | YES  |     | NULL    |       |

3.8.4 在第一列添加WeChat字段

mysql> alter table t1 add wechat int(20) first;

mysql> desc t1;

| Field  | Type     | Null | Key | Default | Extra |

| wechat | int(20)  | YES  |     | NULL    |       |

| id     | int(11)  | YES  |     | NULL    |       |

3.8.5 復制一個表格

mysql> create table t2 select * from t1;  

mysql> create table t3 like t1; 只是復制表結構。

3.8.6 同時添加多個列定義

mysql> alter table t1 add pro char(40), add qq int;

mysql> desc t1;

| Field  | Type     | Null | Key | Default | Extra |

| wechat | int(20)  | YES  |     | NULL    |       |

| id     | int(11)  | YES  |     | NULL    |       |

| name   | char(30) | YES  |     | NULL    |       |

| age    | int(4)   | YES  |     | NULL    |       |

| sex    | char(4)  | YES  |     | NULL    |       |

| addr   | char(40) | NO   |     | NULL    |       |

3.8.7 刪除表結構

mysql> alter table t1 drop addr;

3.8.8 修改表定義

mysql> alter table t1 modify wechat int(30);

mysql> desc t1;

| Field  | Type     | Null | Key | Default | Extra |

| wechat | int(30)  | YES  |     | NULL    |       |

| id     | int(11)  | YES  |     | NULL    |       |

3.8.9 修改列名

mysql> alter table t1 change name test_name char(30);

mysql> desc t1;

| Field     | Type     | Null | Key | Default | Extra |

| wechat    | int(30)  | YES  |     | NULL    |       |

| id        | int(11)  | YES  |     | NULL    |       |

| test_name | char(30) | YES  |     | NULL    |       |

3.9 DML數據操作

3.9.1 insert

mysql> desc t1;

| Field     | Type     | Null | Key | Default | Extra |

| wechat    | int(30)  | YES  |     | NULL    |       |

| id        | int(11)  | YES  |     | NULL    |       |

| test_name | char(30) | YES  |     | NULL    |       |

| age       | int(4)   | YES  |     | NULL    |       |

| sex       | char(4)  | YES  |     | NULL    |       |

| pro       | char(40) | YES  |     | NULL    |       |

| qq        | int(11)  | YES  |     | NULL    |       |

mysql> select  * from t1;

| wechat | id   | test_name | age  | sex  | pro  | qq   |

|   NULL |    1 | NULL      | NULL | NULL | NULL | NULL |

mysql> insert into t1 values(123,2,'limit2',20,'man','teacher',123);

mysql> select * from t1;

| wechat | id   | test_name | age  | sex  | pro     | qq   |

|   NULL |    1 | NULL      | NULL | NULL | NULL    | NULL |

|    123 |    2 | limit2    |   20 | man  | teacher |  123 |

在WeChat下面插入一個微信號

mysql> insert into t1(wechat)values(789);

mysql> select * from t1;

| wechat | id   | test_name | age  | sex  | pro     | qq   |

|   NULL |    1 | NULL      | NULL | NULL | NULL    | NULL |

|    123 |    2 | limit2    |   20 | man  | teacher |  123 |

|    456 |    3 | limit3    |   20 | man  | docter  |  456 |

|    789 | NULL | NULL      | NULL | NULL | NULL    | NULL |

3.9.2 插入多行數據

 

mysql> insert into t1 values(111,2,'limit4',21,'women','nurse',123),(112,2,'limit5',22,'man','woker',123),(113,2,'limit6',23,'man','SE',123);

mysql> select * from t1;                                                                                     

| wechat | id   | test_name | age  | sex   | pro     | qq   |

|   NULL |    1 | NULL      | NULL | NULL  | NULL    | NULL |

|    123 |    2 | limit2    |   20 | man   | teacher |  123 |

|    456 |    3 | limit3    |   20 | man   | docter  |  456 |

|    789 | NULL | NULL      | NULL | NULL  | NULL    | NULL |

|    111 |    2 | limit4    |   21 | women | nurse   |  123 |

|    112 |    2 | limit5    |   22 | man   | woker   |  123 |

|    113 |    2 | limit6    |   23 | man   | SE      |  123 |

3.9.3 更改表內容(要加上where條件)

mysql> update t1 set test_name='limit7' where test_name='limit6';

mysql> select * from t1;

|    113 |    2 | limit7    |   23 | man   | SE      |  123 |

3.9.4 刪除表內容

mysql> select * from t1;

| wechat | id   | test_name | age  | sex   | pro     | qq   |

|   NULL |    1 | NULL      | NULL | NULL  | NULL    | NULL |

|    123 |    2 | limit2    |   20 | man   | teacher |  123 |

|    456 |    3 | limit3    |   20 | man   | docter  |  456 |

|    789 | NULL | NULL      | NULL | NULL  | NULL    | NULL |

|    111 |    2 | limit4    |   21 | women | nurse   |  123 |

|    112 |    2 | limit5    |   22 | man   | woker   |  123 |

|    113 |    2 | limit7    |   23 | man   | SE      |  123 |

刪除WeChat=798一行

mysql> delete from t1 where wehchat='789';  必須加上where條件

mysql> select * from t1;

| wechat | id   | test_name | age  | sex   | pro     | qq   |

|   NULL |    1 | NULL      | NULL | NULL  | NULL    | NULL |

|    123 |    2 | limit2    |   20 | man   | teacher |  123 |

|    456 |    3 | limit3    |   20 | man   | docter  |  456 |

|    111 |    2 | limit4    |   21 | women | nurse   |  123 |

|    112 |    2 | limit5    |   22 | man   | woker   |  123 |

|    113 |    2 | limit7    |   23 | man   | SE      |  123 |

================================================================================

mysql> select * from t1;

| wechat | id   | test_name | age  | sex   | pro     | qq   |

|    123 |    2 | limit2    |   20 | man   | teacher |  123 |

|    456 |    3 | limit3    |   20 | man   | docter  |  456 |

|    111 |    2 | limit4    |   21 | women | nurse   |  123 |

|    112 |    2 | limit5    |   22 | man   | woker   |  123 |

|    113 |    2 | limit7    |   23 | man   | SE      |  123 |

刪除id=2的數據

mysql> delete from t1 where id=2;

mysql> select * from t1;

| wechat | id   | test_name | age  | sex  | pro    | qq   |

|    456 |    3 | limit3    |   20 | man  | docter |  456 |

================================================================================================

truncate table test;  #物理刪除,pages(block),效率高。

3.10 DQL數據查詢語言標准語法

3.10.1 查看用戶連接信息

mysql> select user,host,authentication_string from mysql.user; 

mysql5.7之后password改為了authentication_string

| user          | host       | authentication_string                     |

| root          | localhost  | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |

| mysql.session | localhost  | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |

| mysql.sys     | localhost  | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |

| name1         | 172.16.1.% | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |

| root          | 10.0.0.%   | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| name2         | %          | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |

3.10.2 查看所有信息關於test庫下t1表格

mysql> select * from test.t1;

| wechat | id   | test_name | age  | sex   | pro     | qq   |

|   NULL |    1 | NULL      | NULL | NULL  | NULL    | NULL |

|    123 |    2 | limit2    |   20 | man   | teacher |  123 |

|    456 |    3 | limit3    |   20 | man   | docter  |  456 |

|    789 | NULL | NULL      | NULL | NULL  | NULL    | NULL |

|    111 |    2 | limit4    |   21 | women | nurse   |  123 |

|    112 |    2 | limit5    |   22 | man   | woker   |  123 |

|    113 |    2 | limit6    |   23 | man   | SE      |  123 |

3.10.3 查找年齡為21的人的id和WeChat號碼

mysql> select wechat,id from test.t1 where age=21;

| wechat | id   |

|    111 |    2 |

3.10.4 查找年齡大於21的人的id和WeChat號碼

mysql> select wechat,id from test.t1 where age>21;

| wechat | id   |

|    112 |    2 |

|    113 |    2 |

3.10.5 or和and

mysql> select wechat,id from test.t1 where age>21 and wechat>112;

| wechat | id   |

|    113 |    2 |

mysql> select wechat,id from test.t1 where age>21 or wechat>112;

| wechat | id   |

|    123 |    2 |

|    456 |    3 |

|    789 | NULL |

|    112 |    2 |

|    113 |    2 |

3.10.6 排序

mysql> select wechat,id,test_name  from test.t1 order by id asc; 小到大

| wechat | id   | test_name |

|    789 | NULL | NULL      |

|   NULL |    1 | NULL      |

|    123 |    2 | limit2    |

|    111 |    2 | limit4    |

|    112 |    2 | limit5    |

|    113 |    2 | limit6    |

|    456 |    3 | limit3    |

7 rows in set (0.00 sec)

mysql> select wechat,id,test_name  from test.t1 order by id desc;  大到小

| wechat | id   | test_name |

|    456 |    3 | limit3    |

|    123 |    2 | limit2    |

|    111 |    2 | limit4    |

|    112 |    2 | limit5    |

|    113 |    2 | limit6    |

|   NULL |    1 | NULL      |

|    789 | NULL | NULL      |

3.10.7 顯示第二行后的六行(這里着五行了所有全部顯示粗來)和desc和asc

mysql> select wechat,id,test_name  from test.t1 order by id desc limit 2,6;

| wechat | id   | test_name |

|    111 |    2 | limit4    |

|    112 |    2 | limit5    |

|    113 |    2 | limit6    |

|   NULL |    1 | NULL      |

|    789 | NULL | NULL      |

3.11 安全操作設置(防止不加where刪除過多的數據)

[root@mysql ~]# mysql -uroot -p123

mysql> select * from t3;

| wechat | id   | test_name | age  | sex   | pro     | qq   |

|   NULL |    1 | NULL      | NULL | NULL  | NULL    | NULL |

|    123 |    2 | limit2    |   20 | man   | teacher |  123 |

|    456 |    3 | limit3    |   20 | man   | docter  |  456 |

|    789 | NULL | NULL      | NULL | NULL  | NULL    | NULL |

|    111 |    2 | limit4    |   21 | women | nurse   |  123 |

|    112 |    2 | limit5    |   22 | man   | woker   |  123 |

|    113 |    2 | limit6    |   23 | man   | SE      |  123 |

mysql> delete from t3;

mysql> select * from t3;

Empty set (0.00 sec)

整個表格的內容都沒了

==============================================================================================

登錄的時候加上“-U”參數

[root@mysql ~]# mysql -uroot -p123 -U

mysql> select * from test.t2;

| wechat | id   | test_name | age  | sex   | pro     | qq   |

|   NULL |    1 | NULL      | NULL | NULL  | NULL    | NULL |

|    123 |    2 | limit2    |   20 | man   | teacher |  123 |

|    456 |    3 | limit3    |   20 | man   | docter  |  456 |

|    789 | NULL | NULL      | NULL | NULL  | NULL    | NULL |

|    111 |    2 | limit4    |   21 | women | nurse   |  123 |

|    112 |    2 | limit5    |   22 | man   | woker   |  123 |

|    113 |    2 | limit6    |   23 | man   | SE      |  123 |

7 rows in set (0.00 sec)

mysql> delete from test.t2;

ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

提示你使用了一個安全的update設置,你沒用加上where使用

為了安全設置mysql-U為別名

[root@mysql ~]# echo "alias mysql='mysql -U'" >> /etc/profile

[root@mysql ~]# source /etc/profile

[root@mysql ~]# mysql -uroot -p123

mysql> delete from test.t2;

ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

 

第4章  數據類型

4.1.1 數值數據類型

整數        TINYINT               極小整數數據類型(0-255)

整數       SMALLINT              較小整數數據類型(-2^15 到2^15-1)

整數       MEDIUMINT            中型整數數據類型

整數       INT常規(平均)        大小的整數數據類型(-2^31 到2^31-1)

整數         BIGINT                較大整數數據類型(-2^63到2^63-1)

浮點數       FLOAT                小型單精度(四個字節)浮點數

浮點數      DOUBLE               常規雙精度(八個字節)浮點數

定點數      DECIMAL              包含整數部分、小數部分或同時包括二者的精確值數值

 

4.1.2 字符串數據類型

文本         CHAR           固定長度字符串,最多為255 個字符

文本          VARCHAR      可變長度字符串,最多為65,535 個字符

文本          TINYTEXT      可變長度字符串,最多為255 個字符

文本          TEXT           可變長度字符串,最多為65,535 個字符

文本          MEDIUMTEXT   可變長度字符串,最多為16,777,215 個字符

文本          LONGTEXT      可變長度字符串,最多為4,294,967,295 個字符

整數          ENUM           由一組固定的合法值組成的枚舉

整數          SET             由一組固定的合法值組成的集

 

4.1.3 二進制數據類型

二進制  BINARY類似於 CHAR(固定長度)類型,          但存儲的是二進制字節字符串,

二進制  VARBINARY類似於 VARCHAR(可變長度)類型,  但存儲的是二進制字節字符串,

BLOB     TINYBLOB        最大長度為255 個字節的 BLOB 列

BLOB     BLOB             最大長度為65,535 個字節的 BLOB 列

BLOB     MEDIUDMBLOB   最大長度為16,777,215 個字節的 BLOB 列

BLOB     LONGBLOB       最大長度為4,294,967,295 個字節的 BLOB 列

4.1.4 時間數據類型

DATE            YYYY-MM-DD2                     017-12-16

TIME            hh:mm:ss[.uuuuuu]                   12:59:02.123456

DATE            TIMEYYYY-MM-DD hh:mm:ss[.uuuuuu]2017-12-16 12:59:02.123

TIME            STAMPYYYY-MM-DD hh:mm:ss[.uuuuuu]2017-12-16 12:59:02.12

YEAR            YYYY                               2017

4.1.5 列屬性

數值               UNSIGNED                    禁止使用負值

僅整數             AUTO_INCREMENT            生成包含連續唯一整數值的序列

字符串             CHARACTER SET              指定要使用的字符集

字符串             COLLATE                     指定字符集整理

字符串             BINARY                       指定二進制整理

全部*             NULL 或 NOT NULL            指示列是否可以包含 NULL 值

全部             DEFAULT            如果未為新記錄指定值,則為其提供默認值

第5章 導入練習文件world

導入練習文件

官網

https://dev.mysql.com/doc/world-setup/en/world-setup-installation.html

官網導入方法

https://dev.mysql.com/doc/world-setup/en/world-setup-installation.html

[root@mysql ~]# ll world.sql

-rw-r--r-- 1 root root 397334 4月   2 22:58 world.sql

使用非交互式:(盡量避免使用mysql 導入數據,會產生大量的無用日志)

[root@mysql ~]# mysql -uroot -p123  </root/world.sql

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| world              |

使用Navicat或者SQLyog 鏈接 (前提要給用戶授權才能鏈接)

5.1 select用法

邏輯                                     操作符說明

and邏輯與        只有當所有的子條件都為true時,and才返回true。否則返回false或null

or邏輯或         只要有一個子條件為true,or就返回true。否則返回false或null

not邏輯非        如果子條件為true,則返回false;如果子條件為false,則返回true

xor邏輯異        或當一個子條件為true而另一個子條件為false時,其結果為true;

當兩個條件都為true或都為false時,結果為false。否則,結果為null

5.1.1 從數據庫中查找是中國的並且是山西的城市

mysql> select * from world.city where countrycode='chn' and district='shanxi';

| ID   | Name     | CountryCode | District | Population |

| 1908 | Taiyuan  | CHN         | Shanxi   |    1968400 |

| 1931 | Datong   | CHN         | Shanxi   |     800000 |

| 1986 | Yangquan | CHN         | Shanxi   |     362268 |

| 2000 | Changzhi | CHN         | Shanxi   |     317144 |

| 2078 | Yuci     | CHN         | Shanxi   |     191356 |

| 2083 | Linfen   | CHN         | Shanxi   |     187309 |

| 2156 | Jincheng | CHN         | Shanxi   |     136396 |

| 2212 | Yuncheng | CHN         | Shanxi   |     108359 |

| 2233 | Xinzhou  | CHN         | Shanxi   |      98667 |

5.1.2 查找一百萬到一百零一萬人口的城市(范圍)

mysql> select *  from city where population between 1000000 and 1010000;

| ID   | Name    | CountryCode | District | Population |

| 1466 | Napoli  | ITA         | Campania |    1002619 |

| 1786 | Amman   | JOR         | Amman    |    1000000 |

| 2524 | Zapopan | MEX         | Jalisco  |    1002239 |

| 3591 | Perm    | RUS         | Perm     |    1009700 |

5.1.3 查找id為1  3  5的城市(in的用法)

mysql> select * from city where id in (1,5,3);

| ID | Name      | CountryCode | District      | Population |

|  1 | Kabul     | AFG         | Kabol         |    1780000 |

|  3 | Herat     | AFG         | Herat         |     186800 |

|  5 | Amsterdam | NLD         | Noord-Holland |     731200 |

5.1.4 查找所有一jp開頭的城市信息(like的用法)

mysql> select * from city where countrycode like 'jp%'; 

%匹配所有   _ 只表示一個任意字符

 

| ID   | Name                | CountryCode | District  | Population |

| 1532 | Tokyo               | JPN         | Tokyo-to  |    7980230 |

| 1533 | Jokohama [Yokohama] | JPN         | Kanagawa  |    3339594 |

| 1534 | Osaka               | JPN         | Osaka     |    2595674 |

| 1535 | Nagoya              | JPN         | Aichi     |    2154376 |

| 1536 | Sapporo             | JPN         | Hokkaido  |    1790886 |

5.1.5 order by的用法

asc                執行升序排序。默認值

desc               執行降序排序

使用方法:         ORDER BY子句一般在SELECT語句的最后面

在MySQL中,把NULL值當做一列值中的最小值對待。因此,升序排序時,它出現在最前面

mysql> select * from city where id in (1,8,9,31) order by population desc; 降序

| ID | Name      | CountryCode | District      | Population |

|  1 | Kabul     | AFG         | Kabol         |    1780000 |

|  8 | Utrecht   | NLD         | Utrecht       |     234323 |

|  9 | Eindhoven | NLD         | Noord-Brabant |     201843 |

| 31 | Heerlen   | NLD         | Limburg       |      95052 |

4 rows in set (0.01 sec)

mysql> select * from city where id in (1,8,9,31) order by population asc;   升序(默認是升序)

| ID | Name      | CountryCode | District      | Population |

| 31 | Heerlen   | NLD         | Limburg       |      95052 |

|  9 | Eindhoven | NLD         | Noord-Brabant |     201843 |

|  8 | Utrecht   | NLD         | Utrecht       |     234323 |

|  1 | Kabul     | AFG         | Kabol         |    1780000 |

mysql> select * from city where id in (1,8,9,31) order by population,countrycode;

以人口和國家排序

| ID | Name      | CountryCode | District      | Population |

| 31 | Heerlen   | NLD         | Limburg       |      95052 |

|  9 | Eindhoven | NLD         | Noord-Brabant |     201843 |

|  8 | Utrecht   | NLD         | Utrecht       |     234323 |

|  1 | Kabul     | AFG         | Kabol         |    1780000 |

mysql> select * from city where id in (1,8,9,31) order by 3;

以第三列國家排序

| ID | Name      | CountryCode | District      | Population |

|  1 | Kabul     | AFG         | Kabol         |    1780000 |

|  8 | Utrecht   | NLD         | Utrecht       |     234323 |

|  9 | Eindhoven | NLD         | Noord-Brabant |     201843 |

| 31 | Heerlen   | NLD         | Limburg       |      95052 |

5.1.6 limit用法

它是SELECT語句中的最后一個子句(在order by后面)。

它用來表示從結果集中選取最前面或最后面的幾行。

mysql> select * from city where id in (1,8,9,31) order by 3 limit 2;

只是選出前兩行。

| ID | Name    | CountryCode | District | Population |

|  1 | Kabul   | AFG         | Kabol    |    1780000 |

|  8 | Utrecht | NLD         | Utrecht  |     234323 |

limit  <獲取的行數> [OFFSET <跳過的行數>]

或者

limit [<跳過的行數>,] <獲取的行數> 

mysql> select * from city where id in (1,8,9,31) order by 3 limit   2,2;

| ID | Name      | CountryCode | District      | Population |

|  9 | Eindhoven | NLD         | Noord-Brabant |     201843 |

| 31 | Heerlen   | NLD         | Limburg       |      95052 |

mysql> select * from city where id in (1,8,9,31) order by 3 limit   2 offset 1;

| ID | Name      | CountryCode | District      | Population |

|  8 | Utrecht   | NLD         | Utrecht       |     234323 |

|  9 | Eindhoven | NLD         | Noord-Brabant |     201843 |

5.1.7 natural join

自動到兩張表中查找所有同名同類型的列拿來做連接列,進行相等連接

使用natural join 進行相等連接,兩個表,條件為人口大於1000000的,進行升序排列。

mysql> select name,id,name,countrycode,population,district from city natural join countrylanguage  where popullation > 10000000 order by language;

 

5.1.8 using

mysql> select name,id,countrycode,population,language from city join countrylanguage

使用join進行兩表的來連接,using指定countrycode為關聯列。

using(countrycode);

| name                           | id  | countrycode | population | language         |

| Kabul                          |   1 | AFG         |    1780000 | Balochi          |

第6章 information_schema元數據

查詢 INFORMATION_SCHEMA 數據庫表。其中包含 MySQL 數據庫服務器所管理的所有對象的相關數據

使用 SHOW 語句。用於獲取數據庫和表信息的 MySQL 專用語句

使用 DESCRIBE(或 DESC)語句。用於檢查表結構和列屬性的快捷方式

使用 mysqlshow 客戶端程序。SHOW 語法的命令行程序

INFORMATION_SCHEMA 數據庫優點介紹:

充當數據庫元數據的中央系統信息庫,模式和模式對象,服務器統計信息(狀態變量、設置、連接) 。采用表格式以實現靈活訪問,使用任意 SELECT 語句。是“虛擬數據庫”,表並非“真實”表(基表),而是“系統視圖”,根據當前用戶的特權動態填充表。

 

6.1 INFORMATION_SCHEMA 數據庫中所有的表

mysql>  USE information_schema;

mysql> show tables;

| Tables_in_information_schema          |

| CHARACTER_SETS                        |

| COLLATIONS                            |

| COLLATION_CHARACTER_SET_APPLICABILITY |

6.2 查找引擎是innodb的表

mysql> select table_name, engine from information_schema.tables where engine='innodb';

| table_name                | engine |

| COLUMNS                   | InnoDB |

| t1                        | InnoDB |

| t2                        | InnoDB |

| city                      | InnoDB |

| country                   | InnoDB |

| countrylanguage           | InnoDB |

6.3 查找數據類型是set的表

mysql> select table_schema,table_name, column_name from information_schema.columns where 

data_type='set';

+--------------+--------------+-------------+

| table_schema | table_name   | column_name |

+--------------+--------------+-------------+

| mysql        | columns_priv | Column_priv |

| mysql        | event        | sql_mode    |

| mysql        | proc         | sql_mode    |

| mysql        | procs_priv   | Proc_priv   |

6.4 查看找默認為yes的表

mysql> select character_set_name,collation_name,is_default from  information_schema.collations where

is_default='yes';

6.5 查看每個數據庫下表的個數

mysql> select table_schema,count(*) from information_schema.tables group by table_schema;

| table_schema       | count(*) |

| information_schema |       61 |

| mysql              |       31 |

| performance_schema |       87 |

| sys                |      101 |

| test               |        2 |

| world              |        3 |


免責聲明!

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



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