Clannaddada

导航

(补档)mysql基数-数据库操作

mysql数据库操作

DDL操作

1.数据库操作

//创建数据库
//语法:CREATE DATABASE [IF NOT EXISTS] 'DB_NAME';

[IF NOT EXISTS] 
用于判断数据库存不存在,若存在就退出,若不存在就创建
平常可以不加,若数据库太多了,可以使用这个判断
//查看当前实例有哪些数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

//创建数据库xiaodada
mysql> create database if not exists xiaodada;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| xiaodada           |
+--------------------+
5 rows in set (0.00 sec)
//删除数据库
//语法:DROP DATABASE [IF EXISTS] 'DB_NAME';
[IF EXISTS]
若不存在就退出,若存在就删除
mysql> drop database xiaodada;
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)

2.表操作

//创建表
//语法:CREATE TABLE table_name (col1 datatype 修饰符,col2 datatype 修饰符) ENGINE='存储引擎类型';
mysql> create database if not exists xiaodada;  //创建数据库xiaodada
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| xiaodada           |
+--------------------+
5 rows in set (0.00 sec)

mysql> use xiaodada;   //进入数据库
Database changed
mysql> create table linux (id int not null,name varchar(10),age tinyint(3));   //创建linux表
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+--------------------+
| Tables_in_xiaodada |
+--------------------+
| linux              |
+--------------------+
1 row in set (0.00 sec)

mysql> desc linux;    //查看表的结构
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
| age   | tinyint(3)  | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
//删除表
//语法:DROP TABLE [ IF EXISTS ] 'table_name';
mysql> drop table linux;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
Empty set (0.00 sec)
mysql数据类型

MySQL中定义数据字段的类型对数据库的优化是非常重要的。MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。

数值类型

MySQL支持所有标准SQL数值数据类型。这些类型包括严格数值数据类型(INTEGER、SMALLINT、DECIMAL和NUMERIC),以及近似数值数据类型(FLOAT、REAL和DOUBLE PRECISION)。

关键字INT是INTEGER的同义词,关键字DEC是DECIMAL的同义词。BIT数据类型保存位字段值,并且支持MyISAM、MEMORY、InnoDB和BDB表。作为SQL标准的扩展,MySQL也支持整数类型TINYINT、MEDIUMINT和BIGINT。

下面的表显示了需要的每个整数类型的存储和范围

