首页 > 技术文章 > MySQL数据库

1kxj 2021-10-12 16:41 原文

一、数据库基本概念

1.什么是数据库
  • 数据库是用来组织、存储和管理数据的仓库
  • 为了方便管理互联网世界中数据,就有了数据库管理系统的概念。用户可以对数据库进行新增、查询、更新、删除等操作。
2.常见数据库及其分类
  • 常见数据库分类
    • MySQL 数据库
    • Oracle 数据库(收费)
    • SQL Server 数据库(收费)
    • Mongodb 数据库

二、SQL 语句

1、什么是SQL
  • SQL 是结构化查询语言,专门用来访问和数据库的编程语言,能够以变成形式,操作数据库里面的数据。
  • 三个关键点:
    • SQL 是一门数据库编程语言
    • 使用 SQL 语言编写出来的代码,叫做 SQL 语句
    • SQL 语言只能关系型数据库中使用(例如 MySQL、Oracle、SQL Server)。非关系型数据库(例如 Mongodb)不支持 SQL 语言
2、SQL 的语句
  • 数据库名字 users

    • 通过 * 把 users 表中的所有的数据查询出来
      select * from users
    • 把表格 username,password 项都找出来
      select username,password from users
    • 向数据表插入新的数据行
      insert into users (username,password) values ('mz','123')
      insert into users set username = 'mz',password = '123'
    • 选择性更新数据 where 后面更新的条件
      update users set password=8888,status=1 where id=4
    • 删除表中的行
      delete from users where id=4
    • where子句 运算符
    • 可以通过 where 子句来限定 select 的查询条件
    -- 查询 id 为 1 的所有用户
    select * from users where id=1
    -- 查询 username 不等于 zx 的所有用户
    select * from users where username<>'zx'
    
    • 使用 and 来显示所有 status=0,并且 id 小于3 的用户
      select * from users where status=0 and id<3
    • 使用 or 来显示所有 status 为1,或者 username 为 zs 的用户
      select * from users where status=0 or username='zs'
    • order by 子句 - 升序排序,按照 id
      select * from users order by id asc
    • order by 子句 - 降序排序 按 username 第一个字母进行降序排序
      select * from users order by username desc
    • 多重排序
      select * from users order by id asc,username desc
    • count(*)函数用于返回查询 status=1 结果的总数据条数的数量得到一个数字
      select count(*) from users where status=1
    • 使用 AS 为列设置别名
-- 将列名 username 改为 uname , password 改为 upwd
`select username as uname, password as upwd from users`

三、MySQL模块

1.作用
  • 通过 mysql 模块连接到 mySQL 数据库
  • 通过 mysql 模块执行 SQL 语句
2.安装配置 mysql 模块
  • 安装命令
    `npm i mysql
  • 配置 mysql 模块
// 1.导入 musql 模块
const mysql = require('mysql')

// 2.建立与 mysql 数据库连接
const db = mysql.createPool({
    host: '127.0.0.1',  // 数据库的 ip 地址
    user: 'root', // 登录数据库的账号
    password: 'root', // 登录数据库的密码
    database: 'web67' // 指定要操作哪个数据库
})

// 检测 mysql 模块是否能正常工作
db.query('select 1',(err,data)=>{
    if(err) return console.log(err.message);
    console.log(data);
})
3.使用 select 语句查询数据
// 1.导入mysql模块
const mysql = require('mysql')

// 与服务器建立连接
var db = mysql.createPool({
    host: '127.0.0.1',  // 数据库的 ip 地址
    user: 'root', // 登录数据库的账号
    password: 'root', // 登录数据库的密码
    database: 'web67' // 指定要操作哪个数据库
})

// 查询users中所有用户数据
db.query('select * from users',(err,data)=>{
    if(err) return console.log(err.message);
    console.log(data);
})
4.使用 insert into 语句插入数据
// 导入mysql模块
const mysql = require('mysql')
// 与数据库建立连接
var db = mysql.createPool({
    host: '127.0.0.1',  // 数据库的 ip 地址
    user: 'root', // 登录数据库的账号
    password: 'root', // 登录数据库的密码
    database: 'web67' // 指定要操作哪个数据库
})
// 要插入的数据
const data = {
    username:'kxu',
    password:'123'
}
// mysql语句
const dl = 'insert into users set?'

// 给数据库插入值
db.query(dl,data,(err,user)=>{
    if(err) return console.log(err.message);
    console.log(user);
    if(user.affectedRows ===1) return console.log('插入成功');
})
5.使用 update 语句更新数据
// 1.导入 mysql 模块
const mysql = require('mysql')
// 与数据库建立连接
var db = mysql.createPool({
    host: '127.0.0.1',  // 数据库的 ip 地址
    user: 'root', // 登录数据库的账号
    password: 'root', // 登录数据库的密码
    database: 'web67' // 指定要操作哪个数据库
})

// 要更新的数据
const data = {
    id: 4,
    username:'ffff',
    password:'123',
}
// mysql语句
const ql = 'update users set ? where id=?'

// 更新数据
db.query(ql,[data,data.id],(err,user)=>{
    console.log(user);
    if(err) return console.log(err.message);
    if (user.affectedRows===1) return console.log('更新成功');
})
6.使用 update 语句标记删除数据
// 导入模块
const mysql = require('mysql')

// 连接数据库
var db = mysql.createPool({
    host: '127.0.0.1',  // 数据库的 ip 地址
    user: 'root', // 登录数据库的账号
    password: 'root', // 登录数据库的密码
    database: 'web67' // 指定要操作哪个数据库
})

// mysql语句
const dl = 'update users set status=0 where id=?'

// 删除
db.query(dl,9,(err,data)=>{
    if(err) return console.log(err.message);
    if(data.affectedRows===1) return  console.log('删除成功');
})
7.通过 node 来操作 mysql
// 导入模块
const express = require('express')
const app = express()
const mysql = require('mysql')
// 连接数据库
const db = mysql.createPool({
    host: '127.0.0.1',  // 数据库的 ip 地址
    user: 'root', // 登录数据库的账号
    password: 'root', // 登录数据库的密码
    database: 'web67' // 指定要操作哪个数据库
})

// 把数据返回客户端
app.get('/user',(req,res)=>{
    db.query('select * from users', (err, data) => {
        if(err) return console.log(err.message);
        res.send({
            status:0,
            msg:'请求成功',
            data
        })
    })
})
// 解析post数据全局中间件
app.use(express.urlencoded({ extended: false }))
// 给users中添加用户名密码
app.post('/postuser',(req,res)=>{
    const body = req.body
    const dl = 'insert into users set ?'
    db.query(dl,body, (err, data) => {
        if (err) return console.log(err.message);
        res.send({
            status: 0,
            msg: '添加成功',
        })
    })
})
// 根据id更新用户名
app.post('/duser',(req,res)=>{
    const body = req.body
    const dl = 'update users set ? where id=?'
    db.query(dl,[body,body.id], (err, data) => {
        if (err) return console.log(err.message);
        if(data.affactedRows!==1) console.log('更新失败');
        res.send({
            status: 0,
            msg: '更新成功',
        })
    })
})
// 根据id删除
app.get('/iuser/:id',(req,res)=>{
    const user = req.params.id
    const dl = 'delete from users where id=?'
    db.query(dl, user, (err, data) => {
        if (err) return console.log(err.message);
        if (data.affactedRows !== 1) console.log('更新失败');
        res.send({
            status: 0,
            msg: '删除成功',
        })
    })
})

app.listen(3000,()=>{
    console.log('完成');
})

推荐阅读