MySQL实操学习笔记

Env:Server version: 8.0.22 + macOS 11.2

发展:结绳记事 - 甲骨记事 - 图书馆 - 文件 - 数据库

数据库的优点:

  • 持久化存储
  • 读写速度极高
  • 保证数据的有效性
  • 对程序支持性非常好,容易扩展

关系型数据库核心元素:

  • 数据行(记录)
  • 数据列(字段)
  • 数据表(数据行的集合)
  • 数据库(数据表的集合)

RDBMS

  • Relational Database Management System
  • 通过表来表示关系型

当前主要使用的两种数据库:关系型数据库和非关系型数据库

关系型数据库RDBMS,是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据
主要产品:
- oracle:大型项目中使用,银行,电信等项目
- mysql:web时代使用最广泛的关系型数据库
- ms sql server:在微软的项目中使用
- sqlite:轻量级数据库,主要应用在移动平台

RDBMS-client -(通过SQL语句) RDBMS-server -(交流) 数据库(包含各个数据表)

SQL:结构化查询语言,用来操作RDBMS的数据库语言,当前关系型数据库都支持SQL语言进行操作,也就是说可以通过SQL操作oracle,sql sserver,mysql,sqlite等所有的关系型数据库

SQL语句主要分为:

  • DQL:数据查询语言,用于对数据进行查询,如select
  • DML:数据操作语言,对数据进行增加、修改、删除,如insert、udpate、delete
  • TPL:事务处理语言,对事务进行处理,包括begin transaction、commit、rollback
  • DCL:数据控制语言,进行授权与权限回收,如grant、revoke
  • DDL:数据定义语言,进行数据库、表的管理等,如create、drop
  • CCL:指针控制语言,通过控制指针完成表的操作,如declare cursor

注:不区分大小写,但是为了速度更快,尽量使用大写

MySQL特点(了解):
- 使用C和C++编写,并使用了多用编译器进行测试,保证源代码的可移植性
- 支持多种操作系统,如Linux、Windows、AIX、FreeBSD、HP-UX、MacOS、NovellNetware、OpenBSD、OS/2 Wrap、Solaris等
- 为多种编程语言提供了API,如C、C++、Python、Java、Perl、PHP、Eiffel、Ruby等
- 支持多线程,充分利用CPU资源
- MySQL使用标准的SQL数据语言形式
- 优化的SQL查询算法,有效地提高查询速度
- 提供多语言支持,常见的编码如GB2312、UTF-8
- 提供TCP/IP、ODBC和JDBC等多种数据库连接途径
- 提供用于管理、检查、优化数据库操作的管理工具
- 大型的数据库。可以处理拥有上千万条记录的大型数据库
- 支持多种存储引擎
- MySQL 软件采用了双授权政策,它分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择MySQL作为网站数据库
- Mysql是可以定制的,采用了GPL协议,你可以修改源码来开发自己的Mysql系统
- 在线DDL更改功能
- 复制全局事务标识
- 复制无崩溃从机
- 复制多线程从机

数据类型:

  • 使用数据类型的原则:够用就行,尽量使用取值范围小的,而不用大的,这样可以更多的节省存储空间
    常用数据类型:
    • 整数:int,bit
    • 小数:decimal
    • 字符串:varchar,char
    • 日期时间:date,time,datetime
    • 枚举类型

可视化mysql控制器:Navicat
工作中主要使用命令操作方式,要求熟练编写

select version();
select now();

修改输入提示符

prompt python> \D(完整日期) \U(使用用户)

prompt(修改为默认提示符)

数据库操作:
show databases;
use database;
select database();
create database dbname charset=utf8;
drop database dbname;
show tables;
desc tablename; # 查看表结构
create table tablename(
column1 datatype contrai,
column2 datatype,
column3 datatype,

columnN datatype,
primary key

);
select aid,atitle from areas where pid = ‘540200’; # 前面添加explain语句会得到下面的结果
explain select aid,atitle from areas where pid = ‘540200’;
±—±------------±------±-----------±-----±--------------±-----±--------±-----±-----±---------±------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
±—±------------±------±-----------±-----±--------------±-----±--------±-----±-----±---------±------------+
| 1 | SIMPLE | areas | NULL | ALL | NULL | NULL | NULL | NULL | 3219 | 10.00 | Using where |
±—±------------±------±-----------±-----±--------------±-----±--------±-----±-----±---------±------------+

