MySQL客户端工具的使用

1、MySQL程序的组成

  • 客户端

    • mysql:CLI交互式客户端程序
    • mycli:CLI交互式客户端程序;使用sql语句时会有提示信息
    • mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
    • mysqldump:mysql备份工具
    • mysqladmin:官方提供的shell命令行工具
  • 服务器端

    • mysqld

2、MySQL监听地址

服务器监听的两种socket地址:

socket类型 说明
ip socket 默认监听在tcp的3306端口,支持远程通信
unix sock 监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock)仅支持本地通信

3、数据库配置文件

数据库配置文件为:/etc/my.cnf和/etc/my.cnf.d目录下的配置文件

//修改配置文件,配置字符编码
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci [client]
default-character-set=utf8mb4 [mysql]
default-character-set=utf8mb4

4、客户端工具的使用

//语法:mysql [OPTIONS] [database]
//常用的OPTIONS:
-uUSERNAME //指定用户名,默认为root
-hHOST //指定服务器主机,默认为localhost,推荐使用ip地址
-pPASSWORD //指定用户的密码
-P //指定数据库监听的端口,如-P3307
-S //指定套接字文件位置,多实例部署MySQL时需要使用
-V //查看当前使用的mysql版本
-e //不登录mysql执行sql语句后退出,常用于脚本
--defaults-file=配置文件 //指定MySQL配置文件位置,用于加载客户端配置 [root@localhost ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.38, for Linux (x86_64) using EditLine wrapper [root@localhost ~]# mysql -uroot -pPasswd123! -h127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.38 MySQL Community Server (GPL) Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
//注意,不推荐直接在命令行里直接用-pPASSWORD的方式登录,而是使用-p选项,然后交互式输入密码 [root@localhost ~]# mysql -uroot -p -h127.0.0.1 -e 'show databases;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zsl |
+--------------------+

MySQL 数据类型

MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。

1、数值类型

MySQL 支持所有标准 SQL 数值数据类型。

这些类型包括严格数值数据类型(INTEGER、SMALLINT、DECIMAL 和 NUMERIC),以及近似数值数据类型(FLOAT、REAL 和 DOUBLE PRECISION)。

