Linux系统——Mysql数据库操作

目录

一、数据库基本操作

1.查看数据库结构

1.1查看数据库信息——Show databases

1.2查看数据库中的表信息——Show tables

Show tables in 数据库名

use 数据库名   show tables

1.3显示数据表的结构(字段)——Describe(Desc)

1.4常用的数据类型

1.5扩展

二、SQL语句

1.SQL语句概述

2.SQL分类

3.DDL语句——创建数据库和表(数据定义语言)

3.1创建数据库——Create databases

3.2创建数据表——Create table

3.3删除数据库——Drop database

3.4删除数据表——Drop table

4.DML——管理表中的数据(数据操纵语言)

4.1插入数据——Insert

4.2修改、更新数据表中的数据记录——Update

4.3删除数据——Delete

5.DQL——管理表中的数据(数据查询语言)

5.1Select * ——查询所有

5.2Where——特定条件查找

5.3limit——按行数查询

5.4 \G——以竖向方向显示

6.DCL——数据库用户授权(数据控制语言)

6.1rename——更改数据表名称

6.2add——扩展表结构字段

6.3Change——更改数据字段内容

6.4Drop——删除指定数据字段内容

三、高级操作

1.清空表

1.1Delete from tablename

1.2Truncate table tablename

1.3Drop form tablename

1.4 对比

2.临时表

3.克隆表

3.1Like方法

3.2Show create table方法

四、数据库用户管理

1.新建用户

2.重命名

3.删除用户

4.修改当前密码

5.修改其他用户密码

6.忘记密码

7.修改密码

7.1Update修改密码

7.2直接修改

五、数据库用户授权

1.给指定用户select权限 

2.使用Navicat图形化工具远程连接

3.查看用户权限

4.撤销用户权限

六、总结

1.查看数据库、数据表和表结构的操作

2.创建库和表的操作及删除库和表的操作

3.数据表的增、删、改、查等操作

 4.数据库表的清空表、临时表和克隆表操作

4.1清空表

4.2临时表

4.3克隆表

5.数据库的用户授权相关操作

5.1数据库用户管理

5.2数据库授权


一、数据库基本操作

1.查看数据库结构

1.1查看数据库信息——Show databases

1.2查看数据库中的表信息——Show tables

Show tables in 数据库名

use 数据库名   show tables

1.3显示数据表的结构(字段)——Describe(Desc)

详解

字段含义
Field字段名称
Type数据类型
Null是否为空
Key主键
Default默认值
Extra扩展属性

key主键是唯一的,但是主键可以由多个字段构成 

1.4常用的数据类型

数据类型含义
int整形,用于定义整数类型的数据
float单精度浮点4字节32位,准确表示到小数点后六位
double双精度浮点8字节64位
char固定长度的字符类型
varchar可变长度的字符类型
text文本
image图片
decimal指定长度数组,decimal(5,2)表示5个有效长度数字,小数点后有两位数字

char如果存入数据的实际长度要比指定长度小,会补空格至指定长度,如果存入的数据的实际长度大于指定长度,低版本会被截取,高版本会报错;

1.5扩展

Mysql数据库的数据文件存放在/usr/local/mysql/data目录下,每个数据库对应一个子目录,用于存储数据表文件。如果数据表对应为三个文件,扩展名分别为".frm"、".MYD"、".MYI" 

MYD”文件是MyISAM存储引擎专用,存放MyISAM表的数据。每一个MyISAM表都会有一个“.MYD”文件与之对应,同样存放于所属数据库的文件夹下,和“.frm”文件在一起。

“.MYI”文件也是专属于 MyISAM 存储引擎的,主要存放 MyISAM 表的索引相关信息。对于 MyISAM 存储来说,可以被 cache 的内容主要就是来源于“.MYI”文件中。每一个MyISAM 表对应一个“.MYI”文件,存放于位置和“.frm”以及“.MYD”一样。

MyISAM 存储引擎的表在数据库中,每一个表都被存放为三个以表名命名的物理文件
(frm,myd,myi)。 每个表都有且仅有这样三个文件做为 MyISAM 存储类型的表的存储,也就是说不管这个表有多少个索引,都是存放在同一个.MYI 文件中。

另外还有“.ibd”和 ibdata 文件,这两种文件都是用来存放 Innodb 数据的,之所以有两种文件来存放 Innodb 的数据(包括索引),是因为Innodb的数据存储方式能够通过配置来决定是使用共享表空间存放存储数据,还是独享表空间存放存储数据。独享表空间存储 方式使用“.ibd”文件来存放数据,且每个表一个“.ibd”文件,文件存放在和 MyISAM 数据相同的位置。如果选用共享存储表空间来存放数据,则会使用 ibdata  文件来存放,所有表共同使用一个(或者多个,可自行配置)ibdata 文件。

二、SQL语句

1.SQL语句概述

SQL(Structured Query Language)结构化查询语言,是关系型数据库的标准语言,用于维护管理数据库,其中包括数据查询数据更新访问控制对象管理等功能

2.SQL分类

  • DDL:数据定义语言
  • DML:数据操纵语言
  • DQL:数据查询语言
  • DCL:数据控制语言

3.DDL语句——创建数据库和表(数据定义语言)

DDL语句可用于创建数据库对象,如库、表、索引等

3.1创建数据库——Create databases

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

mysql> create database class;
Query OK, 1 row affected (0.00 sec)

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

3.2创建数据表——Create table

CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型[,...][,PRIMARY KEY (主键名)]);
#主键一般选择能代表唯一性的字段不允许取空值(NULL),一个表只能有一个主键。