类型 大小 范围(有符号) 范围(无符号) 用途
TINYINT 1 byte (-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 446744 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的值 小数值

日期和时间类型

表示时间值的日期和时间类型为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:00/2038 结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07 YYYYMMDD HHMMSS 混合日期和时间值,时间戳

字符串类型

字符串类型指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 极大文本数据

3.用户操作

mysql用户帐号由两部分组成,如'USERNAME'@'HOST'

表示此USERNAME只能从此HOST上远程登录

这里('USERNAME'@'HOST')的HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:

  • IP地址,如:172.16.12.129
  • 通配符
    • %:匹配任意长度的任意字符,常用于设置允许从任何主机登录
    • _:匹配任意单个字符
//数据库用户创建
//语法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
//创建tom用户
mysql> create user 'tom'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> select * from mysql.user\G   //查看详细用户表

mysql> exit
Bye
[root@localhost ~]# mysql -utom -p123456
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 22
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'; 
mysql> drop user 'tom'@'localhost';
Query OK, 0 rows affected (0.02 sec)

4.查看命令show

mysql> show character set;  //查看支持的所有字符集

mysql> show engines;  //查看当前数据库支持的所有存储引擎

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| xiaodada           |
+--------------------+
5 rows in set (0.00 sec)

mysql> show tables from xiaodada;  //不进入某数据库而列出其包含的所有表
+--------------------+
| Tables_in_xiaodada |
+--------------------+
| linux              |
+--------------------+
1 row in set (0.00 sec)
//查看表结构
//语法:DESC [db_name.]table_name;
mysql> desc xiaodada.linux;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
| age   | tinyint(3)  | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
//查看某表的创建命令
//语法:SHOW CREATE TABLE table_name;
mysql> show create table xiaodada.linux;
+-------+--------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------+
| linux | CREATE TABLE `linux` (
  `id` int(11) NOT NULL,
  `name` varchar(10) DEFAULT NULL,
  `age` tinyint(3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------+
1 row in set (0.00 sec)
//查看某表的状态
//语法:SHOW TABLE STATUS LIKE 'table_name'\G
mysql> use xiaodada;
Database changed
mysql> show table status like 'linux'\G
*************************** 1. row ***************************
           Name: linux
         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-25 16:18:01
    Update_time: NULL
     Check_time: NULL
      Collation: latin1_swedish_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)

5.获取帮助

//获取命令使用帮助
//语法:HELP keyword;
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
 ......
 ......

DML操作

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

1.INSERT增

//DML操作之增操作insert
//语法:INSERT [INTO] table_name [(column_name,...)] {VALUES | VALUE} (value1,...),(...),...
mysql> insert into dada (id,name,age) value (1,'dom',20);
Query OK, 1 row affected (0.01 sec)

mysql> insert into dada (id,name,age) values (2,'jerry',23),(3,'tom',25),(4,'sean',28),(5,'zhangshan',26),(6,'zhangshan',20),(7,'lisi',null);
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

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];
//查dada这个表的所有字段
mysql> select * from dada;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  1 | dom       |   20 |
|  2 | jerry     |   23 |
|  3 | tom       |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
|  6 | zhangshan |   20 |
|  7 | lisi      | NULL |
+----+-----------+------+
7 rows in set (0.00 sec)

//查dada这张表的name字段
mysql> select name from dada;
+-----------+
| name      |
+-----------+
| dom       |
| jerry     |
| tom       |
| sean      |
| zhangshan |
| zhangshan |
| lisi      |
+-----------+
7 rows in set (0.00 sec)

//以age字段升序排序查dada这张表
mysql> select * from dada order by age;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  7 | lisi      | NULL |
|  1 | dom       |   20 |
|  6 | zhangshan |   20 |
|  2 | jerry     |   23 |
|  3 | tom       |   25 |
|  5 | zhangshan |   26 |
|  4 | sean      |   28 |
+----+-----------+------+
7 rows in set (0.00 sec)

//以age字段降序排序查dada这张表
mysql> select * from dada order by age desc;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
|  3 | tom       |   25 |
|  2 | jerry     |   23 |
|  1 | dom       |   20 |
|  6 | zhangshan |   20 |
|  7 | lisi      | NULL |
+----+-----------+------+
7 rows in set (0.00 sec)

//以age这张表升序排序并之取前2个结果查dada这张表
mysql> select * from dada order by age limit 2;
+----+------+------+
| id | name | age  |
+----+------+------+
|  7 | lisi | NULL |
|  1 | dom  |   20 |
+----+------+------+
2 rows in set (0.00 sec)

//以age这张表降序排序并之取前2个结果查dada这张表
mysql> select * from dada order by age desc limit 2;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
+----+-----------+------+
2 rows in set (0.00 sec)

//以age这张表升序排序并略过第一个结果之取后面2个结果查dada这张表
mysql> select * from dada order by age limit 1,2;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  1 | dom       |   20 |
|  6 | zhangshan |   20 |
+----+-----------+------+
2 rows in set (0.00 sec)

//判断age字段大于等于25查dada这张表
mysql> select * from dada where age >= 25;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  3 | tom       |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
+----+-----------+------+
3 rows in set (0.00 sec)

//判断age字段大于等于25且name字段为zhangshan查dada这张表
mysql> select * from dada where age >= 25 and name = 'zhangshan';
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  5 | zhangshan |   26 |
+----+-----------+------+
1 row in set (0.00 sec)

//判断age字段不是null查dada这张表
mysql> select * from dada where age is not null;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  1 | dom       |   20 |
|  2 | jerry     |   23 |
|  3 | tom       |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
|  6 | zhangshan |   20 |
+----+-----------+------+
6 rows in set (0.00 sec)

//判断age字段是null查dada这张表
mysql> select * from dada where age is null;
+----+------+------+
| id | name | age  |
+----+------+------+
|  7 | lisi | NULL |
+----+------+------+
1 row in set (0.00 sec)

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 dada;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  1 | dom       |   20 |
|  2 | jerry     |   23 |
|  3 | tom       |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
|  6 | zhangshan |   20 |
|  7 | lisi      | NULL |
+----+-----------+------+
7 rows in set (0.00 sec)

//将name为lisi字段的age改为30
mysql> update dada set age = 30 where name = 'lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from dada where name = 'lisi';
+----+------+------+
| id | name | age  |
+----+------+------+
|  7 | lisi |   30 |
+----+------+------+
1 row in set (0.00 sec)

4.delete语句

//DML操作之删操作delete
//语法:DELETE FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
mysql> select * from dada;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  1 | dom       |   20 |
|  2 | jerry     |   23 |
|  3 | tom       |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
|  6 | zhangshan |   20 |
|  7 | lisi      |   30 |
+----+-----------+------+
7 rows in set (0.00 sec)

//删除age字段小于25
mysql> delete from dada where age < 25;
Query OK, 3 rows affected (0.00 sec)

mysql> select * from dada;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  3 | tom       |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
|  7 | lisi      |   30 |
+----+-----------+------+
4 rows in set (0.00 sec)
mysql> delete from dada;
Query OK, 4 rows affected (0.00 sec)

//删除整张表内容
mysql> select * from dada;
Empty set (0.00 sec)

mysql> desc dada;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   |     | NULL    |       |
| name  | varchar(100) | NO   |     | NULL    |       |
| age   | tinyint(4)   | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> delete from dada;
Query OK, 4 rows affected (0.00 sec)

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

mysql> desc dada;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   |     | NULL    |       |
| name  | varchar(100) | NO   |     | NULL    |       |
| age   | tinyint(4)   | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

5.truncate语句

语句类型 特点
delete DELETE删除表内容时仅删除内容,但会保留表结构
DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项
可以通过回滚事务日志恢复数据
非常占用空间
truncate 删除表中所有数据,且无法恢复
表结构、约束和索引等保持不变,新添加的行计数值重置为初始值
执行速度比DELETE快,且使用的系统和事务日志资源少
通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放
对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据
不能用于加入了索引视图的表
mysql> select * from dada;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  1 | dom       |   20 |
|  2 | jerry     |   23 |
|  3 | tom       |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
|  6 | zhangshan |   20 |
|  7 | lisi      | NULL |
+----+-----------+------+
7 rows in set (0.00 sec)

mysql> truncate dada;
Query OK, 0 rows affected (0.01 sec)

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

mysql> desc dada;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   |     | NULL    |       |
| name  | varchar(100) | NO   |     | NULL    |       |
| age   | tinyint(4)   | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

DCL操作

创建授权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 |
| daxinyu            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

//授权tom用户在数据库本机上登录访问所有数据库
mysql> grant all on *.* to 'tom'@'localhost' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

//也可表示为:
mysql> grant all on *.* to 'tom'@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

//授权tom用户在192.168.118.137上远程登录访问daxinyu数据库
mysql> grant all on daxinyu.* to 'tom'@'192.168.118.137' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

//授权tom用户在所有位置上远程登录访问daxinyu数据库
mysql> grant all on daxinyu.* to 'tom'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

授权后利用student(图形化数据库)进行连接

image

查看授权

//查看当前登录用户的授权信息
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)