类型 大小 范围(有符号) 范围(无符号) 用途
TINYINT 1 Bytes (-128,127) (0,255) 小整数值
SMALLINT 2 Bytes (-32 768,32 767) (0,65 535) 大整数值
MEDIUMINT 3 Bytes (-8 388 608,8 388 607) (0,16 777 215) 大整数值
INT或INTEGER 4 Bytes (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整数值
BIGINT 8 Bytes (-9,223,372,036,854,775,808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 极大整数值
FLOAT 4 Bytes (-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) 0,(1.175 494 351 E-38,3.402 823 466 E+38) 单精度 浮点数值
DOUBLE 8 Bytes (-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 双精度 浮点数值
DECIMAL 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2 依赖于M和D的值 依赖于M和D的值 小数值

2、日期和时间类型

表示时间值的日期和时间类型为DATETIME、DATE、TIMESTAMP、TIME和YEAR。

每个时间类型有一个有效值范围和一个"零"值,当指定不合法的MySQL不能表示的值时使用"零"值。

TIMESTAMP类型有专有的自动更新特性。

类型 大小 ( bytes) 范围 格式 用途
DATE 3 1000-01-01/9999-12-31 YYYY-MM-DD 日期值
TIME 3 '-838:59:59'/'838:59:59' HH:MM:SS 时间值或持续时间
YEAR 1 1901/2155 YYYY 年份值
DATETIME 8 '1000-01-01 00:00:00' 到 '9999-12-31 23:59:59' YYYY-MM-DD hh:mm:ss 混合日期和时间值
TIMESTAMP 4 '1970-01-01 00:00:01' UTC 到 '2038-01-19 03:14:07' UTC结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07 YYYY-MM-DD hh:mm:ss 混合日期和时间值,时间戳

3、字符串类型

字符串类型指CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT、ENUM和SET。

类型 大小 用途
CHAR 0-255 bytes 定长字符串
VARCHAR 0-65535 bytes 变长字符串
TINYBLOB 0-255 bytes 不超过 255 个字符的二进制字符串
TINYTEXT 0-255 bytes 短文本字符串
BLOB 0-65 535 bytes 二进制形式的长文本数据
TEXT 0-65 535 bytes 长文本数据
MEDIUMBLOB 0-16 777 215 bytes 二进制形式的中等长度文本数据
MEDIUMTEXT 0-16 777 215 bytes 中等长度文本数据
LONGBLOB 0-4 294 967 295 bytes 二进制形式的极大文本数据
LONGTEXT 0-4 294 967 295 bytes 极大文本数据

注意

char(n) 和 varchar(n) 中括号中 n 代表字符的个数,并不代表字节个数,比如 CHAR(30) 就可以存储 30 个字符。

CHAR 和 VARCHAR 类型类似,但它们保存和检索的方式不同。它们的最大长度和是否尾部空格被保留等方面也不同。在存储或检索过程中不进行大小写转换。

BINARY 和 VARBINARY 类似于 CHAR 和 VARCHAR,不同的是它们包含二进制字符串而不要非二进制字符串。也就是说,它们包含字节字符串而不是字符字符串。这说明它们没有字符集,并且排序和比较基于列值字节的数值值。

BLOB 是一个二进制大对象,可以容纳可变数量的数据。有 4 种 BLOB 类型:TINYBLOB、BLOB、MEDIUMBLOB 和 LONGBLOB。它们区别在于可容纳存储范围不同。

有 4 种 TEXT 类型:TINYTEXT、TEXT、MEDIUMTEXT 和 LONGTEXT。对应的这 4 种 BLOB 类型,可存储的最大长度不同,可根据实际情况选择。

MySQL SQL语句

1、DDL操作

1.1 数据库DDL操作
//创建数据库
//语法:create database [if not exists] 'da_name'; //创建数据库zsl
mysql> create database zsl;
Query OK, 1 row affected (0.00 sec) //查看当前实例有哪些数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zsl |
+--------------------+
5 rows in set (0.00 sec) //删除数据库
//语法:drop database [if exists] 'da_name'; //删除数据库zsl
mysql> drop database zsl;
Query OK, 0 rows affected (0.00 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec) //创建数据库时,设置数据库的字符集:
//character set:指定数据库采用的字符集,utf8不能写成utf-8,建议使用utf8mb4字符集
//collate:指定数据库字符集的排序规则,utf8mb4的默认排序规则为utf8mb4_general_ci(通过show character set查看)
mysql> create database dbtest character set utf8mb4 collate utf8mb4_general_ci; //数据库客户端字符编码需要和服务端字符集保持一致
//SET NAMES:指定客户端字符集
mysql> SET NAMES utf8mb4; //字符集配置都可写入MySQL配置文件中启动MySQL服务时自动加载
1.2 表DDL操作
//创建表
//语法:create table table_name (col1 datatype 修饰符,col2 datatype 修饰符) ENGINE='存储引擎类型'; //在数据库lsz里创建表lsztable
mysql> CREATE DATABASE IF NOT EXISTS lsz; //创建数据库lsz
Query OK, 1 row affected (0.00 sec) mysql> use lsz; //进入lsz数据库
Database changed //创建lsztable表
mysql> create table lsztable (id int(10) not null,name varchar(100) not null,age tinyint);
Query OK, 0 rows affected (0.00 sec) //查看当前数据库有哪些表
mysql> show tables;
+---------------+
| Tables_in_lsz |
+---------------+
| lsztable |
+---------------+
1 row in set (0.00 sec) //删除表
//语法:drop table [ if exists ] 'table_name'; //删除表lsztable
mysql> drop table lsztable;
Query OK, 0 rows affected (0.01 sec) mysql> show tables;
Empty set (0.00 sec) //创建表时,设置字段、表的字符编码
mysql> create table tb_course(id int not null primary key auto_increment,course_name vaarchar(50)) default character set utf8 collate utf8_general-ci;
1.3 用户操作

MySQL用户帐号由两部分组成,如'USERNAME'@'HOST',表示此USERNAME只能从此HOST上远程登录;HOST用于限制此用户可通过哪些主机远程连接mysql程序。

HOST的值可为:

  • IP地址,如:172.16.12.129
  • 通配符
    • %:匹配任意长度的任意字符,常用于设置允许从任何主机登录
    • _:匹配任意单个字符
//数据库用户创建
//语法:create user 'username'@'host' [identified by 'password']; //创建数据库用户zsl
mysql> create user 'zsl'@'127.0.0.1' identified by 'Passwd123!';
Query OK, 0 rows affected (0.00 sec) //使用新创建的用户和密码登录
[root@localhost ~]# mysql -uzsl -pPasswd123! -h127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.38 MySQL Community Server (GPL) Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> //删除数据库用户
//语法:drop user 'username'@'host'; //删除数据库用户zsl
mysql> drop user 'zsl'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)
1.4 查看命令show
mysql> show character set;		//查看支持的所有字符集
+----------+---------------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+---------------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
......
...... mysql> show variables like '%char%'; //查看客户端的字符编码
+--------------------------------------+----------------------------+
| Variable_name | Value |
+--------------------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
| validate_password_special_char_count | 1 |
+--------------------------------------+----------------------------+
9 rows in set (0.00 sec) mysql> select charset(id) from lsztable; //查看某表中某字段使用的字符编码
......
...... mysql> show engines; //查看当前数据库支持的所有存储引擎
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec) mysql> show databases; //查看数据库信息
+--------------------+
| Database |
+--------------------+
| information_schema |
| lsz |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec) mysql> show tables from lsz; //不进入某数据库而列出其包含的所有表
+---------------+
| Tables_in_lsz |
+---------------+
| lsztable |
+---------------+
1 row in set (0.00 sec) //查看表结构
//语法:desc [db_name.]table_name; mysql> desc lsz.lsztable;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(10) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec) //查看某表的创建命令,可以看到创建表时设置的参数
//语法:show create table table_name; mysql> show create table lsz.lsztable;
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
| lsztable | CREATE TABLE `lsztable` (
`id` int(10) NOT NULL,
`name` varchar(100) NOT NULL,
`age` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) //查看某表的状态
//语法:show table status like 'table_name'\G mysql> show table status like 'lsztable'\G;
*************************** 1. row ***************************
Name: lsztable
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2022-07-26 08:40:01
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec) ERROR:
No query specified
1.5 alter修改操作
//修改数据库属性
//语法:alter adtabase 'db_name' character set charset_name | collate collation_name //修改数据库字符集为utf8
mysql> alter database lsz character set utf8;
Query OK, 1 row affected (0.01 sec) //修改表
//语法:alter table <table_name> [option] //给lsztable表添加新的一列,先查看表结构
mysql> desc lsztable;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(10) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec) //给lsztable表添加一列'phone'
mysql> alter table lsztable add phone int(11);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0 //查看表结构
mysql> desc lsztable;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(10) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| phone | int(11) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec) //修改lsztable表中'phone'列为'sex'
mysql> alter table lsztable change phone sex varchar(4);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc lsztable;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(10) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| sex | varchar(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec) //修改表名为zsltable
mysql> alter table lsztable rename to zsltable;
Query OK, 0 rows affected (0.00 sec) mysql> show tables;
+---------------+
| Tables_in_lsz |
+---------------+
| zsltable |
+---------------+
1 row in set (0.00 sec) //修改数据库字符编码
mysql> altar database dbtest character set utf8 collate utf8_general_ci; //修改表字符编码
mysql> altar table tbtest character set utf8 collate utf8_general_ci;
1.6 获取帮助
//获取命令使用帮助
//语法:help commond; mysql> help create table; //获取创建表的帮助
Name: 'CREATE TABLE'
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[partition_options] CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options]
[partition_options]
[IGNORE | REPLACE]
[AS] query_expression
......
......