mysql> create table class (id int not null,name char(15) not null,grate decimall(4,2),passwd char(45) default'',primary key (id));
#创建一个名为class的数据表 id取整型不为空;name字段字节数为15字节不为空;grate字段整数位最大为4位,小数位为2位;密码字段字节数为45字节;default字段默认值为空;primary key主键定义id为唯一的,也就是说id不可以重复
Query OK, 0 rows affected (0.07 sec)

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

mysql> create table class2 (id int not null,name char(15) not null,grate decimaal(4,2),passwd char(45) default'',primary key (id));
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;                                                           
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
+-----------------+
2 rows in set (0.00 sec)
mysql> describe class;
#查询所建数据表定义的字段类型
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| id     | int(11)      | NO   | PRI | NULL    |       |
| name   | char(15)     | NO   |     | NULL    |       |
| grate  | decimal(4,2) | YES  |     | NULL    |       |
| passwd | char(45)     | YES  |     |         |       |
+--------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> desc class2;
#可以简写为desc
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| id     | int(11)      | NO   | PRI | NULL    |       |
| name   | char(15)     | NO   |     | NULL    |       |
| grate  | decimal(4,2) | YES  |     | NULL    |       |
| passwd | char(45)     | YES  |     |         |       |
+--------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

3.3删除数据库——Drop database

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

mysql> drop database class;
#删除指定数据库
Query OK, 1 row affected (0.00 sec)

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

3.4删除数据表——Drop table

DROP TABLE [数据库名.] 表名

mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
+-----------------+
2 rows in set (0.00 sec)

mysql> drop table class;
#删除指定数据表
Query OK, 0 rows affected (0.00 sec)

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

4.DML——管理表中的数据(数据操纵语言)

DML语句用于对表中的数据进行管理,用来插入、删除和修改数据库中的数据

  • Insert:插入新数据
  • Update:更新原有数据
  • Delete:删除不需要的数据

4.1插入数据——Insert

INSERT INTO 表名(字段1,字段2[,...]) VALUES (字段1的值,字段2的值,...)

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

mysql> desc class;
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| id     | int(11)      | NO   | PRI | NULL    |       |
| name   | char(15)     | NO   |     | NULL    |       |
| grate  | decimal(4,2) | YES  |     | NULL    |       |
| passwd | char(45)     | YES  |     |         |       |
+--------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> insert into class (id,name,grate,passwd) values(1,'cxk',88,PASSWORD('1233123'));
Query OK, 1 row affected, 1 warning (0.00 sec)
#插入数据到class数据表中,对应id为1,name为cxk,grate为88,passwd使用PASSWORD密文密码设置为123123  若不使用PASSWORD(),查询时以明文显示
mysql> select * from class;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
+----+------+-------+-------------------------------------------+
1 row in set (0.00 sec)
#此时看到的cxk客户的相关信息   passwor定义后的密码是加密后的

字段就是属性,值就是信息

mysql> insert into class values(2,'wyb',96,123321);
Query OK, 1 row affected (0.00 sec)

mysql> select * from class;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
|  2 | wyb  | 96.00 | 123321                                    |
+----+------+-------+-------------------------------------------+
2 rows in set (0.00 sec)
#此时看到的新建用户wyb的相关信息   密码显示为明文的

4.2修改、更新数据表中的数据记录——Update

mysql> select * from class;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
|  2 | wyb  | 96.00 | 123321                                    |
+----+------+-------+-------------------------------------------+
2 rows in set (0.00 sec)

mysql> update class set grate ='92';
#如若不指定条件  那么修改更新的数据信息就是数据表中所有信息
#将数据表class中所有grate字段都修改为92
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from class;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 92.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
|  2 | wyb  | 92.00 | 123321                                    |
+----+------+-------+-------------------------------------------+
2 rows in set (0.00 sec)
mysql> update class set grate ='96' where id=2;
#where代表条件判断  更新class数据表中的grate字段 id为2的用户信息的grate字段修改为96
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from class;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 92.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
|  2 | wyb  | 96.00 | 123321                                    |
+----+------+-------+-------------------------------------------+
2 rows in set (0.00 sec)

4.3删除数据——Delete

mysql> select * from class;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 92.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
|  2 | wyb  | 96.00 | 123321                                    |
+----+------+-------+-------------------------------------------+
2 rows in set (0.00 sec)

mysql> delete from class where name='cxk';
#删除特定条件的数据信息  删除class数据表中 条件为name=cxk的用户信息数据
Query OK, 1 row affected (0.00 sec)

mysql> select * from class;
+----+------+-------+--------+
| id | name | grate | passwd |
+----+------+-------+--------+
|  2 | wyb  | 96.00 | 123321 |
+----+------+-------+--------+
1 row in set (0.00 sec)
mysql> insert into class values(1,'cxk',88,PASSWORD('123123'));
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from class;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
|  2 | wyb  | 96.00 | 123321                                    |
+----+------+-------+-------------------------------------------+
2 rows in set (0.00 sec)

mysql> delete from class;
#删除class数据表
Query OK, 2 rows affected (0.00 sec)

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

mysql> insert into class values(1,'cxk',88,PASSWORD('123123'));
#使用delete命令行 删除该数据表还是可以重新写入新的数据
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from class;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
+----+------+-------+-------------------------------------------+
1 row in set (0.00 sec)

5.DQL——管理表中的数据(数据查询语言)

DQL是数据查询语句,只有Select命令行,用于从数据表中查找符合条件的数据记录(查询时可以不指定条件)

5.1Select * ——查询所有

Select 字段1,字段2,......from 表名