//查看指定用户tom的授权信息
mysql> show grants for tom;
+--------------------------------------------------+
| Grants for tom@%                                 |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'%'                  |
| GRANT ALL PRIVILEGES ON `daxinyu`.* TO 'tom'@'%' |
+--------------------------------------------------+
2 rows in set (0.01 sec)

mysql> show grants for 'tom'@'localhost';
+--------------------------------------------------+
| Grants for tom@localhost                         |
+--------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'tom'@'localhost' |
+--------------------------------------------------+
1 row in set (0.00 sec)

mysql> show grants for 'tom'@'127.0.0.1';
+--------------------------------------------------+
| Grants for tom@127.0.0.1                         |
+--------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'tom'@'127.0.0.1' |
+--------------------------------------------------+
1 row in set (0.00 sec)

取消授权

//语法:REVOKE priv_type,... ON db_name.table_name FROM 'username'@'host';
mysql> show grants for 'tom'@'192.168.118.137';
+----------------------------------------------------------------+
| Grants for tom@192.168.118.137                                 |
+----------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'192.168.118.137'                  |
| GRANT ALL PRIVILEGES ON `dada`.* TO 'tom'@'192.168.118.137'    |
| GRANT ALL PRIVILEGES ON `daxinyu`.* TO 'tom'@'192.168.118.137' |
+----------------------------------------------------------------+
3 rows in set (0.00 sec)