2、DML操作

DML操作包括增(INSERT)、删(DELETE)、改(UPDATE)、查(SELECT),均属针对表的操作。

2.1 insert语句
//DML操作之增操作insert
//语法:inster [into] table_name [(column_name,...)] {values | value} (value1,...),(...),... mysql> use lsz;
Database changed //一次插入一条记录
mysql> insert zsltable (id,name,age,sex) value(1,'tom',20,'nan');
Query OK, 1 row affected (0.00 sec) //一次插入多条记录
mysql> insert zsltable (id,name,age,sex) values(2,'jerry',23,'nv'),(3,'zsl',18,'nan'),(4,'sean',null,'nv');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
2.2 select语句

字段column匹配方式:

表示符 含义
* 所有字段
as 字段别名,如col1 AS alias1 当表名很长时用别名代替

条件判断语句WHERE:

操作类型 常用操作符
操作符 >,<,>=,<=,=,!=
BETWEEN column# AND column#
LIKE:模糊匹配 RLIKE:基于正则表达式进行模式匹配
IS NOT NULL:非空
IS NULL:空
条件逻辑操作 AND
OR
NOT

ORDER BY:排序,默认为升序(ASC)

ORDER BY语句 意义
ORDER BY ‘column_name' 根据column_name进行升序排序
ORDER BY 'column_name' DESC 根据column_name进行降序排序
ORDER BY ’column_name' LIMIT 2 根据column_name进行升序排序 并只取前2个结果
ORDER BY ‘column_name' LIMIT 1,2 根据column_name进行升序排序 并且略过第1个结果取后面的2个结果
/DML操作之查操作select
//语法:SELECT column1,column2,... FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n]; #进入数据库
mysql> use lsz;
Database changed #查看表所有的内容
mysql> select * from zsltable;
+----+-------+------+------+
| id | name | age | sex |
+----+-------+------+------+
| 1 | tom | 20 | nan |
| 2 | jerry | 23 | nv |
| 3 | zsl | 18 | nan |
| 4 | sean | NULL | nv |
+----+-------+------+------+
4 rows in set (0.00 sec) #查看表的name字段内容
mysql> select name from zsltable;
+-------+
| name |
+-------+
| tom |
| jerry |
| zsl |
| sean |
+-------+
4 rows in set (0.00 sec) #根据age字段内容从低到高排列
mysql> select * from zsltable order by age;
+----+-------+------+------+
| id | name | age | sex |
+----+-------+------+------+
| 4 | sean | NULL | nv |
| 3 | zsl | 18 | nan |
| 1 | tom | 20 | nan |
| 2 | jerry | 23 | nv |
+----+-------+------+------+
4 rows in set (0.00 sec) #根据age字段内容从高到低排列
mysql> select * from zsltable order by age desc;
+----+-------+------+------+
| id | name | age | sex |
+----+-------+------+------+
| 2 | jerry | 23 | nv |
| 1 | tom | 20 | nan |
| 3 | zsl | 18 | nan |
| 4 | sean | NULL | nv |
+----+-------+------+------+
4 rows in set (0.00 sec) #根据age字段内容从低到高排列取前两行
mysql> select * from zsltable order by age limit 2;
+----+------+------+------+
| id | name | age | sex |
+----+------+------+------+
| 4 | sean | NULL | nv |
| 3 | zsl | 18 | nan |
+----+------+------+------+
2 rows in set (0.00 sec) #根据age字段内容从低到高排列,跳过第一行取下一行
mysql> select * from zsltable order by age limit 1,1;
+----+------+------+------+
| id | name | age | sex |
+----+------+------+------+
| 3 | zsl | 18 | nan |
+----+------+------+------+
1 row in set (0.00 sec) #查看表中年龄大于等于20的行;
mysql> select * from zsltable where age >=20;
+----+-------+------+------+
| id | name | age | sex |
+----+-------+------+------+
| 1 | tom | 20 | nan |
| 2 | jerry | 23 | nv |
+----+-------+------+------+
2 rows in set (0.00 sec) #查看表中年龄大于等于20且名字叫jerry的行;
mysql> select * from zsltable where age >=20 and name = 'jerry';
+----+-------+------+------+
| id | name | age | sex |
+----+-------+------+------+
| 2 | jerry | 23 | nv |
+----+-------+------+------+
1 row in set (0.00 sec) #查看表中年龄18到23的行
mysql> select * from zsltable where age between 18 and 23;
+----+-------+------+------+
| id | name | age | sex |
+----+-------+------+------+
| 1 | tom | 20 | nan |
| 2 | jerry | 23 | nv |
| 3 | zsl | 18 | nan |
+----+-------+------+------+
3 rows in set (0.00 sec) #查看表中年龄不为空的
mysql> select * from zsltable where age is not null;
+----+-------+------+------+
| id | name | age | sex |
+----+-------+------+------+
| 1 | tom | 20 | nan |
| 2 | jerry | 23 | nv |
| 3 | zsl | 18 | nan |
+----+-------+------+------+
3 rows in set (0.00 sec) #查看表中年龄为空的
mysql> select * from zsltable where age is null;
+----+------+------+------+
| id | name | age | sex |
+----+------+------+------+
| 4 | sean | NULL | nv |
+----+------+------+------+
1 row in set (0.00 sec) #查找name字段含有z的行
mysql> select * from zsltable where name like '%z%';
+----+------+------+------+
| id | name | age | sex |
+----+------+------+------+
| 3 | zsl | 18 | nan |
+----+------+------+------+
1 row in set (0.00 sec)
2.3 update语句
//DML操作之改操作update
//语法:UPDATE table_name SET column1 = new_value1[,column2 = new_value2,...] [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n]; mysql> select * from zsltable;
+----+-------+------+------+
| id | name | age | sex |
+----+-------+------+------+
| 1 | tom | 20 | nan |
| 2 | jerry | 23 | nv |
| 3 | zsl | 18 | nan |
| 4 | sean | NULL | nv |
+----+-------+------+------+
4 rows in set (0.00 sec) mysql> update zsltable set age=25 where name = 'sean';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from zsltable where name = 'sean';
+----+------+------+------+
| id | name | age | sex |
+----+------+------+------+
| 4 | sean | 25 | nv |
+----+------+------+------+
1 row in set (0.00 sec)
2.4 delete语句
//DML操作之删操作delete
//语法:DELETE FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n]; mysql> select * from zsltable;
+----+-------+------+------+
| id | name | age | sex |
+----+-------+------+------+
| 1 | tom | 20 | nan |
| 2 | jerry | 23 | nv |
| 3 | zsl | 18 | nan |
| 4 | sean | 25 | nv |
+----+-------+------+------+
4 rows in set (0.00 sec) mysql> delete from zsltable where id =3; //删除某条记录
Query OK, 1 row affected (0.00 sec) mysql> select * from zsltable;
+----+-------+------+------+
| id | name | age | sex |
+----+-------+------+------+
| 1 | tom | 20 | nan |
| 2 | jerry | 23 | nv |
| 4 | sean | 25 | nv |
+----+-------+------+------+
3 rows in set (0.00 sec) mysql> delete from zsltable; //删除整张表的内容
Query OK, 3 rows affected (0.01 sec) mysql> select * from zsltable;
Empty set (0.00 sec) mysql> desc zsltable;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(10) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| sex | varchar(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
2.5 truncate语句

truncate与delete的区别:

语句类型 特点
delete DELETE删除表内容时仅删除内容,但会保留表结构 DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项 可以通过回滚事务日志恢复数据 非常占用空间
truncate 删除表中所有数据,且无法恢复 表结构、约束和索引等保持不变,新添加的行计数值重置为初始值 执行速度比DELETE快,且使用的系统和事务日志资源少 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放 对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据 不能用于加入了索引视图的表
//语法:TRUNCATE table_name;

mysql> select * from zsltable;
+----+-------+------+------+
| id | name | age | sex |
+----+-------+------+------+
| 1 | tom | 20 | nan |
| 2 | jerry | 23 | nv |
| 3 | zsl | 18 | nan |
| 4 | sean | 25 | nv |
+----+-------+------+------+
4 rows in set (0.00 sec) mysql> truncate zsltable;
Query OK, 0 rows affected (0.01 sec) mysql> select * from zsltable;
Empty set (0.00 sec) mysql> select * from zsltable;
Empty set (0.00 sec) mysql> desc zsltable;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(10) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| sex | varchar(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

3、DCL操作

3.1 创建授权grant

权限类型(priv_type)

权限类型 含义
ALL 所有权限
SELECT 读取内容的权限
INSERT 插入内容的权限
UPDATE 更新内容的权限
DELETE 删除内容的权限

指定要操作的对象db_name.table_name

表示方式 含义
. 所有库的所有表
db_name 指定库的所有表
db_name.table_name 指定库的指定表

WITH GRANT OPTION:被授权的用户可将自己的权限副本转赠给其他用户,说白点就是将自己的权限完全复制给另一个用户。不建议使用。

//语法:GRANT priv_type,... ON [object_type] db_name.table_name TO ‘username'@'host' [IDENTIFIED BY 'password'] [WITH GRANT OPTION];

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lsz |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec) //授权lsz用户在数据库本机上登录访问所有数据库
mysql> GRANT ALL ON *.* TO 'lsz'@'localhost' IDENTIFIED BY 'Passwd123!';
Query OK, 0 rows affected, 1 warning (0.00 sec) //也可表示为:
mysql> GRANT ALL ON *.* TO 'lsz'@'127.0.0.1' IDENTIFIED BY 'Passwd123!';
Query OK, 0 rows affected, 1 warning (0.00 sec) //授权lsz用户在192.168.111.135上远程登录访问hzz数据库
mysql> GRANT ALL ON lsz.* TO 'lsz'@'192.168.111.135' IDENTIFIED BY 'Passwd123!';
Query OK, 0 rows affected, 1 warning (0.00 sec) //授权lsz用户在所有位置上远程登录访问lsz数据库
mysql> GRANT ALL ON lsz.* TO 'lsz'@'%' IDENTIFIED BY 'Passwd123!';
Query OK, 0 rows affected, 1 warning (0.00 sec) //刷新授权表
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.2 查看授权
//查看当前登录用户的授权信息
mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec) //查看指定用户lsz的授权信息
mysql> show grants for 'lsz'@'localhost';
+--------------------------------------------------+
| Grants for lsz@localhost |
+--------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'lsz'@'localhost' |
+--------------------------------------------------+
1 row in set (0.00 sec)
3.3 取消授权REVOKE
//语法:REVOKE priv_type,... ON db_name.table_name FROM 'username'@'host';

