记录一些MySQL数据库常用的命令行

前言

此文是笔者学习MySQL数据库时用到的一些基础命令行,在此记录一下,方便回忆,如果能给您带来些许帮助也是极好的。


用管理员身份打开cmd net start mysql 启动数据库

基本命令

  1. 进入到mysql安装路径的bin目录下

    1
    cd MySQL\mysql-5.7.22-winx64\bin
  2. 打开数据库命令:mysql -uroot -p

  3. 停止数据库命令:net stop mysql
  4. 查看版本 select version();
  5. 查看当前时间 select now();
  6. 远程连接 mysql -h ip地址 -u 用户名 -p

数据库操作

  1. 创建数据库
    • 格式:create database 数据库名称 charset=utf8;
  2. 删除数据库
  • 格式:drop database 数据库名:
  1. 切换数据库
    • 格式:use 数据库名
  2. 查看当前选择的数据库
  • 格式:select database();

表操作

  1. 查看当前数据库中所有表
    • 格式:show tables;
  2. 创建表

    • 格式:create table 表名(列及类型)
    • 说明:

      1
      2
      3
      auto_increment 表明自增长  
      primary key 表明主键
      not null 表示不为空
    • 示例:

      1
      creart table student(id int aut_increment primary key, name varchar(20) not null, age int not null, gender bit default 1, address varchar(20), isDelete bit default 0);
  3. 删除表

    • 格式:drop table 表名;
    • 示例:
      1
      drop table student;
  4. 查看表结构

    • 格式:desc 表名;
    • 示例:
      1
      desc student;
  5. 查看建表语句

    • 格式:show create table 表名;
    • 示例:
      1
      show create table student;
  6. 重命名表名

    • 格式:rename table 原表名 to 新表名;
    • 示例:
      1
      rename table car to newCar;

数据操作

  1. 全列插入

    • 格式:insert into 表名 values(...);
    • 说明:主键列是自动增长的,但是在全列插入时需要占位,通常使用0,插入成功以后以实际数据为准
    • 示例:
      1
      insert into student values(0,"tom",19,1,"北京",0);
  2. 缺省插入

    • 格式:insert into 表名(列1,列2,...) values(值1,值2,...);
    • 示例:
      1
      insert into student(name,age,address) values("lilie",19,"上海");
  3. 同时插入多条数据

    • 格式:insert into 表名 values(...),(...),...;
    • 示例:
      1
      insert into student values(0,"hanmeimei",18,0,"北京",0),(0,"poi",18,1,"海南",0),...;

  • 格式:delete from 表名 where 条件;
  • 示例:

    1
    delete from student where id=2;
  • 注意:没有条件是全部删除,慎用

  • 格式: updata 表名 set 列1=值1,列2=值2,... where 条件;
  • 示例:
    1
    updata student set age=16 where id=5;

  1. 基本语法

    • 格式:select * from 表名;
    • 说明:as aa 表示给name起一个别名
    • 示例:
      1
      2
      3
      select * from student;
      select name, age from student;
      select name as aa, age from student;
  2. 消除重复行

    • 在select后面列的前面使用distinct可以消除重复行
    • 示例:
      1
      2
      select gender from student;
      select distinct gender from student;
  3. 条件查询

    • 语法 select * from 表名 where 条件;

    • 比较运算符: = > < >= <= !=或<>

    • 需求:查询id值大于5的所有数据

    • 示例:

      1
      select * from student where id>5;
    • 逻辑运算符: and or not

    • 需求:查询id值大于4的女同学

    • 示例:

      1
      select * from student where id>4 and gender=0;
    • 模糊查询: like

    • %表示任意多个任意字符 _表示一个任意字符

    • 需求:查询姓李的同学
    • 示例:

      1
      select * from student where name like "李%";
    • 范围查询: in 表示在一个非连续的范围内; between…and.. 表示在一个连续的范围内

    • 需求:查询标号为2 4 7的学生

    • 示例:

      1
      select * from student where id in (2, 4, 7);
    • 需求:查询编号为4到7的学生

    • 示例:

      1
      select * from student where id between 4 and 7;
    • 空判断

    • 注意:null与””是不同的
    • 判断空 is null
    • 判断非空 is not null
    • 需求:查找没有地址的同学
    • 示例:
      1
      select * from student where address is null;
  4. 聚合

  • 为了快速得到统计数据,提供了5个聚合函数
    1
    2
    3
    4
    5
    count(*)
    max(列)
    min(列)
    sum(列)
    avg(列)
  1. 分组

    • 语法:select 列1, 列2. 聚合... from 表名 group by 列1, 列2,...
    • 需求:查询男女总数
    • 示例:

      1
      select gender, count(*) from student group by gender;
    • 分组后筛选数据:

    • 语法:select 列1, 列2. 聚合... from 表名 group by 列1, 列2,...having 列1,...
  • where与having的区别:where是对from后面指定的表进行筛选,属于对原始数据的筛选
    • having是对group by的结果进行筛选
  1. 排序
  • 语法:·select * from 表名 order by 列1 asc|desc, 列2 asc|desc,…·
    • asc升序 desc降序 默认升序
    • 需求:按年龄排序
    • 示例:
      1
      select * from stufent order by age;
  1. 分页
    • 语法:select * from 表名 limit start,count;
    • 说明:start索引从0开始
    • 示例:
      1
      select * from student limit 0,3;

关联

  • 建表语句:
1
2
1.
create table class(id int auto_increment primary key, name varchar(20) not null, stuNum int not null);
1
2
2.
create table students(id int auto_increment primary key, name varchar(20) not null, age int not null, gender bit default 1, classid int not null, foreign key(classid) references class(id));

总结:

这里只是最基本的MySQL语句,基础但很重,写在这里,如果想学习更多推荐文档教程菜鸟教程

如果觉得文章不错,请我吃根辣条吧~~