mysql> revoke all on dada.* from 'tom'@'192.168.118.137';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for 'tom'@'192.168.118.137';
+----------------------------------------------------------------+
| Grants for tom@192.168.118.137                                 |
+----------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'192.168.118.137'                  |
| GRANT ALL PRIVILEGES ON `daxinyu`.* TO 'tom'@'192.168.118.137' |
+----------------------------------------------------------------+
2 rows in set (0.00 sec)

实战案例

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 ~]# yum  module disable mysql
[root@localhost ~]# yum -y install mysql-community-server mysql-community-client  mysql-community-common mysql-community-devel  --nogpgcheck
[root@localhost ~]# rpm -e mysql57-community-release
[root@localhost ~]# systemctl enable --now mysqld
[root@localhost ~]# grep "password" /var/log/mysqld.log
2022-07-26T04:20:43.787728Z 1 [Note] A temporary password is generated for root@localhost: QthCgNq#g98;
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.39

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> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

mysql> set password = password('123456');
[root@localhost ~]# mysql -uroot -p123456
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 10
Server version: 5.7.39 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> 

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 if not exists daxinyu;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| daxinyu            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use daxinyu;
Database changed

mysql> create table student (id int(11) not null key auto_increment,name varchar(100) not null,age tinyint(4));
Query OK, 0 rows affected (0.00 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.01 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) value (1,'tom',20);
Query OK, 1 row affected (0.00 sec)

mysql> insert into student (id,name,age) values (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);
Query OK, 10 rows affected (0.00 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+-------------+------+
| 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 |
+----+-------------+------+
11 rows in set (0.00 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 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  3 | wangqing    |   25 |
|  2 | jerry       |   23 |
|  1 | tom         |   20 |
|  6 | zhangshan   |   20 |
| 11 | qiuxiaotian |   20 |
| 10 | qiuyi       |   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 | qiuyi       |   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 | sean      |   28 |
|  5 | zhangshan |   26 |
|  3 | wangqing  |   25 |
+----+-----------+------+
4 rows in set (0.00 sec)

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

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

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

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

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

mysql> select * from student where age > 23 and age < 30;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  3 | wangqing  |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
+----+-----------+------+
3 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 = 'zhangshan';
Query OK, 2 rows affected (0.00 sec)

mysql> delete from student where age <= 20;
Query OK, 4 rows affected (0.00 sec)

mysql> select * from student;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  2 | jerry    |   23 |
|  3 | wangqing |   25 |
|  4 | sean     |   28 |
|  7 | lisi     |   50 |
|  9 | wangwu   |  100 |
+----+----------+------+
5 rows in set (0.00 sec)

posted on 2022-07-26 14:51  linux-ada  阅读(64)  评论(0编辑  收藏  举报