mysql> revoke all on *.* from 'lsz'@'192.168.111.135';
Query OK, 0 rows affected (0.00 sec) mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

注意:mysql服务进程启动时会读取mysql库中的所有授权表至内存中:

  • GRANT或REVOKE等执行权限操作会保存于表中,mysql的服务进程会自动重读授权表,并更新至内存中
  • 对于不能够或不能及时重读授权表的命令,可手动让mysql的服务进程重读授权表
mysql> FLUSH PRIVILEGES;

实战案例

1.搭建mysql服务

[root@localhost ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
[root@localhost ~]# rpm -Uvh mysql57-community-release-el7-11.noarch.rpm
[root@localhost ~]# dnf -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel --nogpgcheck
[root@localhost ~]# systemctl enable --now mysqld

2.创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age),表结构如下:

mysql> desc student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
mysql> create database zsl;
Query OK, 1 row affected (0.00 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lsz |
| mysql |
| performance_schema |
| sys |
| zsl |
+--------------------+
6 rows in set (0.00 sec) mysql> create table student(id int(11) primary key auto_increment,name varchar(100) not null,age tinyint(4));
Query OK, 0 rows affected (0.01 sec) mysql> desc student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

3.查看下该新建的表有无内容(用select语句)

mysql> select * from student;
Empty set (0.00 sec)

4.往新建的student表中插入数据(用insert语句),结果应如下所示:

+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
mysql> insert into student (id,name,age) values(1,'tom',20),(2,'jerry',23),(3,'wangqing',25),(4,'seann',28),(5,'zhangsan',26),(6,'zhangsan',20),(7,'lisi',null),(8,'chenshuo',10),(9,'wangwu',3),(10,'qiuyyi',15),(11,'qiuxiaotian',20);
Query OK, 11 rows affected (0.00 sec)
Records: 11 Duplicates: 0 Warnings: 0 mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | seann | 28 |
| 5 | zhangsan | 26 |
| 6 | zhangsan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.01 sec)

