mySQL数据库

128 阅读2分钟

SQL基础资料

  • 关系型数据库

  • 在数据库增删改查

  • 创建数据库

    基础语法

    SELECT

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

    insert into

    -- 插入字段  
    insert into users (username,password) values ('汤姆','666666')
    

    update

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

    delete

    -- 删除数据  
    delete from users where id=3
    

    where 使用

     -- where 使用
     select * from users where id=2
     select * from users where id>1
     select * from users where username != '汤姆'
    

    and or 使用

    -- and使用
    select * from users where status = 0 and id < 3-- or使用
    select * from users where status = 0 or username = '汤姆'
    

    order by 排序

    -- 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
    

    -- 多重排序

    -- 多重排序  status-降序 id-升序
    select * from users order by status desc, id asc
    

    统计数据 count(*)

    -- 统计数据  count(*)
    select count(*) from users where status = 0
    

    as关键字 设置别名

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

    项目操作数据库

    npm install mysql

    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 } ]
    })
    

    查询数据

    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);//打印数据列表
    })
    

    插入数据

    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('插入数据成功');
        }
    })
    

    更新数据

    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("更新数据成功");
        }
      });
    ​
    

    删除数据

    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("标记删除数据成功");
        }
    })