mysql> select * from class;
#查询class数据表中所有数据
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
+----+------+-------+-------------------------------------------+
1 row in set (0.00 sec)

mysql> select id,name from class;
#查询class数据表中id和name字段数据
+----+------+
| id | name |
+----+------+
|  1 | cxk  |
+----+------+
1 row in set (0.00 sec)

mysql> select id,name,grate from class;
#查询class数据表中id、name和grate字段数据
+----+------+-------+
| id | name | grate |
+----+------+-------+
|  1 | cxk  | 88.00 |
+----+------+-------+
1 row in set (0.00 sec)

5.2Where——特定条件查找

Select 字段1,字段2......from 表名 Where 条件表达式

mysql> insert into class values(2,'wyb',96,PASSWORD('123321'))
    -> ;
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from class;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
|  2 | wyb  | 96.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
+----+------+-------+-------------------------------------------+
2 rows in set (0.00 sec)

mysql> select name,id,grate from class where id=2;
+------+----+-------+
| name | id | grate |
+------+----+-------+
| wyb  |  2 | 96.00 |
+------+----+-------+
1 row in set (0.00 sec)

mysql> select name,id,grate from class where name='wyb';
+------+----+-------+
| name | id | grate |
+------+----+-------+
| wyb  |  2 | 96.00 |
+------+----+-------+
1 row in set (0.00 sec)

5.3limit——按行数查询

按行数查询

mysql> insert into class values(3,'xzq',95,PASSWORD('123321'));
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> insert into class values(4,'lyf',95,PASSWORD('123321'));
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from class;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
|  2 | wyb  | 96.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
|  3 | xzq  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
|  4 | lyf  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
+----+------+-------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> select id,name from class limit 2;
+----+------+
| id | name |
+----+------+
|  1 | cxk  |
|  2 | wyb  |
+----+------+
2 rows in set (0.00 sec)
mysql> select * from class;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
|  2 | wyb  | 96.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
|  3 | xzq  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
|  4 | lyf  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
+----+------+-------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> select id,name from class limit 2,3;
#显示第2行的后3行,也就是从第三行开始 显示三行
+----+------+
| id | name |
+----+------+
|  3 | xzq  |
|  4 | lyf  |
+----+------+
2 rows in set (0.00 sec)

5.4 \G——以竖向方向显示

mysql> select * from class;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
|  2 | wyb  | 96.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
|  3 | xzq  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
|  4 | lyf  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
+----+------+-------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> select * from class\G;
*************************** 1. row ***************************
    id: 1
  name: cxk
 grate: 88.00
passwd: *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1
*************************** 2. row ***************************
    id: 2
  name: wyb
 grate: 96.00
passwd: *437F1809645E0A92DAB553503D2FE21DB91270FD
*************************** 3. row ***************************
    id: 3
  name: xzq
 grate: 95.00
passwd: *437F1809645E0A92DAB553503D2FE21DB91270FD
*************************** 4. row ***************************
    id: 4
  name: lyf
 grate: 95.00
passwd: *437F1809645E0A92DAB553503D2FE21DB91270FD
4 rows in set (0.00 sec)

ERROR: 
No query specified

6.DCL——数据库用户授权(数据控制语言)

DCL语句设置用户权限(用户不存在时,则新建用户)

Grant 权限列表 ON 数据库名.表名 to 用户名@来源地址 [ Identified by '密码' ]

Alter 修改字段、数据表

6.1rename——更改数据表名称

mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
+-----------------+
2 rows in set (0.00 sec)

mysql> alter table class2 rename class3;
#修改class数据表的名字变更为class3
Query OK, 0 rows affected (0.03 sec)

mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class3          |
+-----------------+
2 rows in set (0.00 sec)

6.2add——扩展表结构字段

mysql> alter table class3 add Phone varchar(11) default '13100113399';
#修改数据表内容 新增Phone的数据列 字段字节长度为11位  默认值为13100113399
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select *  from class3;
+----+------+-------+-------------------------------------------+-------------+
| id | name | grate | passwd                                    | Phone       |
+----+------+-------+-------------------------------------------+-------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 | 13100113399 |
|  2 | wyb  | 96.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD | 13100113399 |
|  3 | xzq  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD | 13100113399 |
|  4 | lyf  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD | 13100113399 |
+----+------+-------+-------------------------------------------+-------------+
4 rows in set (0.00 sec)

6.3Change——更改数据字段内容

mysql> alter table class3 change id num varchar(15) unique key;
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from class3;
+-----+------+-------+-------------------------------------------+-------------+
| num | name | grate | passwd                                    | Phone       |
+-----+------+-------+-------------------------------------------+-------------+
| 1   | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 | 13100113399 |
| 2   | wyb  | 96.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD | 13100113399 |
| 3   | xzq  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD | 13100113399 |
| 4   | lyf  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD | 13100113399 |
+-----+------+-------+-------------------------------------------+-------------+
4 rows in set (0.00 sec)

6.4Drop——删除指定数据字段内容

mysql> alter table class3 drop Phone;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from class3;
+-----+------+-------+-------------------------------------------+
| num | name | grate | passwd                                    |
+-----+------+-------+-------------------------------------------+
| 1   | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
| 2   | wyb  | 96.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
| 3   | xzq  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
| 4   | lyf  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
+-----+------+-------+-------------------------------------------+
4 rows in set (0.00 sec)

三、高级操作

1.清空表

1.1Delete from tablename

DELETE清空表后,返回的结果内有删除的记录条目

DELETE 工作时是一行一行的删除记录数据的;如果表中有自增长字段,使用DELETE FROM 删除
所有记录后,再次新添加的记录会从原来最大的记录ID后面继续自增写入记录。

