Skip to content

Latest commit

 

History

History
269 lines (241 loc) · 9.94 KB

07.mySql.md

File metadata and controls

269 lines (241 loc) · 9.94 KB

mySql

简介

  • 常见数据库有 mysql、oracle、sql server、mongodb
  • 其中 mysql、oracle、sql server 属于传统型数据库(又叫做关系型数据库或sql数据库),这三者的设计理念相同,用法比较类似
  • 而 Mongodb 属于新型数据库(又叫做: 非关系型数据库 或 NOSQL 数据库),它在一定程度上弥补了传统型数据库的缺陷

常用语句

  • drop database if exists xxx; 创建该库时有相同的则删除 利于后面的创建覆盖

  • create database xxx charset utf8; 创建库 并设置编码格式

  • alter database 数据库名 character set 编码方式; 指定数据库设置编码格式

    • alter database studentdb库名 character set 'utf8'; 修改已创建数据库编码格式
    • alter table student表名 convert to character set 'utf8'; 修改已创建表编码格式
  • use xxx; 进入指定库

  • create table xxx( id int , name varchar(20)); 创建一张表 并添加字段

  • drop table xxx; 删除指定的表

  • drop database xxx; 删除指定的库

  • show databases; 查看当前所有的库

  • show tables; 查看所有表

  • 排序 order by

    • select * from test1 order by state desc; 默认升序 关键字asc 降序 关键字 desc
    • select * from test1 order by state desc ,useName asc; 多个条件排序
  • count(*) 返回查询结果的总匹配条数

    • select count(*) from test1 where state=4 ;
  • as 字段设置别名

    • select state s 状态 from test1 ; s 可以省略
  • and 并列条件 与 && 必须同时满足所有

  • or 交集条件 或 || 多个条件任意满足一个即可

  • in() 查询某个范围内的数据 例:查找id为1和2的数据 select * from 表名 where id in(1,2)

  • not in() 与in() 方法相反 例:查找id不为1和2的其他数据

where 条件

  • = 等于
  • <> 不等于
  • < 小于
  • <= 小于等于
  •      大于
  • = 大于等于

  • between 在xx区间内
  • like 搜索某种模式

数据内容的增删改查

  • insert into xxx表名 values (1,'张三'); 向指定表中添加一条数据 需要抒写所有字段
  • insert into xxx表名(id,name字段) values (1,'张三'),(2,'李四'),(3,'王五'); 向指定表中添加多数据 只需要抒写指定的字段

  • delete from xxx表名 where 列名=值 (条件); 根据条件删除表中的一条数据

  • update xxx表名 set 字段=值; 修改整个字段值
  • update xxx表名 set 字段=值修改后的值 where 字段=值 (条件); 用于指定条件的数据 进行更新

  • select * xxx字段名 from xxx表名; 查看指定表中的指定字段数据
  • select * xxx字段名1,xxx字段名2 from xxx表名; 查看指定表中的指定多个字段数据
  • select * from xxx表名; 查看指定的表

数据表 字段的增删改

  • 1.修改表名
    • alter table 旧表名 rename 新表名;
  • 2.修改表字段名
    • alter table 表名 change 旧字段名 新字段名 新字段类型;
  • 3.修改表字段类型
    • alter table 表名 modify 字段名 新字段类型;
  • 4.添加字段
    • alter table 表名 add 字段名 字段类型;
  • 5.删除字段
    • alter table 表名 drop 字段名;
  • 6.调换字段顺序
    • alter table 表名 modify 字段名1 数据类型 first|after 字段名2;

约束

  • 设置 主键 PRIMARY KEY() 字段
  • 设置 id自动递增
    • 设置自增需要把id设置为主键
    • 设置了自增 添加数据的时候可以省略
    • demo
      create table mytable( #创建一张表
      id int auto_increment,
      PRIMARY KEY(id)  #将id设置为主键
      );

字段类型

    1. 整数类型

      数据类型 数据范围
      TINYINT -128 -- 127
      SMALLINT -32768 -- 32767
      MEDIUMINT -2^23 -- 2^23-1
      INT -2^31 -- 2^31-1
      BIGINT -2^63 -- 2^63-1
  • ​ 2.字符串类型

    数据类型 数据范围 用途
    CHAR(n) 0 -- 255字节 定长字符串
    VARCHAR(n) 0 -- 65535字节 变长字符串
    TEXT 0 -- 65535字节 长文本数据
    LONGTEXT 0 -- 2^32-1字节 极大文本数据
    BLOB 0 -- 65535字节 二进制长文本数据
    LONGBLOB 0 -- 2^32-1字节 二进制极大文本数据
  • 3.小数类型

    • m 表示浮点数的总长度,n 表示小数点后有效位数。
    数据类型 数据用法 数据范围
    Float Float(m,n) 7位有效数
    Double Double(m,n) 15位有效数
    Decimal Decimal(m,n) 28位有效数
  • 4.时间类型

    数据类型 格式 用途
    DATE YYYY-MM-DD 日期
    TIME HH:MM:SS 时间
    YEAR YYYY 年份
    DATETIME YYYY-MM-DD HH:MM:SS 日期和时间
    TIMESTAMP 10位或13位整数(秒数) 时间戳
    1. 枚举类型
    • enum(枚举值1,枚举值2,...) 枚举类型只能在列出的值中选择一个,如性别。