5.修改lisi的年龄为50

mysql> update student set age=50 where name='lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from student where name = 'lisi';
+----+------+------+
| id | name | age |
+----+------+------+
| 7 | lisi | 50 |
+----+------+------+
1 row in set (0.00 sec)

6.以age字段降序排序

mysql> select * from student order by age desc;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 7 | lisi | 50 |
| 4 | seann | 28 |
| 5 | zhangsan | 26 |
| 3 | wangqing | 25 |
| 2 | jerry | 23 |
| 1 | tom | 20 |
| 6 | zhangsan | 20 |
| 11 | qiuxiaotian | 20 |
| 10 | qiuyyi | 15 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
+----+-------------+------+
11 rows in set (0.00 sec)

7.查询student表中年龄最小的3位同学跳过前2位

mysql> select * from student order by age  limit 2,3;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 10 | qiuyyi | 15 |
| 1 | tom | 20 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
3 rows in set (0.00 sec)

8.查询student表中年龄最大的4位同学

mysql>  select * from student order by age desc limit 4;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 7 | lisi | 50 |
| 4 | seann | 28 |
| 5 | zhangsan | 26 |
| 3 | wangqing | 25 |
+----+----------+------+
4 rows in set (0.00 sec)

9.查询student表中名字叫zhangshan的记录

