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("标记删除数据成功");
        }
    })
相关推荐
Three~stone40 分钟前
Matlab2025b的安装教程(附安装包和密钥破解文件)
学习·mysql·持续部署
4***571 小时前
数据库(MySQL):使用命令从零开始在Navicat创建一个数据库及其数据表(一).创建基础表
数据库·mysql·oracle
F***74171 小时前
数据库课设---酒店管理系统(MySQL、VBNet)
android·数据库·mysql
1***t8272 小时前
MySQL 8.0安装
数据库·mysql·adb
U***74692 小时前
Linux(CentOS)安装 MySQL
linux·mysql·centos
3***g2052 小时前
Linux系统离线部署MySQL详细教程(带每步骤图文教程)
linux·mysql·adb
c***69302 小时前
使用mysql报Communications link failure异常解决
数据库·mysql
t***q332 小时前
关于Mysql 中 Row size too large (> 8126) 错误的解决和理解
数据库·mysql
i***t91911 小时前
Linux下MySQL的简单使用
linux·mysql·adb