mysql中的三种注释方式

  • 方式1 -- 单行注释
  • 方式2 # 单行注释
  • 方式3 /* */ 多行注释

通过模块操作数据库 增删改查

  • 查找数据
    • demo
        //引用内置模块
      const express = require('express');
      //引用mysql   安装 npm i mysql -s
      const mysql = require('mysql');
      let datas = mysql.createConnection({
          host: "127.0.0.1",      //地址
          user: 'root',           //用户名
          password: '',           //密码
          database: 'lc_study2',  //数据库名
      })
    
      datas.query('select *from  mytable', (err, res) => {
          if (err) return console.log(err.message);
          console.log(res);
      })
  • 插入数据 简单和复杂写法
    • demo
            //复杂写法
               const express = require('express');
          //引用mysql   安装 npm i mysql -s
          const mysql = require('mysql');
          let datas = mysql.createConnection({
              host: "127.0.0.1", //地址
              user: 'root', //用户名
              password: '', //密码
              database: 'a', //数据库名
          })
          const adds = {
              useName: '小明',
              pwd: '6787',
              state: 200
          }
          //sql 语句 value(?,?,?) 有几个数据 添加一个?
          const sql = 'insert into test1 (useName,pwd,state) values (?,?,?)';
          datas.query(sql, [adds.useName, adds.pwd, adds.state], (err, res) => {
              if (err) return console.log(err.message);
              console.log(res);
              if (res.affectedRows == 1) { //affectedRows 受影响的行数 为1 代表成功
                  console.log('数据添加成功');
              }
          })
      
         //  插入数据简单写法
        const express = require('express');
        //引用mysql   安装 npm i mysql -s
        const mysql = require('mysql');
        let datas = mysql.createConnection({
            host: "127.0.0.1", //地址
            user: 'root', //用户名
            password: '', //密码
            database: 'a', //数据库名
        })
        const adds = {
            useName: '小明',
            pwd: '6787',
            state: 200
        }
        const sql = 'insert into test1 set ?';
        datas.query(sql, adds, (err, res) => {
            if (err) return console.log(err.message);
            console.log(res);
            if (res.affectedRows == 1) { //affectedRows 受影响的行数 为1 代表成功
                console.log('数据添加成功');
            }
        })
  • 改变数据
    • demo
         const express = require('express');
      const mysql = require('mysql');
      let datas = mysql.createConnection({
          host: "127.0.0.1", //地址
          user: 'root', //用户名
          password: '', //密码
          database: 'a', //数据库名
      })
      const adds = {
          useName: '小明',
          pwd: '7878',
          state: 500
      }
      // - update xxx表名 set 字段=值修改后的值 where 字段=值 (条件);   用于指定条件的数据 进行更新
      const sql = 'update test1 set ? where useName="小红"';
      datas.query(sql, adds, (err, res) => {
          if (err) return console.log(err.message);
          console.log(res);
          if (res.affectedRows == 1) { //affectedRows 受影响的行数 为1 代表成功
              console.log('数据添加成功');
          }
      })
  • 删除数据
    • demo
       const express = require('express');
       const mysql = require('mysql');
       let datas = mysql.createConnection({
           host: "127.0.0.1", //地址
           user: 'root', //用户名
           password: '', //密码
           database: 'a', //数据库名
       })
       //  const sql = ' delete from test1  where id=1 || id=5';
       const sql = ' delete from test1  where id in(1,5)'; // 使用 in()  删除 id 为1和id为5的数据
       datas.query(sql, (err, res) => {
           if (err) return console.log(err.message);
           console.log(res);
       })

其他

版本连接出现问题 (8版本前后 身份验证协议发生改变)

  • 报错 Client does not support authentication protocol requested by server; consider upgrading MySQL client
    • 客户端不支持服务器请求的身份验证协议;考虑升级MySQL客户端
  • 解决方式 先登录 mysql -u root -p"123456"

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';