• MySQL 基本语法


    1.说明: 创建默认数据库
    create database 库名;


    2.说明: 创建指定字符编码的数据库
    create database 库名 character set 字符编码格式;


    3.说明: 删除数据库
    drop database 库名;


    4.说明: 创建新表
    create table 表名 (列名1 列类型 列约束[是否为空,是否为主键,是否主键自动增加...],
    列名2  列类型 列约束[是否为空,是否为主键,是否主键自动增加...],
    ...
    列名n 列类型 列约束[是否为空,是否为主键,是否主键自动增加...]
    );


    5.说明: 删除表
    drop table 表名;


    6.说明: 修改表结构
    ①说明: 添加列
    alter table 表名 add 列名 列类型 [约束];
    ②说明: 修改列
    alter table 表名 change 列名 列新名 新列类型 [约束];
    ③说明: 删除列
    alter table 表名 drop 列名;
    ④说明: 重命名表
    alter table 表名 rename 新表名;
    ⑤说明: 修改表的存储引擎
    alter table 表名 engine = '存储引擎名';


    7.说明: 查看当前数据库服务器下所有的数据库
    show databases;


    8.说明: 查看当前数据库的编码格式
    show variables like 'character%';


    9.说明: 设置当前客户端字符集
    set character_set_client = 字符格式;


    10.说明: 设置连接字符集
    set character_set_connection = 字符格式;


    11.说明: 设置结果字符集
    set character_set_results = 字符格式;


    12.说明: 使用指定数据库
    use 库名;


    13.说明: 查看当前数据库下所有的表
    show tables;


    14.说明: 查看表结构
    desc 表名;/describe 表名;


    15.说明: 查看之前所定义的表的结构的语法
    show create table 表名;


    16.说明: 查询表中所有数据
    show * from 表名;


    17.说明: 查询表中指定条件的数据
    show * from 表名 where 筛选条件;


    18.说明: 插入数值
    insert into 表名(需要插入数据的列1,列2...列n)  values(对应列的数值1,数值2...数值n);


    19.说明: 修改表中数据
    update 表名 set 列=‘新值’where 限定修改条件;


    20.说明: 清空表中数据(可恢复)
    delete from 表名;


    21.说明: 清空表中数据(不可恢复)
    truncate table 表名;


    22.说明: 删除符合指定条件的数据
    delete from 表名 where 指定条件;


    23.说明: 增加指定名称的主键
    alter table 表名 add constraint 指定名称(PK) primary key (需要设为主键的列名);


    24.说明: 删除表中主键
    alter table 表名 drop primary key [主键名];


    25.说明: 添加指定名称的外键
    alter table 从表名 add constraint 指定名称(FK) foreign key (从表中列名) references 主表(主键列);


    26.说明: 删除表中指定名称的外键
    alter table 表名 drop foreign key [外键名];


    27.说明: 查询表中指定列的值(消除结果中的重复值)
    select distinct 列名 from 表名;


    28.说明: 分页查询
    select 列名 from 表名 where 条件 limit 开始位置,表示行数;


    29.说明: select 语法
    select [all|distinct]
    *|表名.*|表名.列名 [as 列别名]
    from 
    表名[as 表别名]
    [left|right|inner join 表名2] -- 联合查询
    [where 指定条件] -- 指定结果需要满足的条件
    [group by 进行分组的列名] -- 指定结果按照哪几个字段进行分组
    [having 指定分组结果的次要条件] -- 过滤分组的记录必须满足的次要条件
    [order by 进行排序的列名 排序方式] -- 指定查询记录按一个或多个条件排序
    [limit 限制条件];


    30.说明: 范围查询
    select * from 表名 where 列名 between 小值 and 大值;


    31.说明: 模糊查询
    select * from 表名 where 列名 like '模糊查询条件';


    32.说明: 查询模糊查询条件中带转义字符的数据
    select * from 表名 where 列名 like '转义字符符号_%' escape '转义字符符号';


    33.说明: 包含查询
    select * from 表名 where 列名 in (范围);


    34.说明: 不包含
    select * from 表名 where not in (范围);


    35.说明: 数据为空查询
    select * from 表名 where 列名 is null;


    36.说明: 数据不为空查询
    select * from 表名 where 列名 is not null;


    37.说明: 多表查询
    select * from 表1,表2,...表n where 多表中的关系条件;


    38.说明: 分组查询
    select * from 表名 [where 筛选条件] group by 分组列名 [having 分组结果筛选条件];


    39.说明: 内连接查询
    ①隐式内连接
    select 要查询的列 from 表1,表2...表n where 多表关系条件;
    ②显示内连接
    select 要查询的列 from 表1 inner join 表2 on 多表关系条件;


    40.说明: 外连接查询
    ①左连接
    select 要查询的列 from 表1 left join 表2 on 多表关系条件;
    ②右连接
    select 要查询的列 from 表2 right join 表2 on 多表关系条件;


    41.说明: 变量赋值使用
    ① set @变量名 = 赋值; 或 set @变量名 := 赋值;
    ② select @变量名 := 赋值; 或 select @变量名 := 字段名 from 表名 where 条件;


    42.说明: if 条件语句
    select if(表达式1,表达式2,表达式3) from 表名 [where 筛选条件...]; 判断表达式1,成立返回表达式2结果,否则返回表达式3结果。


    43.说明: case 条件语句
    ① select case when 条件1 then 结果1 when 条件2 then 结果2 ... else 其他结果 end [别名] from 表名 [where 筛选条件...];
    ② select case 要判断的字段或表达式 when 常量1 then 结果1 ... else 其他结果 end [别名] from 表名 [where 筛选条件...];


    44.说明: 查询 session 变量
    show session variables;


    45.说明: 查询 global 变量
    show global variables;


    46.说明: 设置系统变量新值
    set @@变量类型.变量名 = 新值;


    47.说明: 主键索引的添加
    ①创建表时添加: create table 表名 (列名 类型 primary key);
    或: create table 表名 (列名 类型 ...,primary key (索引列));
    ②创建表后添加: alter table 表名 add primary key (索引列);


    48.说明: 唯一索引的添加
    ①创建表时添加: create table 表名 (列名 类型 unique);
    或: create talbe 表名 (列名 类型...,unique key [索引名] (索引列));
    ②创建表后添加: alter table 表名 add unique key [索引名] (索引列);


    49.说明: 常规索引的添加
    ①创建表时添加: create table 表名 (列名 类型 ..., index/key [索引名] (索引列1,索引列2,..));
    ②创建表后添加: alter table 表名 add index [索引名] (索引列1,索引列2...);


    50.说明: 全文索引的添加 (只能用于MyISAM 类型的数据表)(用于char,varchar,text数据列类型)
    ①创建表时添加: create table 表名 (列名 类型 ...,fulltext(索引列))engine = MyISAM;
    ②创建表后添加: alter table 表名 add fulltext [索引名] (索引列);


    51.说明: 删除索引
    ① drop index 索引名 on 表名;
    ② alter table 表名 drop index 索引名;
    ③ alter table 表名 drop primary key;


    52.说明: 查看索引
    show index[或keys] from 表名;


    53.说明: 分析SQL语句的执行性能
    explain 表名; <==> desc 表名
    explain select 语句;


    54.说明: 创建视图
    create view 视图名 as <select语句>;


    55.说明: 删除视图
    drop view 视图名;


    56.说明: 查看视图
    select * from 视图名;


    57.说明: 查看所有视图
    use information_schema;
    select * from views;


    58.说明: 创建自定义函数
    create function 函数名(参数列表)
    returns 返回值类型
    【begin】 函数主体 【end】;


    59.说明: 调用函数
    select 函数名;


    60.说明: 删除函数
    drop function 函数名;


    61.说明: 自定义函数中的变量
    declare 变量名【,变量名2...】 变量数据类型 【default 默认值】;


    62.说明: 给变量赋值
    select 要赋的值 into 要赋值的变量名;
    <==> select 要赋值的变量名 := 要赋的值;
    <==> set 要赋值的变量名 = 要赋的值;


    63.说明: 流程控制语句语法:
    ① if 条件判断结构
    if 条件判断语句1 then 满足条件1输出的结果;
    elseif 条件判断语句2 then 满足条件2输出的结果;
    ...
    else 都不满足上述条件输出的结果;
    end if;
    ② case 分支结构
    Ⅰ: 等值判断
    case 判断字段/列/变量
    when 值1 then 等于值1输出的结果;
    when 值2 then 等于值2输出的结果;
    ...
    else 都不满足上述条件输出的结果;
    end case;
    Ⅱ: 区间判断
    case 
    when 判断条件1 then 满足条件1输出的结果;
    when 判断条件2 then 满足条件2输出的结果;
    ...
    else 都不满足上述条件输出的结果;
    end case;


    64.说明: Loop 循环(死循环,一般需要跟leave、iterate)
    [begin_label:] Loop
    循环语句
    end Loop [end_label];


    65.说明: Leave 跳出循环控制
    leave label;


    66.说明: iterate 跳出本次循环
    iterate label;


    67.说明: repeat 循环 (满足条件跳出循环)
    [begin_label:] repeat
    循环语句;
    until 结束循环条件
    end repeat [end_label];


    68.说明: while 循环 (满足条件时,执行循环内的条件)
    [begin_label:] while 参与循环执行的条件 DO
    循环语句;
    end while [end_label];


    69.说明: 创建存储过程
    create procedure 存储过程名称 ([存储过程参数INoutinout] 参数名称 参数类型)
    begin
    存储过程的主体;
    end;


    70.说明: 存储过程调用
    call 存储过程名称(参数);


    71.说明: 删除存储过程
    drop procedure 存储过程名称;


    72.说明: 查看库内所有存储过程
    show procedure status;


    73.说明: 定义异常
    declare 异常名 condition for 异常类型; (sqlstate_value 例: sqlstate '42000'或 mysql_error_code 例: 1148)


    74.说明: 异常捕获
    declare 错误处理方式 handler for 异常名[,...] 错误代码 (处理方式:continue,exit,undo)


    75.说明: 关闭自动提交模式
    set autocommit = 0;


    76.说明: 开启自动提交模式
    set autocommit = 1;


    77.说明: 开始事务
    start transaction;


    78.说明: 提交事务
    commit;


    79.说明: 数据回滚
    rollback;

  • 相关阅读:
    objective c 中基本类型的操作
    [转载]Server.MapPath和Request.MapPath()的用法
    [转载]mstsc远程报:这可能是由于CredSSP 加密Oracle修正的两种完美解决方法
    [转载]忘记token怎么加入k8s集群
    ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded; 的解决办法
    [转载]AutoMapper 9.0的改造
    [转载]k8s注册节点提示Docker SystemdCheck]: detected cgroupfs" as the Docker cgroup dr iver. The r ecommended dr fiver is" systemd"
    [转载]Linux的Vi命令详解
    [转载]查看虚拟机里的Centos7的IP
    [转载]centos关闭swap分区
  • 原文地址:https://www.cnblogs.com/798911215-Darryl-Tang/p/9118570.html
Copyright © 2020-2023  润新知