• 一文搞懂│mysql 的语法操作命令从入门到精通


    🎈 初级语法之登录、库操作、表操作、数据操作,索引

    • 登录数据库:mysql –u用户名 [-h服务器IP地址] [-P端口] -p密码
    • 数据库操作之显示所有数据库:show databases;
    • 数据库操作之创建数据库:create database 数据库名;
    • 数据库操作之选择数据库:use 数据库名称;
    • 数据操作之查看执行查询效率:explain sql语句
    • 数据库操作之删除数据库
    drop database 数据库名;
    drop database if exists 数据库;
    
    • 1
    • 2
    • 数据表操作之显示所有数据表
    show tables;
    show tables in 数据库名;
    
    • 1
    • 2
    • 数据表操作之创建数据表
    create table 表名(
        -- code
    )engine=innodb comment '用户表';
    
    • 1
    • 2
    • 3
    • 数据表操作之删除数据表
    drop table 表名;
    drop table if exists 表名;
    
    • 1
    • 2
    • 数据表操作之显示表结构
    describe 表名;
    describe 表名\G;
    -- 推荐简写方式
    desc 表名;
    
    • 1
    • 2
    • 3
    • 4
    • 数据表操作之显示建表语句
    show create table 表名;
    show create table 表名\G;
    
    • 1
    • 2
    • 数据表操作之修改表的属性
    alter table 表名 属性名=新属性值;
    alter table test engine=MyISAM;
    
    • 1
    • 2
    • 数据表操作之修改表名或者将表迁移数据库
    rename table [库名.]旧表名 to [库名.]新表名;
    
    • 1
    • 数据表操作之向已有的一个表中添加字段
    alter table 表名 add 新字段名 字段类型 [字段属性列表];
    
    • 1
    • 数据表操作之删除表字段
    alter table 表名 drop 字段名
    
    • 1
    • 数据表操作之修改已有字段名称
    alter table 表名 change 旧字段名 新字段名 新字段类型 [字段属性];
    
    • 1
    • 数据表操作之修改表字段类型和属性
    alter table 表名 modify 字段名 字段新类型 [字段新属性列表];
    
    • 1
    • 数据操作之新增数据
    insert into 表名 values (字段1, 字段2, … 字段n值);
    insert into 表名 (字段1, 字段2, … 字段n) values (字段1, 字段2, … 字段n值);
    
    • 1
    • 2
    • 数据操作之查询数据
    select 字段列表 from [库名.]表名 [where 查询条件];
    
    • 1
    • 数据操作之更新数据
    update 表名 set 字段1=字段1新值, 字段2=字段2新值,. 字段n=字段n新值 where 条件语句;
    
    • 1
    • 数据操作之删除数据
    delete from 表名 where 条件语句;
    
    • 1
    • 数据操作之清空表并重新规划索引
    truncate [库名.]表名;
    
    • 1
    • 主键索引之建立字段自增
    Alter table 表名 modify 字段名 字段类型 auto_increment
    
    • 1
    • 主键索引之建立主键索引
    alter table 表名 add primary key(字段名称)
    
    • 1
    • 主键索引之删除主键索引
    Alter table 表名 drop primary key;
    
    • 1
    • 主键索引之删除字段自增
    alter table 索引名 modify id int unsigned;
    
    • 1
    • 唯一索引之创建唯一索引
    create unique index 索引的名称 on 表名(字段名称)
    
    • 1
    • 唯一索引之删除唯一索引
    alter table 表名 drop index 索引的名称
    
    • 1
    • 普通索引之创建普通索引
    create index 索引的名称 on 表名(字段名称);
    
    • 1
    • 普通索引之删除普通索引
    alter table 表名 drop index 索引的名称
    
    • 1
    • 前缀索引之创建前缀索引
    alter table 表名 add index 索引名称(字段名称(长度)
    • 1
    • 前缀索引之删除前缀索引
    alter table 表名 drop index 索引名称
    
    • 1

    🎈 高级语法之主键、试图、变量、流程、函数、存储过程

    • 高级语法之主键
    • 使用外键的前提:外键所存在的表必须是 Innodb 引擎的、
    • 默认情况下被关联的外键数据是 无法删除
    • 默认情况下被关联的字段值是无法被修改的,非关联字段可以修改
    • 主表关联字段修改后,会影响外键字段的值
    • 主表数据删除后,外键字段会自动修改为 NULL
    -- 创建外键
    create table tableName(
        foreign key (外键字段) references 主表 (主表关联字段) [删除时执行语句] [更新时执行语句];
    )engine=Innodb charset=utf-8;
    
    -- 删除外键
    alter table 表名 drop  foreign key 外键标识;
    -- 删除索引
    alter table 表名 drop index 索引字段名;
    
    -- 添加外键
    alter table 表名 add foreign key (外键字段) references 主表 (主表关联字段) [删除时执行语句] [更新时执行语句];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 高级语法之视图
    • 应用场景:有一个查询语句非常复杂,写太多很麻烦,可以用一个视图来代替这个 select 语句,充当一个 变量 角色
    • 视图其实是以表子查询的 SQL 语句的形态存在的
    • 操作视图就相当于操作原表,一般不会直接操作视图,保持视图数据的干净
    • 重启 MYSQL 后,在不同的黑窗口客户端中依然能够使用重启之前创建的视图
    • 视图 可以 在多个客户端进程中通用,视图必须在 选择数据库之后 才能进行操作
    -- 创建视图
    create view 视图名称 as (查询SQL语句);
    
    -- 删除试图,删除的时候不会影响源表
    drop view 视图名称;
    drop view if exists 视图名称;
    
    -- 查看数据库中所有视图
    select * from information_schema.views;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 高级语法之变量
    • mysql变量分类:系统变量用户变量局部变量
    • 查看全局所有变量:show global variables;
    • 查看所有的会话:show session variables;
    • 修改全局变量:set @@global.变量名=变量值;,重启数据库后,set方式修改的全局变量将会被重新初始化
    • 修改会话变量:set @@session.变量名=变量值;,重启数据库之后,set方式修改的会话变量的值将会被还原
    • 定义用户变量:set @变量名=变量值;
    • 定义局部变量:declare 变量名 变量数据类型 [default 默认值];
    • 定义mysql结束符:delimiter 符号
    -- 定义用户变量
    set @变量名=变量值;
    set @变量名=变量值,@变量名=变量值;
    
    -- 获取用户变量
    select @变量名,@变量名;
    
    -- 通过select语句定义用户变量,将查询结果中的最后一条数据的name,age,email分别赋值给不同的用户变量
    select @var1=name,@var2=age,@var3=email from table where 1;
    
    -- 定义局部变量示例
    declare var1 int default 0;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 高级语法之流程分支
    • 流程分支:if分支while循环
    if  条件语句  then
    	if结构体
    [elseif  条件语句  then
    	elseif结构体]
    else
    	else结构体
    end if;
     
    -- if分支示例
    if @var > 100 then
        insert into values('');
    else
        insert into values('');
    end if;
    
    -- while循环
    while 条件 do
    	循环结构体
    end while;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 高级语法之函数
    -- 定义函数
    create  function  函数名(形参1 形参1数据类型, 形参2  形参2数据类型,, 形参n  形参n数据类型)
    returns  预估的函数返回值数据类型
    [begin]
    函数的结构体
    [end]
    
    -- 使用函数
    select  函数名(实参列表);
    
    -- 删除函数
    drop  function  函数名;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 高级语法之存储过程
    • 参数的传递类型:
    • in:只进不出(只传值不传名) 在存储过程中修改值将不影响外部变量
    • out:只出不进(只传名不传值) 在存储过程中无法获得值
    • inout:能够进也能够出(即传名也传值) 最常用的类型
    -- 创建存储过程
    create  procedure  存储过程名(数据传递类型  形参1  形参1数据类型, 数据传递类型  形参2  形参2数据类型,, 数据传递类型  形参n  形参n数据类型)
    begin
    	存储过程结构体
    end
    
    -- 调用存储过程
    call  存储过程名(实参列表);
    
    -- 删除存储过程
    drop  procedure  存储过程名;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    🎈 查询语法之联合查询、子查询、链接查询

    • 联合查询:联合两条查询数据
    -- 如果存在相同的数据,则保留其中一条
    (select * from table where id>200)
    union
    (select * from table where id>200);
    
    -- 如果存在相同的数据,则全部保留
    (select * from table where id>200)
    union all
    (select * from table where id>200);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 标量子查询:本条查询语句的查询条件 是 另一个查询语句查询出来的单个值
    -- 查询id为10的老师所带的所有班级信息
    select * from class where teacher_name = (
        select name from teacher where id = 10
    );
    
    • 1
    • 2
    • 3
    • 4
    • 列子查询:本条查询语句的查询条件 是 另一个查询语句查询出来的一列结果
    -- 查询id为10和30的老师所带的所有班级
    select * from class where teacher_name in (
        select name from teacher where id = 10 or id=30
    );
    
    • 1
    • 2
    • 3
    • 4
    • 行子查询:本条查询语句中的查询条件 是 另一个查询语句所查出来的一行记录
    select * from class where (teacher_name,teacher_num)=(
        select name,num from teacher where id = 10
    )
    
    • 1
    • 2
    • 3
    • 表子查询:本条查询语句所查询的表 是 另一个查询语句查询出来的一个虚拟表结果集
    -- 蠕虫复制
    insert into table values(select * from teacher where 1);
    
    • 1
    • 2
    • exists 查询
    -- 查询出table1和table2表name字段同时存在相同值的数据
    select * from table1 where exists (select * from table2 where table1.name == table2.name);
    
    • 1
    • 2
    • 连接查询之内连接:在两张表中,如果任意一张表的数据根据条件连不上另外一张表中的数据,则这条记录直接被忽略不展示出来
    select * from table1 inner join table2 on table1.name = table2.name;
    -- 内连接的简写方式可以省略inner关键字
    select * from table1 join table2 on table1.name = table2.name;
     
    -- 在内连接中,where关键字可以代替on关键字
    select * from table1 join table2 where table1.name = table2.name;
     
    -- using演示案例  两个表存在相同的name字段且值相同才连接
    select * from table1 join table2 on using(name);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 连接查询之交叉连接:所谓的交叉连接,指的是将左表的每条数据都与右表的每条数据连接一次
    -- 如果内连接不指定条件,就是交叉连接。
    select * from table1 inner join table2;
    
    • 1
    • 2
    • 连接查询之左外连接
    select * from table left outer join table2 on table.name = table2.name;
    -- 简写
    select * from table left join table2 on table.name = table2.name;
    
    • 1
    • 2
    • 3
    • 连接查询之右外连接
    select * from table right outer join table2 on table.name = table2.name;
    -- 简写
    select * from table right join table2 on table.name = table2.name;
    
    • 1
    • 2
    • 3
    • 连接查询之全外连接
    -- 左外连接 和 右外连接 联合起来
    (select * from table right join table2 on table.name = table2.name)
    union
    (select * from table right join table2 on table.name = table2.name);
    
    • 1
    • 2
    • 3
    • 4
    • 连接查询之自然连接:两张表有且仅有一个相同的字段,否则数据会出错,所以,实际项目中并没有什么卵用
    -- 自然内连接 等同于 内连接使用using条件
    select * from table join table2 using(name);
    -- 等同于
    select * from table natural join table2;
     
    -- 自然左外连接
    select * from table natural left join table2;
    -- 自然右外连接
    select * from table natural right join table2;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    vue2——电商项目 黑马
    第02章 SpringBoot概述
    云计算 - 负载均衡SLB方案全解与实战
    前端基础知识
    个人如何进行深度复盘?这6大高效的复盘模型,让你的年终总结如虎添翼!
    Go中为什么json.Unmarshal为什么需要指向map的指针?
    AUTOSAR汽车电子嵌入式编程精讲300篇-汽车 CAN FD 总线应用研究(下)
    无处不在的智慧:嵌入式技术引领智能生活
    电容笔和Apple pencil的区别有哪些?十大电容笔知名品牌
    1688搜索店铺列表 API
  • 原文地址:https://blog.csdn.net/weixin_41635750/article/details/126930768