mysql> select * from class3;
+-----+------+-------+-------------------------------------------+
| num | name | grate | passwd                                    |
+-----+------+-------+-------------------------------------------+
| 1   | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
| 2   | wyb  | 96.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
| 3   | xzq  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
| 4   | lyf  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
+-----+------+-------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> delete from class3;
Query OK, 4 rows affected (0.00 sec)

mysql> select * from class3;
Empty set (0.00 sec)
mysql> insert into class3 values(1,'cxk',79,123456)
    -> ;
Query OK, 1 row affected (0.00 sec)

mysql> select * from class3;
+-----+------+-------+--------+
| num | name | grate | passwd |
+-----+------+-------+--------+
| 1   | cxk  | 79.00 | 123456 |
+-----+------+-------+--------+
1 row in set (0.00 sec)
mysql> desc class3;
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| num    | varchar(15)  | NO   | PRI | NULL    |       |
| name   | char(15)     | NO   |     | NULL    |       |
| grate  | decimal(4,2) | YES  |     | NULL    |       |
| passwd | char(45)     | YES  |     |         |       |
+--------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

1.2Truncate table tablename

TRUNCATE清空表后,没有返回被删除的条目: TRUNCATE 工作时是将表结构按原样重新建立,
因此在速度上TRUNCATE会比DELETE清空表快;使用TRUNCATE TABLE 清空表内数据后,
ID会从1开始重新记录

mysql> truncate table class3;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
| class3          |
+-----------------+
3 rows in set (0.00 sec)

mysql> insert into class3 values(1,'cxk',56,123123) ;
Query OK, 1 row affected (0.00 sec)

mysql> select * from class3;
+-----+------+-------+--------+
| num | name | grate | passwd |
+-----+------+-------+--------+
| 1   | cxk  | 56.00 | 123123 |
+-----+------+-------+--------+
1 row in set (0.00 sec)

1.3Drop form tablename

mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
| class3          |
+-----------------+
3 rows in set (0.00 sec)

mysql> drop table class3;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
+-----------------+
2 rows in set (0.00 sec)

1.4 对比

区别Drop table tablenameTruncate table tablenameDelete table tablename
所属SQL语句属于DDL属于DDL属于DML
是否可回滚不可回滚(无法恢复)不可回滚(无法恢复)可回滚(可恢复)
是否带Where不可带Where不可带Where可带Where
是否删除表内容和表结构表内容和结构删除表内容删除表结构在,表内容要看where执行情况
删除速度删除速度快删除速度快删除速度慢,需要逐行删除
速度比较最快次之最慢
最好使用Delete

当不再需要一张表的时候使用Drop;想保留部分数据的时候使用Delete,并且带上Where语句;保留表而删除所有数据的时候用Truncate 

2.临时表

临时建立的表,用于保存一些临时数据,不会长期存在(下次重新进入数据库即不存在)

如果在退出连接之前,也可以可执行增删改查等操作,比如使用DROP TABLE语句手动直接删除临时表。

mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
+-----------------+
2 rows in set (0.00 sec)

mysql> create temporary table class3(id int(6) zerofill primary key auto_increment,name varchar(15) not null,cardid int(18) not null unique key,hobby varchar(60));
#创建临时表 数据表名为class3 id 整型为6字节 zerofill数值不满6位时,前面位数用0补充(00001)
#auto_increment:表示此字段为自增长字段,即每条记录自动递增1,默认从1开始递增;自增长字段数据不可以重复;自增长字段必须是主键;如添加的记录数据没有指定此字段的值且添加失败也会自动递增一次
#unique key:表示此字段唯一键约束,此字段数据不可以重复:一张表中只能有一个主键,但是一张表中可以有多个唯一键
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
#建立的临时表是看不到的                                                           
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
+-----------------+
2 rows in set (0.00 sec)

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

mysql> insert into class3 values(1,'cxk',12345678,ctrl);
ERROR 1054 (42S22): Unknown column 'ctrl' in 'field list'
mysql> insert into class3 values(1,'cxk',12345678,'ctrl');
Query OK, 1 row affected (0.00 sec)

mysql> select * from class3;
+--------+------+----------+-------+
| id     | name | cardid   | hobby |
+--------+------+----------+-------+
| 000001 | cxk  | 12345678 | ctrl  |
+--------+------+----------+-------+
1 row in set (0.00 sec)
mysql> insert into class3(name,cardid,hobby) values('wyb',87654321,'skateboardiing');
#若不指定id  可以自动增加
Query OK, 1 row affected (0.00 sec)

mysql> select * from class3;
+--------+------+----------+---------------+
| id     | name | cardid   | hobby         |
+--------+------+----------+---------------+
| 000001 | cxk  | 12345678 | ctrl          |
| 000002 | wyb  | 87654321 | skateboarding |
+--------+------+----------+---------------+
2 rows in set (0.00 sec)

此时退出数据库再重新进入