建立索引,唯一索引,聚合索引

create index pid on areas(pid);
create unique pid on areas(pid);

  • 添加字段
    alter table tablename add colname type;
    alter table students add birthday datetime;
  • 修改字段且重命名
    alter table tablename change oldcolname newcolname type;
    alter table students change birthday birth datetime not null;
  • 修改字段但不重命名
    alter table tablename modify colname type;
    alter table students modify birthday date not null;
  • 删除字段
    alter table tablename drop colname;
    alter tbale students drop birthday;

drop table tablename;

  • 查看表的创建语句
    show create database dbname
    ±---------±--------------------------------------------------------------------------------------------------+
    | Database | Create Database |
    ±---------±--------------------------------------------------------------------------------------------------+
    | areas | CREATE DATABASE areas /*!40100 DEFAULT CHARACTER SET utf8 / /!80016 DEFAULT ENCRYPTION=‘N’ */ |
    ±---------±--------------------------------------------------------------------------------------------------+
    show create table tablename;
    ±------±-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table | Create Table |
    ±------±-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | areas | CREATE TABLE areas (
    aid int NOT NULL,
    atitle varchar(255) DEFAULT NULL,
    pid int DEFAULT NULL,
    PRIMARY KEY (aid)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC |
    ±------±-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

  • 查询基本使用
    select * from tablename;
    select col_1,col_2 from tablename;

  • 主键设置的是auto_increment,但在全列插入(值的顺序与表中字段的顺序对应)时需要占位,通常使用0或者default或者null
    insert into tablename values(…);

  • 部分列插入:值的顺序与给出的列顺序对应
    insert into tablename(name,hometown,birthday) values(‘黄蓉’,‘桃花岛’,‘2021-01-01);

  • 插入多行
    insert into tablename values(value1,value2,value3),(value11,value22,value33),(…)
    insert into tablename(name,hometown,birthday) values(),(),()

  • 修改
    update tablename set col_1=value1,col_2=value2,col_3=value where 条件;

  • 删除
    delete from tablename where 条件;
    delete from tablename where id=3;

  • 由于数据的重要性,所以一般不会用物理删除,会用逻辑删除代替
    update tablename set isdelete=1 where id=3;

  • 备份
    mysqldump -uroot -p dbanme >python.sql

  • 恢复
    连接mysql,创建新的数据库newdbname
    mysql -uroot -p newdbname < python.sql

  • 建立模型(E-R模型):
    数据库可以被建模为

    • 实体集合
    • 实体间的联系
      实体(Entity)是一个存在且区别于其他对象的对象
    • 特定的一个人/公司/事件/工厂
      实体有属性
    • 人的名字和住址
      实体集是具有吸纳沟通类型,即相同性质和属性的实体集合
      联系(relationship)是一个几个实体间的关联

三范式:设计数据库的规范

  • 第一范式:强调的是列的原子性,即列不能够再分成其他几列
    比如【联系人】表,电话列如果同时包含家庭电话和公司电话,那么这种表结构就没达到1NF,要复合需要把电话拆分,即分成家庭电话和公司电话

  • 第二范式:1NF的基础上,另外包含两部分内容,一是表必须有一个主键,二是没有包含在逐渐中的列必须完全依赖于主键,而不能只依赖于主键的一部分
    比如【订单明细】表,一个订单中可以订购多种产品,所以单单一个orderid不足以称为主键,主键应该是(orderid,productid)
    (OrderID,ProductID,UnitPrice,Discount,Quantity,ProductName)
    很明显unitprice,productname只依赖于prodictid,所以不符合2NF;
    可以把【OrderDetail】表拆分为【OrderDetail】(OrderID,ProductID,Discount,Quantity)和【Product】(ProductID,UnitPrice,ProductName)
    来消除原订单表中UnitPrice,ProductName多次重复的情况。

  • 第三范式:首先是2NF,另外非主键列必须直接依赖于主键,不能存在传递依赖

  • 查询
    select id as 序号,name as 名字,height as 身高 from students;
    select s.id,s.name,s.height from students as s;
    消除重复行查询:select distinct id,name,height from students;

  • 条件

    • 比较运算符:>, =, <, <=, >=, !=
    • 逻辑运算符:and, or, not
    • 模糊查询: like, %表示人一多个任意字符, _表示一个任意字符
    • 范围查询: in表示在一个非连续的范围内, between … and …表示在一个连续的范围内
    • 空判断: 注意null和’'是不同的,判断为空is null,非空is not null
      查询没有填写身高的学生
      select * from students where height is null;
      select * from students where height is not null;
    • 优先级:小括号,not,比较运算符,逻辑运算符,and比or先运算
  • 排序
    select * from tablename order by col_1 asc|desc(升序或者降序),col_2 asc|desc;

  • 聚合函数

    • 总数:count()
      select count(*) from students;
    • 最大/小值:max(),min()
      select max(id) from students;
    • 求和:sum()
      select sum(age) from students where gender=‘男’;
    • 平均值:avg()
      select avg(age) from students where is_delete=0 and gender=‘女’;
  • 分组

    • group by:将查询结果按照一个或多个字段进行分组,字段值相同的为一组
    • group by + group concat()用来输出某个字段的集合
    • group by + 集合函数
      select gender,count(*) from students group by gender;
    • group by + having 分组查询后制定一些条件输出查询结果,having只能和group by一起用
      select gender,count() from students group by gneder having count()>2
    • group by + with rollup:在结果最后新增一行,记录当前列里所有记录的总和
      select gender,count(*) from students group by gender with rollup;
  • 获取部分行
    当数据量过大时,在一页中查看数据是一件非常麻烦的事
    select * from tablename limit start,count

查询前三行男生信息

select * from students where gender=1 limit 0,3

  • 分页
    数据总个数 = p1,每页显示m条数据,判断p1%m是否为0,若为0,总页数为p1/m,不为0,总页数为(p1/m + 1)

求第N页的数据

select * from students where is_delete=0 limit (n-1)*m,m;

  • 连接查询
    当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,在选择合适的列返回

mysql支持三种类型的连接查询,分别为:

  • 内连接查询:两个表联合,查交集,都有的显示出来

  • 右连接查询:左连接懂了就懂了

  • 左连接查询:以左表为准,连接右表,有值显示,没值右表的数据用null代替
    select * from tablename_1 inner/left/right join tablename_2 on tablename_1.col = tablename_2.col

    • 使用内连接查询班级表与学生表
      select * from students inner join classes on students.cls_id=classes.id

mysql> select * from classes;
±—±-------------+
| id | name |
±—±-------------+
| 1 | python_01期 |
| 2 | python_02期 |
| 3 | python_03期 |
±—±-------------+
3 rows in set (0.00 sec)

mysql> select * from students;
±—±----------±-----±-------±-------±-------±---------------------+
| id | name | age | height | gender | cls_id | is_delete |
±—±----------±-----±-------±-------±-------±---------------------+
| 1 | 小明 | 18 | 180.00 | 女 | 1 | 0x00 |
| 2 | 小月月 | 18 | 180.00 | 女 | 2 | 0x01 |
| 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | 0x00 |
| 4 | 刘德华 | 59 | 175.00 | 男 | 2 | 0x01 |
| 5 | 黄蓉 | 38 | 160.00 | 女 | 1 | 0x00 |
| 6 | 凤姐 | 28 | 150.00 | 保密 | 2 | 0x01 |
| 7 | 王祖贤 | 18 | 172.00 | 女 | 1 | 0x01 |
| 8 | 周杰伦 | 36 | NULL | 男 | 1 | 0x00 |
| 9 | 程坤 | 27 | 181.00 | 男 | 2 | 0x00 |
| 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | 0x00 |
| 11 | 金星 | 33 | 162.00 | 中性 | 3 | 0x01 |
| 12 | 静香 | 12 | 180.00 | 女 | 4 | 0x00 |
| 13 | 郭靖 | 12 | 170.00 | 男 | 4 | 0x00 |
| 14 | 周杰 | 34 | 176.00 | 女 | 5 | 0x00 |
±—±----------±-----±-------±-------±-------±---------------------+
14 rows in set (0.00 sec)

内连接

mysql> select * from students inner join classes where students.cls_id=classes.id;
±—±----------±-----±-------±-------±-------±---------------------±—±-------------+
| id | name | age | height | gender | cls_id | is_delete | id | name |
±—±----------±-----±-------±-------±-------±---------------------±—±-------------+
| 1 | 小明 | 18 | 180.00 | 女 | 1 | 0x00 | 1 | python_01期 |
| 2 | 小月月 | 18 | 180.00 | 女 | 2 | 0x01 | 2 | python_02期 |
| 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | 0x00 | 1 | python_01期 |
| 4 | 刘德华 | 59 | 175.00 | 男 | 2 | 0x01 | 2 | python_02期 |
| 5 | 黄蓉 | 38 | 160.00 | 女 | 1 | 0x00 | 1 | python_01期 |
| 6 | 凤姐 | 28 | 150.00 | 保密 | 2 | 0x01 | 2 | python_02期 |
| 7 | 王祖贤 | 18 | 172.00 | 女 | 1 | 0x01 | 1 | python_01期 |
| 8 | 周杰伦 | 36 | NULL | 男 | 1 | 0x00 | 1 | python_01期 |
| 9 | 程坤 | 27 | 181.00 | 男 | 2 | 0x00 | 2 | python_02期 |
| 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | 0x00 | 2 | python_02期 |
| 11 | 金星 | 33 | 162.00 | 中性 | 3 | 0x01 | 3 | python_03期 |
±—±----------±-----±-------±-------±-------±---------------------±—±-------------+
11 rows in set (0.00 sec)

mysql> select * from students inner join classes on students.cls_id=classes.id;
±—±----------±-----±-------±-------±-------±---------------------±—±-------------+
| id | name | age | height | gender | cls_id | is_delete | id | name |
±—±----------±-----±-------±-------±-------±---------------------±—±-------------+
| 1 | 小明 | 18 | 180.00 | 女 | 1 | 0x00 | 1 | python_01期 |
| 2 | 小月月 | 18 | 180.00 | 女 | 2 | 0x01 | 2 | python_02期 |
| 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | 0x00 | 1 | python_01期 |
| 4 | 刘德华 | 59 | 175.00 | 男 | 2 | 0x01 | 2 | python_02期 |
| 5 | 黄蓉 | 38 | 160.00 | 女 | 1 | 0x00 | 1 | python_01期 |
| 6 | 凤姐 | 28 | 150.00 | 保密 | 2 | 0x01 | 2 | python_02期 |
| 7 | 王祖贤 | 18 | 172.00 | 女 | 1 | 0x01 | 1 | python_01期 |
| 8 | 周杰伦 | 36 | NULL | 男 | 1 | 0x00 | 1 | python_01期 |
| 9 | 程坤 | 27 | 181.00 | 男 | 2 | 0x00 | 2 | python_02期 |
| 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | 0x00 | 2 | python_02期 |
| 11 | 金星 | 33 | 162.00 | 中性 | 3 | 0x01 | 3 | python_03期 |
±—±----------±-----±-------±-------±-------±---------------------±—±-------------+
11 rows in set (0.00 sec)

左连接

mysql> select * from students left join classes on students.cls_id=classes.id;
±—±----------±-----±-------±-------±-------±---------------------±-----±-------------+
| id | name | age | height | gender | cls_id | is_delete | id | name |
±—±----------±-----±-------±-------±-------±---------------------±-----±-------------+
| 1 | 小明 | 18 | 180.00 | 女 | 1 | 0x00 | 1 | python_01期 |
| 2 | 小月月 | 18 | 180.00 | 女 | 2 | 0x01 | 2 | python_02期 |
| 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | 0x00 | 1 | python_01期 |
| 4 | 刘德华 | 59 | 175.00 | 男 | 2 | 0x01 | 2 | python_02期 |
| 5 | 黄蓉 | 38 | 160.00 | 女 | 1 | 0x00 | 1 | python_01期 |
| 6 | 凤姐 | 28 | 150.00 | 保密 | 2 | 0x01 | 2 | python_02期 |
| 7 | 王祖贤 | 18 | 172.00 | 女 | 1 | 0x01 | 1 | python_01期 |
| 8 | 周杰伦 | 36 | NULL | 男 | 1 | 0x00 | 1 | python_01期 |
| 9 | 程坤 | 27 | 181.00 | 男 | 2 | 0x00 | 2 | python_02期 |
| 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | 0x00 | 2 | python_02期 |
| 11 | 金星 | 33 | 162.00 | 中性 | 3 | 0x01 | 3 | python_03期 |
| 12 | 静香 | 12 | 180.00 | 女 | 4 | 0x00 | NULL | NULL |
| 13 | 郭靖 | 12 | 170.00 | 男 | 4 | 0x00 | NULL | NULL |
| 14 | 周杰 | 34 | 176.00 | 女 | 5 | 0x00 | NULL | NULL |
±—±----------±-----±-------±-------±-------±---------------------±-----±-------------+
14 rows in set (0.00 sec)

右连接

mysql> select * from students right join classes on students.cls_id=classes.id;
±-----±----------±-----±-------±-------±-------±---------------------±—±-------------+
| id | name | age | height | gender | cls_id | is_delete | id | name |
±-----±----------±-----±-------±-------±-------±---------------------±—±-------------+
| 1 | 小明 | 18 | 180.00 | 女 | 1 | 0x00 | 1 | python_01期 |
| 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | 0x00 | 1 | python_01期 |
| 5 | 黄蓉 | 38 | 160.00 | 女 | 1 | 0x00 | 1 | python_01期 |
| 7 | 王祖贤 | 18 | 172.00 | 女 | 1 | 0x01 | 1 | python_01期 |
| 8 | 周杰伦 | 36 | NULL | 男 | 1 | 0x00 | 1 | python_01期 |
| 2 | 小月月 | 18 | 180.00 | 女 | 2 | 0x01 | 2 | python_02期 |
| 4 | 刘德华 | 59 | 175.00 | 男 | 2 | 0x01 | 2 | python_02期 |
| 6 | 凤姐 | 28 | 150.00 | 保密 | 2 | 0x01 | 2 | python_02期 |
| 9 | 程坤 | 27 | 181.00 | 男 | 2 | 0x00 | 2 | python_02期 |
| 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | 0x00 | 2 | python_02期 |
| 11 | 金星 | 33 | 162.00 | 中性 | 3 | 0x01 | 3 | python_03期 |
±-----±----------±-----±-------±-------±-------±---------------------±—±-------------+
11 rows in set (0.00 sec)

mysql> select * from classes left join students on students.cls_id=classes.id;
±—±-------------±-----±----------±-----±-------±-------±-------±---------------------+
| id | name | id | name | age | height | gender | cls_id | is_delete |
±—±-------------±-----±----------±-----±-------±-------±-------±---------------------+
| 1 | python_01期 | 1 | 小明 | 18 | 180.00 | 女 | 1 | 0x00 |
| 1 | python_01期 | 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | 0x00 |
| 1 | python_01期 | 5 | 黄蓉 | 38 | 160.00 | 女 | 1 | 0x00 |
| 1 | python_01期 | 7 | 王祖贤 | 18 | 172.00 | 女 | 1 | 0x01 |
| 1 | python_01期 | 8 | 周杰伦 | 36 | NULL | 男 | 1 | 0x00 |
| 2 | python_02期 | 2 | 小月月 | 18 | 180.00 | 女 | 2 | 0x01 |
| 2 | python_02期 | 4 | 刘德华 | 59 | 175.00 | 男 | 2 | 0x01 |
| 2 | python_02期 | 6 | 凤姐 | 28 | 150.00 | 保密 | 2 | 0x01 |
| 2 | python_02期 | 9 | 程坤 | 27 | 181.00 | 男 | 2 | 0x00 |
| 2 | python_02期 | 10 | 刘亦菲 | 25 | 166.00 | 女 | 2 | 0x00 |
| 3 | python_03期 | 11 | 金星 | 33 | 162.00 | 中性 | 3 | 0x01 |
±—±-------------±-----±----------±-----±-------±-------±-------±---------------------+
11 rows in set (0.01 sec)

mysql>

  • 自关联
    设计省信息的provinces
    • id
    • ptitle
      设计市信息的表结构citys
    • id
    • ctitle
    • proid
      citys表的proid表示城市所属的省,对于那个着provinces表的id值

查询省为‘河南省’的所有城市

select city.* from areas as city inner join areas as provinces on city.pid=provinces.aid where provinces.atitle=‘河南省’;

  • 子查询
    在一个select语句中,嵌入了另一个select语句,那么被嵌入的select语句称之为子查询语句,或者嵌套查询
    • 标量子查询
      select * from students where age >(select avg(age) from students);
      ±—±----------±-----±-------±-------±-------±---------------------+
      | id | name | age | height | gender | cls_id | is_delete |
      ±—±----------±-----±-------±-------±-------±---------------------+
      | 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | 0x00 |
      | 4 | 刘德华 | 59 | 175.00 | 男 | 2 | 0x01 |
      | 5 | 黄蓉 | 38 | 160.00 | 女 | 1 | 0x00 |
      | 6 | 凤姐 | 28 | 150.00 | 保密 | 2 | 0x01 |
      | 8 | 周杰伦 | 36 | NULL | 男 | 1 | 0x00 |
      | 11 | 金星 | 33 | 162.00 | 中性 | 3 | 0x01 |
      | 14 | 周杰 | 34 | 176.00 | 女 | 5 | 0x00 |
      ±—±----------±-----±-------±-------±-------±---------------------+
    • 列级子查询

      查询还有学生在班的所有班级名字

      select name from classes where id in (select cls_id from students);
      | name |
      ±-------------+
      | python_01期 |
      | python_02期 |
      | python_03期 |
      ±-------------+
    • 行级子查询

      查询班级年龄最大,身高最高的学生

      select * from students where age=(select max(age) from students) or height=(select max(height) from students);
      ±—±----------±-----±-------±-------±-------±---------------------+
      | id | name | age | height | gender | cls_id | is_delete |
      ±—±----------±-----±-------±-------±-------±---------------------+
      | 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | 0x00 |
      | 4 | 刘德华 | 59 | 175.00 | 男 | 2 | 0x01 |
      ±—±----------±-----±-------±-------±-------±---------------------+

总结:查询的完整格式
select distinct *
from tablename
where …
group by … having …
order by …
limit start,count;

  • 视图
    一条select语句执行后返回的结果集
    视图是对若干张基本表的引用,一张虚表,查询语句执行的结果
    方便操作,特别是查询操作,减少复杂的SQL语句,增强可读性

    • 定义
      create view v_name as select …;
    • 查看
      show tables;
    • 使用
      select * from v_name;
    • 删除试图
      drop view v_name;
    • 作用
      提高重用性,就像一个函数
      对数据库重构,却不影响程序的运行
      提高了安全性能,可以对不同的用户
  • 索引:相当于给数据分区,取快递的方式

    • 查看
      show index from tablename;
    • 创建
      create index ind_name on tablename(col_name(长度))
    • 删除索引
      drop index ind_name on tablename;

账户管理
- 查看所有用户
- 查看user表的结构
desc user;
- 查看所有用户
select host,user,authentication_string from user;
- 创建账户&授权
grant 权限列表 on 数据库 to ‘用户名’@‘访问主机’ identified by ‘密码’;
# 创建一个laowang的账号,密码为123456,只能通过本地访问, 并且只能对jing_dong数据库中的所有表进行读操作
grant select on jing_dong.* to ‘laowang’@‘localhost’ identified by ‘123456’;
- 查看用户有哪些权限
show grants for laowang@localhost;
# 创建一个laoli的账号,密码为12345678,可以任意电脑进行链接访问, 并且对jing_dong数据库中的所有表拥有所有权限
grant all privileges on jing_dong.* to “laoli”@"%" identified by “12345678”;
- 账户操作
- 修改权限
grant 权限名称 on 数据库 to 账户@主机 with grant option;
- 修改密码,修改完成后需要刷新权限
update user set authentication_string=password(‘123’) where user=‘laowang’;
flush privileges;
- 删除账户
drop user ‘username’@‘server’
drop user ‘laowang’@’%’;
delete from user where user=‘username’;
flush privileges;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值