mysql> select * from student where name = 'zhangsan' ;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 5 | zhangsan | 26 |
| 6 | zhangsan | 20 |
+----+----------+------+
2 rows in set (0.00 sec)

10.查询student表中名字叫zhangshan且年龄大于20岁的记录

mysql> select * from student where name = 'zhangsan' and age >20;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 5 | zhangsan | 26 |
+----+----------+------+
1 row in set (0.00 sec)

11.查询student表中年龄在23到30之间的记录

mysql> select * from student where age between 23 and 30;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | seann | 28 |
| 5 | zhangsan | 26 |
+----+----------+------+
4 rows in set (0.00 sec)

12.修改wangwu的年龄为100

mysql> update student set age=100 where name = 'wangwu';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from student where name ='wangwu';
+----+--------+------+
| id | name | age |
+----+--------+------+
| 9 | wangwu | 100 |
+----+--------+------+
1 row in set (0.00 sec)

13.删除student中名字叫zhangshan且年龄小于等于20的记录

mysql>  delete from student where name ='zhangsan'and age<=20;
Query OK, 1 row affected (0.00 sec) mysql> select * from student where name = 'zhangsan' and age<=20;
Empty set (0.00 sec)

MySQL客户端工具的使用与MySQL SQL语句的更多相关文章

  1. 4个mysql客户端工具的比较

    mysql是我以前学习和练习所使用的数据,现在在工作中也在使用,之前公司里用oracle,我在做自己的东西的时候觉得用oracle太不方便,于是就找了mysql(当时也考虑过sqlserver,觉得还 ...

  2. MySQL Server类型之MySQL客户端工具的下载、安装和使用

    本博文的主要内容有 .MySQL Server 5.5系列的下载 .MySQL Server 5.5系列的安装 .MySQL Server 5.5系列的使用 .MySQL Server 5.5系列的卸 ...

  3. mysql客户端工具

    MySQL 数据库不仅提供了数据库的服务器端应用程序,同时还提供了大量的客户端工具程序,如 mysql,mysqladmin,mysqldump 等等,都是大家所熟悉的.虽然有些人对这些工具的功能都已 ...

  4. MySQL Server类型之MySQL客户端工具的下载、安装和使用(博主推荐)

    本博文的主要内容有 .MySQL Server 5.5系列的下载 .MySQL Server 5.5系列的安装 .MySQL Server 5.5系列的使用 .MySQL Server 5.5系列的卸 ...

  5. MySQL Workbench类型之MySQL客户端工具的下载、安装和使用

    本博文的主要内容有 .MySQL Workbench的下载 .MySQL Workbench的安装 .MySQL Workbench的使用 个人推荐,比较好的MySQL客户端工具 注意啊! 对于noi ...

  6. MySql数据库3【优化2】sql语句的优化

    1.SELECT语句优化 1).利用LIMIT 1取得唯一行[控制结果集的行数] 有时,当你要查询一张表是,你知道自己只需要看一行.你可能会去的一条十分独特的记录,或者只是刚好检查了任何存在的记录数, ...

  7. MySql update inner join!MySql跨表更新 多表update sql语句?如何将select出来的部分数据update到另一个表里面?

    项目中,评论数,关注数等数据,是实时更新的.+1,-1 这种. 有的时候,可能统计不准确. 需要写一个统计工具,更新校准下. 用Java写SQL和函数,代码很清晰,方便扩展,但是太慢了. 为了简单起见 ...

  8. mysql怎么终止当前正在执行的sql语句

    mysql怎么终止当前正在执行的sql语句 show processlist; kill 要杀的ID kill 7

  9. mysql优化(三)–explain分析sql语句执行效率

    mysql优化(三)–explain分析sql语句执行效率 mushu 发布于 11个月前 (06-04) 分类:Mysql 阅读(651) 评论(0) Explain命令在解决数据库性能上是第一推荐 ...