mysql> quit
Bye
[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 12
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| class              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
mysql> use class;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
+-----------------+
2 rows in set (0.00 sec)

mysql> select * from class3;
ERROR 1146 (42S02): Table 'class.class3' doesn't exist
#临时表找不到了

3.克隆表

3.1Like方法

mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
+-----------------+
2 rows in set (0.00 sec)

mysql> create table class3 like class2;
#克隆数据表的结构
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
| class3          |
+-----------------+
3 rows in set (0.00 sec)
mysql> select * from class3;
Empty set (0.00 sec)

mysql> insert into class3 select * from class2;
#克隆数据表class2的数据内容
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from class3;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
|  2 | wyb  | 96.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
|  3 | xzq  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
|  4 | lyf  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
+----+------+-------+-------------------------------------------+
4 rows in set (0.00 sec)

3.2Show create table方法

mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
| class3          |
+-----------------+
3 rows in set (0.00 sec)

mysql> show create table class2\G
*************************** 1. row ***************************
       Table: class2
Create Table: CREATE TABLE "class2" (
  "id" int(11) NOT NULL,
  "name" char(15) NOT NULL,
  "grate" decimal(4,2) DEFAULT NULL,
  "passwd" char(45) DEFAULT '',
  PRIMARY KEY ("id")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> create table class4(select * from class3);
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
| class3          |
| class4          |
+-----------------+
4 rows in set (0.00 sec)

mysql> select * from class4;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
|  2 | wyb  | 96.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
|  3 | xzq  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
|  4 | lyf  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
+----+------+-------+-------------------------------------------+
4 rows in set (0.00 sec)

如果想将数据迁移到其他地方使用Like备份方法;

如果想只备份数据的内容可以使用第二种方法。

四、数据库用户管理

1.新建用户

CREATE USER '用户名'@'来源地址' [IDENTIFIED BY [PASSWORD] '密码'];

  • 来源地址:可以指定为localhost本机,也可以指定%(代表任意地址),或者指定为192.168.241.%(代表192.168.241网段的主机都可以登录)
  • IDENTIFIED BY:不能省略,如果省略,用户密码将为空(不建议使用)
  • 密码:若使用明文密码,直接输入密码即可,键入到数据库中数据库会自动加密;若使用加密密码,需要先使用Select PASSWORD('密码')命令,来获取加密密码,再将获取到的加密密码添加到PASSWORD中
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from user\G;
#查看用户信息  所有创建后的user用户信息均在user表里
*************************** 1. row ***************************
                  Host: localhost
                  User: root
           Select_priv: Y
           Insert_priv: Y
           Update_priv: Y
           Delete_priv: Y
           Create_priv: Y
             Drop_priv: Y
           Reload_priv: Y
         Shutdown_priv: Y
          Process_priv: Y
             File_priv: Y
            Grant_priv: Y
       References_priv: Y
            Index_priv: Y
            Alter_priv: Y
          Show_db_priv: Y
            Super_priv: Y
 Create_tmp_table_priv: Y
      Lock_tables_priv: Y
          Execute_priv: Y
       Repl_slave_priv: Y
      Repl_client_priv: Y
      Create_view_priv: Y
        Show_view_priv: Y
   Create_routine_priv: Y
    Alter_routine_priv: Y
      Create_user_priv: Y
            Event_priv: Y
          Trigger_priv: Y
Create_tablespace_priv: Y
              ssl_type: 
            ssl_cipher: 
           x509_issuer: 
          x509_subject: 
         max_questions: 0
           max_updates: 0
       max_connections: 0
  max_user_connections: 0
                plugin: mysql_native_password
 authentication_string: *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9
      password_expired: N
 password_last_changed: 2024-03-19 13:37:01
     password_lifetime: NULL
        account_locked: N
*************************** 2. row ***************************
                  Host: localhost
                  User: mysql.sys
           Select_priv: N
           Insert_priv: N
           Update_priv: N
           Delete_priv: N
           Create_priv: N
             Drop_priv: N
           Reload_priv: N
         Shutdown_priv: N
          Process_priv: N
             File_priv: N
            Grant_priv: N
       References_priv: N
            Index_priv: N
            Alter_priv: N
          Show_db_priv: N
            Super_priv: N
 Create_tmp_table_priv: N
      Lock_tables_priv: N
          Execute_priv: N
       Repl_slave_priv: N
      Repl_client_priv: N
      Create_view_priv: N
        Show_view_priv: N
   Create_routine_priv: N
    Alter_routine_priv: N
      Create_user_priv: N
            Event_priv: N
          Trigger_priv: N
Create_tablespace_priv: N
              ssl_type: 
            ssl_cipher: 
           x509_issuer: 
          x509_subject: 
         max_questions: 0
           max_updates: 0
       max_connections: 0
  max_user_connections: 0
                plugin: mysql_native_password
 authentication_string: *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE
      password_expired: N
 password_last_changed: 2024-03-19 13:17:45
     password_lifetime: NULL
        account_locked: Y
*************************** 3. row ***************************
                  Host: localhost
                  User: cxk
           Select_priv: N
           Insert_priv: N
           Update_priv: N
           Delete_priv: N
           Create_priv: N
             Drop_priv: N
           Reload_priv: N
         Shutdown_priv: N
          Process_priv: N
             File_priv: N
            Grant_priv: N
       References_priv: N
            Index_priv: N
            Alter_priv: N
          Show_db_priv: N
            Super_priv: N
 Create_tmp_table_priv: N
      Lock_tables_priv: N
          Execute_priv: N
       Repl_slave_priv: N
      Repl_client_priv: N
      Create_view_priv: N
        Show_view_priv: N
   Create_routine_priv: N
    Alter_routine_priv: N
      Create_user_priv: N
            Event_priv: N
          Trigger_priv: N
Create_tablespace_priv: N
              ssl_type: 
            ssl_cipher: 
           x509_issuer: 
          x509_subject: 
         max_questions: 0
           max_updates: 0
       max_connections: 0
  max_user_connections: 0
                plugin: mysql_native_password
 authentication_string: *6691484EA6B50DDDE1926A220DA01FA9E575C18A
      password_expired: N
 password_last_changed: 2024-03-20 16:46:58
     password_lifetime: NULL
        account_locked: N
3 rows in set (0.00 sec)

ERROR: 
No query specified




mysql> create user 'cxk'@'localhost' identified by 'abc123';
#新建一个user用户名为cxk 指定在本机可以登录 密码使用加密密码 
Query OK, 0 rows affected (0.00 sec)
mysql> select authentication_string from user where user='cxk';
#查看user数据表 下cxk的加密密码  authentication_string代表加密密码的字段
+-------------------------------------------+
| authentication_string                     |
+-------------------------------------------+
| *6691484EA6B50DDDE1926A220DA01FA9E575C18A |
+-------------------------------------------+
1 row in set (0.00 sec)

2.重命名

mysql> rename user 'cxk'@'localhost' to 'wyb'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> select authentication_string from user where user='cxk';
Empty set (0.00 sec)

mysql> select authentication_string from user where user='wyb';
+-------------------------------------------+
| authentication_string                     |
+-------------------------------------------+
| *6691484EA6B50DDDE1926A220DA01FA9E575C18A |
+-------------------------------------------+
1 row in set (0.00 sec)

3.删除用户

mysql> drop user 'wyb'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> select authentication_string from user where user='wyb';
Empty set (0.00 sec)

4.修改当前密码

mysql> set password = password('123123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
[root@localhost ~]#mysql -uroot -p123123
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 17
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> 

5.修改其他用户密码

mysql> set password for 'cxk'@'localhost' = password('123123');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye
[root@localhost ~]#mysql -ucxk -p123123
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 18
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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>

6.忘记密码

[root@localhost ~]#vim /etc/my.cnf
[root@localhost ~]#sed -n '24p' /etc/my.cnf
skip-grant-tables
[root@localhost ~]#systemctl restart mysqld
[root@localhost ~]#mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> 

7.修改密码

7.1Update修改密码

mysql> update mysql.user set authentication_string = password('abc123') where user='root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

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

mysql> quit
Bye
[root@localhost ~]#vim /etc/my.cnf
[root@localhost ~]#sed -n '24p' /etc/my.cnf
#skip-grant-tables
[root@localhost ~]#mysql -uroot -pabc123
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 7
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> quit
Bye

7.2直接修改

[root@localhost ~]#vim /etc/my.cnf
[root@localhost ~]#sed -n '24p' /etc/my.cnf
skip-grant-tables
[root@localhost ~]#systemctl restart mysqld
[root@localhost ~]#mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> set password for root@localhost=password('123123');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye
[root@localhost ~]#vim /etc/my.cnf
[root@localhost ~]#sed -n '24p' /etc/my.cnf
#skip-grant-tables
[root@localhost ~]#systemctl restart mysqld
[root@localhost ~]#mysql -uroot -p123123
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 3
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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>

五、数据库用户授权

Grant提权

GRANT 权限列表 ON 数据库名.表名 TO '用户名'@'来源地址' [IDENTIFIED BY '密码'];

  • 权限列表:用于列出授权使用的各种数据库操作,以逗号进行分隔,如“select, insert, update”。使用"all"表示所有权限,可授权执行任何操作。
  • 数据库名.表名:用于指定授权操作的数据库和表的名称,其中可以使用通配符"*"。例如,使用“class.*"表示授权操作的对象为class数据库中的所有表。
权限含义
insert插入数据
select查询表数据
update更新表数据
create创建库/表
drop删除库/表
refernces关联数据表
index建立索引
alter更改表属性
creat temp orary tableslock tables锁表
execute运行可返回多个结果的给定的 SQL 语句
create view创建视图
show view显示视图
create routine创建存储过程
alter routine修改存储过程
event事件
trigger on创建触发器

1.给指定用户select权限 

[root@localhost ~]#mysql -uroot -p123123
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.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| class              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> grant select ON class.* to 'cxk'@'localhost' identified by '123123';
#授权select权限class数据库 给 cxk@localhost用户 
Query OK, 0 rows affected, 2 warnings (0.00 sec)

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

mysql> quit
Bye
[root@localhost ~]#mysql -ucxk -p123123
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.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> select * from class;
ERROR 1046 (3D000): No database selected
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| class              |
+--------------------+
2 rows in set (0.01 sec)

mysql> use class;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| class           |
| class2          |
| class3          |
| class4          |
+-----------------+
4 rows in set (0.00 sec)

mysql> select * from class;
+----+------+-------+-------------------------------------------+
| id | name | grate | passwd                                    |
+----+------+-------+-------------------------------------------+
|  1 | cxk  | 88.00 | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
|  2 | wyb  | 96.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
|  3 | xzq  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
|  4 | lyf  | 95.00 | *437F1809645E0A92DAB553503D2FE21DB91270FD |
+----+------+-------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> drop table class;
ERROR 1142 (42000): DROP command denied to user 'cxk'@'localhost' for table 'class'
mysql> drop table from class;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from class' at line 1
mysql> delete from class;
ERROR 1142 (42000): DELETE command denied to user 'cxk'@'localhost' for table 'class'
mysql> quit
Bye

2.使用Navicat图形化工具远程连接

[root@localhost ~]#mysql -uroot -p123123
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 6
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> grant all privileges on class.* to 'cxk'@'192.168.241.%' identified by '123123';
#授权class数据库的所有权限  给 cxk@192.168.241网段的主机 使用密码123123可以对class数据库做相关操作
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

mysql> quit
Bye

3.查看用户权限

[root@localhost ~]#mysql -uroot -p123123
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 9
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> show grants for 'cxk'@'192.168.241.%';
+------------------------------------------------------------+
| Grants for cxk@192.168.241.%                               |
+------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'cxk'@'192.168.241.%'                |
| GRANT ALL PRIVILEGES ON "class".* TO 'cxk'@'192.168.241.%' |
+------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> show grants for 'cxk'@'localhost';
+------------------------------------------------+
| Grants for cxk@localhost                       |
+------------------------------------------------+
| GRANT USAGE ON *.* TO 'cxk'@'localhost'        |
| GRANT SELECT ON "class".* TO 'cxk'@'localhost' |
+------------------------------------------------+
2 rows in set (0.00 sec)

4.撤销用户权限

mysql> show grants for 'cxk'@'192.168.241.%';
+------------------------------------------------------------+
| Grants for cxk@192.168.241.%                               |
+------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'cxk'@'192.168.241.%'                |
| GRANT ALL PRIVILEGES ON "class".* TO 'cxk'@'192.168.241.%' |
+------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> revoke all on class.* from 'cxk'@'192.168.241.%';
#撤销cxk用户192.168.241网段的主机所有class数据库的权限
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for 'cxk'@'192.168.241.%';
+---------------------------------------------+
| Grants for cxk@192.168.241.%                |
+---------------------------------------------+
| GRANT USAGE ON *.* TO 'cxk'@'192.168.241.%' |
+---------------------------------------------+
1 row in set (0.00 sec)

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

mysql> show grants for 'cxk'@'192.168.241.%';
+---------------------------------------------+
| Grants for cxk@192.168.241.%                |
+---------------------------------------------+
| GRANT USAGE ON *.* TO 'cxk'@'192.168.241.%' |
+---------------------------------------------+
1 row in set (0.00 sec)

六、总结

1.查看数据库、数据表和表结构的操作

  • 查看数据库show databases
  • 查看数据表show tables
  • 查看表结构describe tablename或desc tablename

2.创建库和表的操作及删除库和表的操作

  • 创建库create database databasename
  • 创建表create table tablename
  • 删除库drop database databasename
  • 删除表drop table tablename

3.数据表的增、删、改、查等操作

  • 数据表增:Insert into tablename
  • 数据表改:update tablename set 指定内容
  • 数据表删:delete from tablename
  • 数据表查:select(*、where、limit、\G)
  • 数据表结构名称改:alter table tablename rename 新名称
  • 数据表扩展结构增:alter table tablename add 扩展名
  • 数据表字段内容改:alter table tablename change 原结构字段名称 新结构字段名称
  • 数据表字段内容删:alter table tablename drop 指定结构字段

 4.数据库表的清空表、临时表和克隆表操作

4.1清空表

  • delete from tablename
  • truncate table tablename
  • drop from tablename

4.2临时表

create temporary tabletablename

4.3克隆表

  • Like方法:create table 新表 like旧表------->insert into 新表 select *from旧表
  • Show create table方法:create table 新表(select * from 旧表)

5.数据库的用户授权相关操作

5.1数据库用户管理

新建用户:create user '用户名'@'localhost(或者指定IP)' identified by '密码'

重命名:rename user '旧用户名'@'localhost(或者指定IP)' to '新用户名'@'localhost(或者指定IP)'

删除用户:drop user '用户名'@'localhost(或者指定IP)'

修改当前密码:set password = password('密码')

修改其他用户密码:set password for '用户名'@'localhost' = password('新密码');

修改密码:update mysql.user set authentication_string = password('abc123') where user='root'

修改密码:flush privileges------>set password for root@localhost=password('123123')

5.2数据库授权

授权:grant 权限列表 ON 数据库名.表名 to '用户名'@'来源地址' identified by '密码'

查看权限:show grants '用户名'@'来源地址'

撤销权限:revoke 权限列表 on 数据库名.表名 from '用户名'@'来源地址'

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/473705.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

查看angular版本的问题The Angular CLI requires a minimum Node.js version of v18.13.

angular版本与node.js版本不匹配的问题 下载安装angular 查看版本,发现不匹配 安装指定版本即可 查看版本并运行

网络编程-DAY6

1>创建一个武器信息库,包含编号(主键)、名称、属性、描述、价格 2>添加三把武器 3>修改某把武器的价格 4>展出价格在1000到4000的武器 5>卖掉一把武器,删除该武器的信息 6>几天后,客户顶着光头…

【Qt】使用Qt实现Web服务器(四):传递参数、表单提交和获取请求参数

1、示例 1)演示 2)提交 3)显示 2、源码 1)示例源码Demo1->FormController void FormController::service(HttpRequest& request, HttpResponse& response) {

3.6 条件判断语句cmp,je,ja,jb及adc、sbb指令

汇编语言 1. adc指令 adc是带进位加法指令,它利用了CF位上记录的进位值指令格式:adc 操作对象1,操作对象2功能:操作对象1 操作对象1 操作对象2 CF例如:adc ax,bx,实现的功能是:ax ax bx …

嵌入式中MCU内存管理分配算法对比

本文主要介绍内存的基本概念以及操作系统的内存管理算法。 一、内存的基本概念 内存是计算机系统中除了处理器以外最重要的资源,用于存储当前正在执行的程序和数据。 内存是相对于CPU来说的,CPU可以直接寻址的存储空间叫做内存,CPU需要通过驱动才能访问的叫做外存。…

uniapp通过script引入外部sdk的方法

文章目录 一、index.html引入二、动态引入1.App.vue引入2.单页面引入 一、index.html引入 例如 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /><script>var coverSupport CSS in window && type…

【项目部署】git自动化部署项目

Git自动化部署项目 前言Git自动化部署项目自动化脚本shnodejs监听端口服务PM2启动node服务创建WebHooks 思考总结 前言 本次以egg后端项目关联gitee自动化部署为例子&#xff0c;涉及PM2进程管理工具、WebHooks、自动化脚本sh、nodejs监听端口服务等知识&#xff0c;此外服务器…

mysql笔记:23. 在Mac上安装与卸载MySQL

文章目录 下载MySQL安装包1. 打开MySQL官网&#xff0c;点击DOWNLOADS2. 点击GPL Downloads3. 点击MySQL Community Server打开下载页面4. 选择需要的文件进行下载5. ARM or x86 DMGbrewTAR卸载1. 在系统中卸载2. 在终端中卸载 MySQL对Mac电脑的适配十分强大&#xff0c;再加上…

晶圆为什么要抛光?

为什么要把晶圆打磨的这么光滑? 晶圆的最终命运是被切成一枚枚芯片(die),封装在暗无天日的小盒子里,只露出几枚引脚,芯片会看阈值,阻值,电流值,电压值,就是没人看它的颜值,我们在制程中,反复给晶圆打磨抛光,还是为了满足生产中的平坦化需要,尤其是在每次做光刻时…

使用Pygame做一个乒乓球游戏(2)使用精灵重构

本节没有添加新的功能&#xff0c;而是将前面的功能使用精灵类(pygame.sprite.Sprite) 重构。 顺便我们使用图片美化了一下程序。 看到之前的代码&#xff0c;你会发现代码有点混乱&#xff0c;很多地方使用了全局变量(global)。 本节我们将使用类进行重构。 Block(Sprite)…

【phoenix】flink程序执行phoenix,phoenix和flink-sql-connector-hbase包类不兼容

问题报错 Caused by: java.lang.RuntimeException: java.lang.RuntimeException: class org.apache.flink.hbase.shaded.org.apache.hadoop.hbase.client.ClusterStatusListener$MulticastListener not org.apache.hadoop.hbase.client.ClusterStatusListener$Listener如下图&…

基于cnn深度学习的yolov5+pyqt+分类+resnet+骨龄检测系统

往期热门博客项目回顾&#xff1a; 计算机视觉项目大集合 改进的yolo目标检测-测距测速 路径规划算法 图像去雨去雾目标检测测距项目 交通标志识别项目 yolo系列-重磅yolov9界面-最新的yolo 姿态识别-3d姿态识别 深度学习小白学习路线 YOLOv5与骨龄识别 YOLOv5&a…

NCV7428D15R2G中文资料PDF数据手册参数引脚图图片价格概述参数芯片特性原理

产品概述&#xff1a; NCV7428 是一款系统基础芯片 (SBC)&#xff0c;集成了汽车电子控制单元 (ECU) 中常见的功能。NCV7428 为应用微控制器和其他负载提供低电压电源并对其进行监控&#xff0c;包括了一个 LIN 收发器。 产品特性&#xff1a; 控制逻辑3.3 V或5 V VOUT电源&…

(css)步骤条el-steps区分等待、进行中、完成三种状态的图片

(css)步骤条el-steps区分等待、进行中、完成三种状态的图片 效果&#xff1a; <el-steps :active"active" finish-status"success" class"steps"><el-step title"选择.."></el-step><el-step title"..规则&…

【Java】使用 Java 语言实现一个冒泡排序

大家好&#xff0c;我是全栈小5&#xff0c;欢迎阅读小5的系列文章。 这是《Java》系列文章&#xff0c;每篇文章将以博主理解的角度展开讲解&#xff0c; 特别是针对知识点的概念进行叙说&#xff0c;大部分文章将会对这些概念进行实际例子验证&#xff0c;以此达到加深对知识…

鸿蒙Harmony应用开发—ArkTS-高级组件:@ohos.arkui.advanced.ComposeTitleBar(头像和单双行文本标题栏)

一种普通标题栏&#xff0c;支持设置标题、头像&#xff08;可选&#xff09;和副标题&#xff08;可选&#xff09;&#xff0c;可用于一级页面、二级及其以上界面配置返回键。 说明&#xff1a; 该组件从API Version 10开始支持。后续版本如有新增内容&#xff0c;则采用上角…

Linux CentOS 7.6安装mysql5.7.26详细保姆级教程

一、通过wget下载mysql安装包 1、下载 //进入home目录 cd /home //下载mysql压缩包 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz //解压 tar -xvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz //重命名文件夹 mv mys…

【Springboot3+Mybatis】文件上传阿里云OSS 基础管理系统CRUD

文章目录 一、需求&开发流程二、环境搭建&数据库准备三、部门管理四、员工管理4.1 分页(条件)查询4.2 批量删除员工 五、文件上传5.1 介绍5.2 本地存储5.3 阿里云OSS1. 开通OSS2. 创建存储空间Bucket 5.4 OSS快速入门5.5 OSS上传显示文件 六、配置文件6.1 yml配置6.2 C…

【设计模式】Java 设计模式之模板命令模式(Command)

命令模式&#xff08;Command&#xff09;的深入分析与实战解读 一、概述 命令模式是一种将请求封装为对象从而使你可用不同的请求把客户端与接受请求的对象解耦的模式。在命令模式中&#xff0c;命令对象使得发送者与接收者之间解耦&#xff0c;发送者通过命令对象来执行请求…

【NLP】多头注意力(Multi-Head Attention)的概念解析

一. 多头注意力 多头注意力&#xff08;Multi-Head Attention&#xff09;是一种在Transformer模型中被广泛采用的注意力机制扩展形式&#xff0c;它通过并行地运行多个独立的注意力机制来获取输入序列的不同子空间的注意力分布&#xff0c;从而更全面地捕获序列中潜在的多种语…