mysql基础学习

SQL基础资料

  • 关系型数据库

  • 在数据库增删改查

  • 创建数据库

    基础语法

    SELECT

    • 大小写不敏感
    • 注释 -- 空格
    sql 复制代码
    -- 通过*号查询所有数据  users(表名)
    SELECT * FROM users;
    ​
    -- 查询 username, password 的数据
    SELECT username, password FROM users

    insert into

    sql 复制代码
    -- 插入字段  
    insert into users (username,password) values ('汤姆','666666')

    update

    bash 复制代码
    -- 更新数据 id为2的数据修改
    update users set password='999999', status=1  where id=2

    delete

    sql 复制代码
    -- 删除数据  
    delete from users where id=3

    where 使用

    csharp 复制代码
     -- where 使用
     select * from users where id=2
     select * from users where id>1
     select * from users where username != '汤姆'

    and or 使用

    sql 复制代码
    -- and使用
    select * from users where status = 0 and id < 3
    ​
    -- or使用
    select * from users where status = 0 or username = '汤姆'

    order by 排序

    vbnet 复制代码
    -- order by 排序 (默认升序) asc
    select * from users order by status
    select * from users order by id asc
    -- 降序 order by  **** desc
    select * from users order by id desc

    -- 多重排序

    sql 复制代码
    -- 多重排序  status-降序 id-升序
    select * from users order by status desc, id asc

    统计数据 count(*)

    sql 复制代码
    -- 统计数据  count(*)
    select count(*) from users where status = 0

    as关键字 设置别名

    csharp 复制代码
    -- as关键字 设置别名
    select count(*) as total from users where status = 0
    select username as uanme, password as pwd from users

    项目操作数据库

    npm install mysql

    javascript 复制代码
    const mysql = require('mysql')
    //建立mysql数据库链接
    const db = mysql.createPool({
        host:'127.0.0.1',//数据库IP地址
        user:'root',//数据库登录账号
        password:'admin123', //数据库登录密码
        database:'my_db_01',//指定操作那个数据库
    })
    ​
    //测试
    db.query('SELECT 1',(err,res)=>{
        console.log(err);//null
        console.log(res);//[ RowDataPacket { '1': 1 } ]
    })

    查询数据

    javascript 复制代码
    const mysql = require('mysql')
    //建立mysql数据库链接
    const db = mysql.createPool({
        host:'127.0.0.1',//数据库IP地址
        user:'root',//数据库登录账号
        password:'admin123', //数据库登录密码
        database:'my_db_01',//指定操作那个数据库
    })
    //查询数据
    const sqlStr = 'select * from users'
    db.query(sqlStr, (err, res) => {
        console.log(res);//打印数据列表
    })

    插入数据

    javascript 复制代码
    const mysql = require('mysql')
    //建立mysql数据库链接
    const db = mysql.createPool({
        host:'127.0.0.1',//数据库IP地址
        user:'root',//数据库登录账号
        password:'admin123', //数据库登录密码
        database:'my_db_01',//指定操作那个数据库
    })
    ​
    //插入数据
    const user = {
        username:"测试",
        password:"ceshimima"
    }
    //定义执行的sql语句
    const sqlStr = 'insert into users (username ,password) values (?,?)'
    //执行sql语句
    db.query(sqlStr,[user.username,user.password],(err,res)=>{
        if(err) return console.log(err);
        if(res.affectedRows === 1) {
            console.log('插入数据成功');
        }
    })
    ​
    ​
    //插入数据简写
    const user = {
        username:"测试便捷",
        password:"ceshimimabj",
        status:1
    }
    //定义执行的sql语句
    const sqlStr = 'insert into users set ?'
    //执行sql语句
    db.query(sqlStr,user,(err,res)=>{
        if(err) return console.log(err);
        if(res.affectedRows === 1) {
            console.log('插入数据成功');
        }
    })

    更新数据

    javascript 复制代码
    const mysql = require('mysql')
    //建立mysql数据库链接
    const db = mysql.createPool({
        host:'127.0.0.1',//数据库IP地址
        user:'root',//数据库登录账号
        password:'admin123', //数据库登录密码
        database:'my_db_01',//指定操作那个数据库
    })
    ​
    //更新数据
    const user = {
      id: 6,
      username: "更新数据",
      password: "gxsj000",
    };
    //定义执行的sql语句
    const sqlStr = "update users set username=?,password = ? where id=? ";
    //执行sql语句
    db.query(sqlStr, [user.username, user.password,user.id], (err, res) => {
      if (err) return console.log(err);
      if (res.affectedRows === 1) {
        console.log("更新数据成功");
      }
    });
    ​
    //更新数据简写
    const user = {
        id: 12,
        username: "新数据简写",
        password: "gxsj1110jx",
      };
      //定义执行的sql语句
      const sqlStr = "update users set ? where id=? ";
      //执行sql语句
      db.query(sqlStr, [user,user.id], (err, res) => {
        if (err) return console.log(err);
        if (res.affectedRows === 1) {
          console.log("更新数据成功");
        }
      });
    ​

    删除数据

    javascript 复制代码
    const mysql = require('mysql')
    //建立mysql数据库链接
    const db = mysql.createPool({
        host:'127.0.0.1',//数据库IP地址
        user:'root',//数据库登录账号
        password:'admin123', //数据库登录密码
        database:'my_db_01',//指定操作那个数据库
    })
    ​
    //删除数据
    const sqlStr = "delete from users where id=? ";  
    //一个占位符可以省略数组
    db.query(sqlStr,4,(err,res)=>{
        if (err) return console.log(err);
        if (res.affectedRows === 1) {
            console.log("删除数据成功");
        }
    })
    ​
    //标记删除
    const sqlStr = "update users set status=? where id=?";
    //标记删除id为6的数据  status=1 - 标记删除
    db.query(sqlStr,[1,6],(err,res)=>{
        if (err) return console.log(err);
        if (res.affectedRows === 1) {
            console.log("标记删除数据成功");
        }
    })
相关推荐
‎ദ്ദിᵔ.˛.ᵔ₎21 小时前
MySQL 表约束
数据库·mysql
linmengmeng_13141 天前
【总结】MyBatis-Plus批量插入与清表的正确姿势
java·spring boot·mysql·mybatis
达梦数据1 天前
DMDRS空间数据类型说明:Oracle、DM8、MySQL空间数据类型映射
数据库·mysql·oracle
lusklusklusk1 天前
Oracle索引组织表,SQLServer聚集索引,Mysql Innodb表的数据行不是在磁盘上像排队一样严格连续地存放
mysql·postgresql·oracle·sqlserver
螺蛳粉 螺蛳粉1 天前
mysql的备份与恢复
linux·运维·数据库·mysql
计算机毕设定制辅导-无忧学长1 天前
《基于SpringBoot的中学教师数字胜任力测评网站的设计与实现》
java·vue.js·spring boot·mysql·中学教师数字胜任力测评网站
guo_wen_qiang1 天前
mysql中有哪些日志
数据库·mysql
天天喝旺仔1 天前
MySQL索引优化实战:B+Tree原理、索引失效与覆盖索引调优
数据库·sql·mysql·性能优化
lqg_zone1 天前
CentOS 7 虚拟机磁盘 I/O 卡顿排查实录:从 iostat 异常到虚拟盘后端故障的完整定位
linux·服务器·mysql·centos