随机推荐

  1. 现有教学数据库JX_DB,作业

    现有教学数据库JX_DB,数据库有以下三个基本表: 学生表student,它由学号sno.姓名sname.性别sex.出生日期Bdate.所在系dept五个属性构成.其中,学号不能为空,值是唯一的: ...

  2. 好客租房33-事件绑定this指向(总结)

    1推荐使用class的实例方法 //导入react import React from 'react'   import ReactDOM from 'react-dom' //导入组件   // 约 ...

  3. IOI2020 国家集训队作业 泛做

    题号 题目名称 rating 算法 完成情况 CF504E Misha and LCP on Tree CF505E Mr.Kitayuta vs. Bamboos CF506E Mr.Kitayut ...

  4. VUE3 之 render 函数的使用 - 这个系列的教程通俗易懂,适合新手

    1. 概述 老话说的好:不用想的太多.太远,做好当天的事,知道明天要做什么就可以了. 言归正传,今天我们来聊聊 VUE 中 render 函数的使用. 2. render 函数 2.1 一个简单的例子 ...

  5. C# 类继承中的私有字段都去了哪里?

    最近在看 C++ 类继承中的字段内存布局,我就很好奇 C# 中的继承链那些 private 字段都哪里去了? 在内存中是如何布局的,毕竟在子类中是无法访问的. 一:举例说明 为了方便讲述,先上一个例子 ...

  6. django框架11

    内容概要 用户登录之后跳转到用户登录之前想要访问的页面 django操作cookie补充 django操作session django操作session补充 CBV添加装饰器 django中间件 自定 ...

  7. 2.C++标准库函数:getline函数 定界流输入截取函数 -windows编程

    引言:今天工作遇到了一个需要按行读取txt文件数据的需求,查询了一下getline()函数,发现这竟然是一个C++的标准库函数,而且设计的很好,特地做一下记录.getline本质是一个定界流输入截取函 ...

  8. Solon 1.8.3 发布,云原生微服务开发框架

    相对于 Spring Boot 和 Spring Cloud 的项目 启动快 5 - 10 倍 qps 高 2- 3 倍 运行时内存节省 1/3 ~ 1/2 打包可以缩小到 1/2 ~ 1/10(比如 ...

  9. RPA-UiPath视频教程1

    UiPath下载.安装.激活.第一个案例Helloworld!.参数类型.变量的介绍和使用 https://www.bilibili.com/video/av92816532 RPA直播公开课2020 ...

  10. bat-配置环境变量2-给PATH追加环境变量

    使用setx /M path "%path%;%%winrar%%"这种方式修改环境变量存在的问题 对于 path 这种 既有用户级变量和系统级变量的变量 直接使用setx /M ...