• mysql 数据库语法


    • 显示数据库:show databases;
      -查看 mysql 版本: select version();
      primary key 主键 auto_increment 自动递增 not null ,要求该约束所修饰的字段,不能为null或空 unsigned 约束的字段,会去掉负值,添加到正值,范围 x 2 + 1
      排序
      命令格式:select * from 表名 order by 字段名 asc(升序)|desc(降序);
      多字段排序
      select * from department order by groups desc,kpi desc;
      多字段排序时,先按第一字段排序,第一段相同的,再按第二字段排序。

    创建

    • 创建数据库命令格式: create database 数据库名字 charset=字符集
    • 创建表命令格式: create table 表名( 字段 属性 );

    删除

    • 删除表
      命令格式:drop table 表名;
    • 删库
      格式: drop database 库名;
      删除主键
      alter table 表名 drop 主键字段名;
      删除数据
      命令格式
      delete from 表名 where 【条件】

    修改

    • 修改已创建的表名
      命令格式:alter table 原表名 rename to 新表名;
    • 10.1.38-MariaDB 修改密码:
      update mysql.user set password = password('密码') where user='要修改的用户(root)';
      flush privileges;
      exit;
      修改创建好的表字
      段命令格式: alter table 表名 change 原字段名 新字段名 字段类型 字段属性(约束);
      添加新的字段
      命令格式: alter table 表名 add 字段名 字段类型 字段属性(约束);
      修改表名
      命令格式:alter table 旧表名 rename to 新表名;
      创建表后修改主键
      alter table 表名 change 原字段名(要设为主键的字段) 新字段名 int primary key auto_incremnt not null;
      修改数据
      update 表名 set 字段名1=要修改值1,字段名2=要修改值2 where [条件]

    查找

    • 基本查询
      命令格式:select [要查询的字段,如果是所有字段,就是*;如果单个,写字段名] from 【表名】 where 【条件】

    • 范围查询
      命令格式: select [字段] from 表名 where 字段名 between 开始 and 结束
      in ( ) 在 ... 里 not in

    • 模糊查询
      命令格式: select [字段] from 表名 where 字段 like ...%...

    • 内联接查询
      命令格式:select [字段] from 表名1 inner join 表名2 on 表名1.连接字段=表名2.连接字段

    • 右链接
      select * from student s right join class c on s.cls_id=c.id

    • 左连接
      select * from class c left join student s on s.cls_id=c.id;

    • 全联接
      select * from class c left join student s on c.id=s.cls_id
      union
      select * from class c right join student s on c.id=s.cls_id;

    • 自联接(自查询)
      表和自身的连接,使用 inner join 来完成

    • 插入数据

    • 命令格式:
      insert into 表名(字段名1,字段名2,字段名3.....)values(值1,值2,值3...);

    • 另外一种形式
      insert into 表名 values(字段1的值,字段2的值,字段3的值。。。。);

    • 分组
      group by 分组

    • 分组 group_concat()+group by

    • 聚合函数

    • 统计函数 count()
      命令格式: select count(要统计的字段) from 表名 where [条件]

    • 求最大值
      命令格式:select max(求最大值的字段) from 表名;

    • 求最小值
      命令格式:select min(要计算最小值的字段) from 表名;

    • 求和
      命令格式:
      select sum(要求和/总数的字段) from 表名;

    • 求平均值
      select avg(要求平均值字段) from 表名;

    having
    having 和 where 都 可以对记录进行筛选;但是having跟在group by 后面,group by 跟在 where 后面;having 后面条件必须在 select 字段中出现,没有,就会报错;where 是必须是表中字段
    where ... group by ... having

    视图

    • 命令格式:
      create view 视图名字 as select语句;
    • 查看视图
      命令格式:show tables;
    • 删除视图
      drop view 视图名字
    • 修改视图
      create or replace view 视图名字 as select语句;

    索引

    • 查看索引
      命令格式:
      show index from 表名;
    • 创建索引的命令
      格式:
      create index idx_索引名 on 表名(字段名(索引长度))
    • 删除索引
      命令格式:
      drop index 索引名称 on 表名;
  • 相关阅读:
    aop 切面编程
    动态代理模式
    idea 从接口方法 跳转到 实现类 对应的方法
    2019年的某一天
    javaweb . 页面登出 操作
    Could not find acceptable representation报错
    window下mysql character_set_server修改不生效问题
    git 命令推送
    spring boot 2.x + elasticsearch+mybatis-plus
    java8的Consumer函数式接口
  • 原文地址:https://www.cnblogs.com/chao460/p/14146549.html
Copyright © 2020-2023  润新知