search
HomeWeb Front-endJS TutorialLet's talk about how node operates the MySQL database (add, delete, modify, check)

How does node operate the MySQL database? The following article will take you through the methods of adding, deleting, modifying and querying the MySQL database in the node project. I hope it will be helpful to you!

Let's talk about how node operates the MySQL database (add, delete, modify, check)

Download and install mysql, check whether the installation is successful


Lets talk about how node operates the MySQL database (add, delete, modify, check)

net start mysql
  • Start mysql
  • You can right-click My Computer on the desktop and enter Computer Management to check whether mysql has been successfully run [Related tutorial recommendations: nodejs video tutorial]

Lets talk about how node operates the MySQL database (add, delete, modify, check)

Download and install navicat


Function: Provide us with the function of connecting and operating mysql database

Download

##www.navicat.com.cn/products#na…

Installation

Double-click, all the way to next

Use

to find the application, click to start

Lets talk about how node operates the MySQL database (add, delete, modify, check)

If the connection test passes, continue When you get down, you can click the OK button to officially connect to mysql.

Lets talk about how node operates the MySQL database (add, delete, modify, check)

The effect after connecting is as follows:

Lets talk about how node operates the MySQL database (add, delete, modify, check)

Database Introduction


What is a database

English:

database The warehouse that saves and manages data is the database.

What is data? Files, pictures, videos, orders, usernames, passwords and more.

These data need a special place to save and manage.

Before we learned database technology, the data we used were all saved in the file system (db.json). We need a

specialized software to manage our data, which is a database.

Lets talk about how node operates the MySQL database (add, delete, modify, check)

Two camps-database classification

    ##Relational database, representative product:
    • MySQL
    • Oracle
    • Sql server
    • DB2
  • non Relational database
    • redis
    • Key-value storage databaseHBaise column storage database
    • mongodb
    • Document-oriented databaseneo4j graph database
    • Elasticsearch search engine storage
  • Reference: Database usage ranking
db-engines.com/en/ranking

Lets talk about how node operates the MySQL database (add, delete, modify, check)

Understanding relational database

In a relational database, there are three-level relationships:

Database
  • Data table
  • Field
  • Analogy
excel

:

Lets talk about how node operates the MySQL database (add, delete, modify, check)

Each column is a type of data---
    Field
  • Each row represents a piece of data---
  • Record
Database##Databaseexcel fileData tableA certain sheet in the excel fileTable structure: FieldIn the sheet Header: Column

MySQL简介

Lets talk about how node operates the MySQL database (add, delete, modify, check)MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品 。MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。

  • 体积小、速度快、总体拥有成本低,一般中小型网站的开发都选择 MySQL 作为网站数据库。
  • 搭配 PHPApache 可组成良好的开发环境。

3p技术:php,asp,jsp

操作数据库-用navicat


新建数据库

Lets talk about how node operates the MySQL database (add, delete, modify, check)

在弹出的窗口中填写数据库名即可。

Lets talk about how node operates the MySQL database (add, delete, modify, check)点击确定之后,会在左侧的数据库列中看到上面创建成功的数据库。

新建数据表

Lets talk about how node operates the MySQL database (add, delete, modify, check)

点击 "新建表", 然后在开始设置字段

Lets talk about how node operates the MySQL database (add, delete, modify, check)

主键: 作用是区别一条数据和其它数据。(它相当于人的身份证号)

设置字段完成之后,点击上图左上角所示的保存按钮,就会进一步弹出对话框,让填写表的名字。

我们填入user

Lets talk about how node operates the MySQL database (add, delete, modify, check)

编辑字段

Lets talk about how node operates the MySQL database (add, delete, modify, check)

添加数据

Lets talk about how node operates the MySQL database (add, delete, modify, check)

一条内容输入完成后,按下tab,会自动进入下一条记录的输入

学习使用SQL语句


结构化查询语言(Structured Query Language)简称SQL,用来操作关系型数据库:

  • 是一种数据库查询和程序设计语言,用来存取数据以及查询、更新、和管理关系型数据库。
  • .sql是数据库脚本文件的扩展名。

最常用的用于数据操作的sql语句有四类,分别对应对数据的四种操作:

  • 增(create)(例如:用户注册)
  • 删(delete) (例如:删除订单)
  • 改(update) (例如:修改密码)
  • 查(select , read) (例如:信息搜索)

在navicat中运行sql - 添加数据


打开sql编辑区

Lets talk about how node operates the MySQL database (add, delete, modify, check)

然后:

Lets talk about how node operates the MySQL database (add, delete, modify, check)

学习 sql中的insert into 命令

格式:

 insert into 表名(字段名1,字段名2,....)  values (值1,值2,....)

注意:

  • 字段的顺序要和值的顺序是完全匹配的

  • 字段列表可以不与真实数据表中的字段完全相等,

    • 可以省略一些不必要的字段
    • 顺序与不需要与定义表时的顺序一致
  • 如果是字符串类型的字段,其值要加"",如果是数值类型的字符串,其值不需要加“”

示例:

insert into stu (sex, weight, name) values ('男', 60, '庞凯')

sql-delete语句-删除数据


格式

 delete  from 表名  where 删除条件复制代码

注意:不指定条件将删除所有数据

示例

-- 删除id为14的同学
delete from stu where id=14

-- 删除的时候,不加条件,将删除stu表中的全部记录
delete from stu

sql-update语句-修改数据


格式

update 表名 set 字段1=值1, 字段2=值2,...  where 修改条件

注意:

- 要修改的值使用键值对来表示 
- 多个字段用,分隔
- 不指定条件,将修改当前表中全部的记录

示例

-- 修改id为1的同学的年龄为53
update stu set age=53 where id = 1

-- 修改id为1的同学的年龄为35,身高为160
update stu set age=35,height=160 where id = 1

-- 如果修改的时候,不加条件,则会修改全部的数据
update stu set weight = 60

sql-select-语句-数据查询


作用

把数据从数据库查出来

格式

SELECT  字段名1, 字段名2, .....  FROM 表名  WHERE <条件表达式>

示例

# 查询部分字段SELECT id,name,age FROM stu
# 查询所有字段SELECT * FROM stu
# 带条件的查询SELECT * FROM 表名 WHERE 条件1 and 条件2

where子句


select field1, field2... from 表名 查询表中的所有数据

where 可以使用条件来筛选查询出的结果

-- 查询所有的学生
select * from stu

-- 查询所有学生的id,name,height
select id,name,height from stu

-- 带条件的查询
select * from stu where 条件

-- 查询所有的男同学
select * from stu where sex=&#39;男&#39;

-- 查询id为2的男同学
select * from stu where id=2

-- 查询年龄大于50的同学
select * from stu where age > 50

-- 查询年龄大于50岁的男同学
select * from stu where age>50 and sex=&#39;男&#39;

-- 查询年龄在30~60之间的同学,包括30和60
select * from stu where age>=30 and age<=60
select * from stu where age between 30 and 60

node.js操作mysql


通过mysql这个包来操作mysql数据库。

安装包

mysql模块是一个第三方模块,专门用来操作MySQL数据库。

# 安装
npm i mysql

参考:www.npmjs.com/package/mys…

使用步骤

要想用这个包连接数据库,首先要确保在电脑有mysql(phpstudy 还要启动mysql服务)

一共需要4个步骤:

  • 加载 MySQL 模块

  • 创建 MySQL 连接对象

  • 连接 MySQL 服务器

  • 执行SQL语句

var mysql = require(&#39;mysql&#39;);

var connection = mysql.createConnection({
  host     : &#39;localhost&#39;,   // 你要连接的数据库服务器的地址
  port     : 3306,// 端口号
  user     : &#39;root&#39;,        // 连接数据库服务器需要的用户名
  password : &#39;root&#39;,        // 连接数据库服务器需要的密码
  database : &#39;gz61&#39;      //你要连接的数据库的名字
});

connection.connect((err) => {
  // 如果有错误对象,表示连接失败
  if (err) return console.log(&#39;数据库连接失败&#39;)
  // 没有错误对象提示连接成功
  console.log(&#39;mysql数据库连接成功&#39;)
});

参考地址: www.npmjs.com/package/mys…

node操作mysql-查询操作


执行查询类型的SQL语句,查询结果(result)是一个数组,每个单元是对象,对象的属性是数据表的字段名。

// 1. 加载mysql
const mysql = require(&#39;mysql&#39;);
// 2. 创建连接对象
const conn = mysql.createConnection({
    // 对象的属性名字不能改变
    host: &#39;localhost&#39;,
    port: 3306,
    user: &#39;root&#39;,
    password: &#39;root&#39;,
    database: &#39;gz61&#39;
});
// 3. 连接到MySQL服务器
connection.connect((err) => {
  // 如果有错误对象,表示连接失败
  if (err) return console.log(&#39;数据库连接失败&#39;)
  // 没有错误对象提示连接成功
  console.log(&#39;mysql数据库连接成功&#39;)
});
// 4. 执行SQL语句
let sql = &#39;select id,name,age from stu&#39;;
connection.query(sql, (err, result, fields) => {
    if (err) throw err; // throw err 相当于 return console.log(err);
    console.log(result); // result就是查询结果
});

node操作mysql-添加操作


执行添加类型的SQL语句,查询结果(result)是一个对象,该对象中有两个属性要关注:

  • affectedRows: 受影响行数
  • insertID: 查询数据的主键值
// 1. 加载mysql
const mysql = require(&#39;mysql&#39;);
// 2. 创建连接对象
const conn = mysql.createConnection({
    // 对象的属性名字不能改变
    host: &#39;localhost&#39;,
    port: 3306,
    user: &#39;root&#39;,
    password: &#39;root&#39;,
    database: &#39;gz61&#39;
});
// 3. 连接到MySQL服务器
connection.connect((err) => {
  // 如果有错误对象,表示连接失败
  if (err) return console.log(&#39;数据库连接失败&#39;)
  // 没有错误对象提示连接成功
  console.log(&#39;mysql数据库连接成功&#39;)
});

let sql = &#39;insert into users (name,password) values("小王","snv")&#39;
connection.query(sql, (err, result) => {
    if (result.affectedRows > 0) {
        console.log(&#39;添加成功,新数据的id为:&#39; + result.insertId);
    } else {
        console.log(&#39;添加失败&#39;);
    }
});

node操作mysql-修改操作


执行修改类型的SQL语句,查询结果(result)是一个对象,该对象中有 affectedRows 属性,表示本次修改操作影响到的行数。

// 1. 加载mysql
const mysql = require(&#39;mysql&#39;);
// 2. 创建连接对象
const conn = mysql.createConnection({
    // 对象的属性名字不能改变
    host: &#39;localhost&#39;,
    port: 3306,
    user: &#39;root&#39;,
    password: &#39;root&#39;,
    database: &#39;gz61&#39;
});
// 3. 连接到MySQL服务器
connection.connect((err) => {
  // 如果有错误对象,表示连接失败
  if (err) return console.log(&#39;数据库连接失败&#39;)
  // 没有错误对象提示连接成功
  console.log(&#39;mysql数据库连接成功&#39;)
});
// 更新
// update stu set 字段=值,字段=值 where id=11
let sql = &#39;update users set password="123" where name="小王"&#39;;

conn.query(sql, (err, result) => {
    if (err) throw err;
    if (result.affectedRows > 0) {
        console.log(&#39;修改成功&#39;);
    } else {
        console.log(&#39;修改失败&#39;);
    }
})

node操作mysql-删除操作


执行删除类型的SQL语句,查询结果(result)是一个对象,该对象中有 affectedRows 属性

// 1. 加载mysql
const mysql = require(&#39;mysql&#39;);
// 2. 创建连接对象
const conn = mysql.createConnection({
    // 对象的属性名字不能改变
    host: &#39;localhost&#39;,
    port: 3306,
    user: &#39;root&#39;,
    password: &#39;root&#39;,
    database: &#39;gz61&#39;
});
// 3. 连接到MySQL服务器
connection.connect((err) => {
  // 如果有错误对象,表示连接失败
  if (err) return console.log(&#39;数据库连接失败&#39;)
  // 没有错误对象提示连接成功
  console.log(&#39;mysql数据库连接成功&#39;)
});
// 删除
let sql = &#39;delete from stu where id=1&#39;;

connection.query(sql,(err, result) => {
    if (err) throw err;
    if (result.affectedRows > 0) {
        console.log(&#39;删除成功&#39;);
    } else {
        console.log(&#39;删除失败&#39;);
    }
});

了解一下软删除


做删除 : delete from 表名 条件 会把数据直接从数据库中删除掉!

思路:

  • 不是真的删除,而是设置一个特殊的字段表示当前的状态:正常还是已经删除

Lets talk about how node operates the MySQL database (add, delete, modify, check)

# 目标:把id=16的软删除掉
update stu set isDelete=1 where id=16

模块化封装


分析上面几个单独的功能点,它们基本的语法格式是一致的,只是要执行的sql语句不同而已,所以,我们可以对它们进行一个简单的封装。然后再写测试文件对其进行测试。

涉及两个文件:

  • sql.js
  • sqltest.js

封装模块

模块名:sql.js

// 由于四项(insert,delete,update,select)操作只是sql语句不同

// 1. 加载mysql
const mysql = require(&#39;mysql&#39;);
// 2. 创建连接对象
const conn = mysql.createConnection({
    // 对象的属性名字不能改变
    host: &#39;localhost&#39;,
    port: 3306,
    user: &#39;root&#39;,
    password: &#39;root&#39;,
    database: &#39;gz61&#39;
});
// 3. 连接到MySQL服务器
connection.connect((err) => {
  // 如果有错误对象,表示连接失败
  if (err) return console.log(&#39;数据库连接失败&#39;)
  // 没有错误对象提示连接成功
  console.log(&#39;mysql数据库连接成功&#39;)
});

module.exports = connection

测试

sqltest.js

const conn = require(&#39;./sql&#39;);

conn.query(&#39;select * from users where username="小美1" and userpassword="666"&#39;, (err, data) => {
  console.log(err);
  console.log(data);
  if (data.length > 0) {
    console.log(&#39;用户名密码Ok&#39;);
  } else {
    console.log(&#39;用户名密码error&#39;);
  }
});

更多node相关知识,请访问:nodejs 教程

excel file

The above is the detailed content of Let's talk about how node operates the MySQL database (add, delete, modify, check). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
深入理解MySQL索引优化器工作原理深入理解MySQL索引优化器工作原理Nov 09, 2022 pm 02:05 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于索引优化器工作原理的相关内容,其中包括了MySQL Server的组成,MySQL优化器选择索引额原理以及SQL成本分析,最后通过 select 查询总结整个查询过程,下面一起来看一下,希望对大家有帮助。

sybase是什么数据库sybase是什么数据库Sep 22, 2021 am 11:39 AM

sybase是基于客户/服务器体系结构的数据库,是一个开放的、高性能的、可编程的数据库,可使用事件驱动的触发器、多线索化等来提高性能。

visual foxpro数据库文件是什么visual foxpro数据库文件是什么Jul 23, 2021 pm 04:53 PM

visual foxpro数据库文件是管理数据库对象的系统文件。在VFP中,用户数据是存放在“.DBF”表文件中;VFP的数据库文件(“.DBC”)中不存放用户数据,它只起将属于某一数据库的 数据库表与视图、连接、存储过程等关联起来的作用。

数据库系统的构成包括哪些数据库系统的构成包括哪些Jul 15, 2022 am 11:58 AM

数据库系统由4个部分构成:1、数据库,是指长期存储在计算机内的,有组织,可共享的数据的集合;2、硬件,是指构成计算机系统的各种物理设备,包括存储所需的外部设备;3、软件,包括操作系统、数据库管理系统及应用程序;4、人员,包括系统分析员和数据库设计人员、应用程序员(负责编写使用数据库的应用程序)、最终用户(利用接口或查询语言访问数据库)、数据库管理员(负责数据库的总体信息控制)。

microsoft sql server是什么软件microsoft sql server是什么软件Feb 28, 2023 pm 03:00 PM

microsoft sql server是Microsoft公司推出的关系型数据库管理系统,是一个全面的数据库平台,使用集成的商业智能(BI)工具提供了企业级的数据管理,具有使用方便可伸缩性好与相关软件集成程度高等优点。SQL Server数据库引擎为关系型数据和结构化数据提供了更安全可靠的存储功能,使用户可以构建和管理用于业务的高可用和高性能的数据应用程序。

数据库的什么是指数据的正确性和相容性数据库的什么是指数据的正确性和相容性Jul 04, 2022 pm 04:59 PM

数据库的“完整性”是指数据的正确性和相容性。完整性是指数据库中数据在逻辑上的一致性、正确性、有效性和相容性。完整性对于数据库系统的重要性:1、数据库完整性约束能够防止合法用户使用数据库时向数据库中添加不合语义的数据;2、合理的数据库完整性设计,能够同时兼顾数据库的完整性和系统的效能;3、完善的数据库完整性有助于尽早发现应用软件的错误。

go语言可以写数据库么go语言可以写数据库么Jan 06, 2023 am 10:35 AM

go语言可以写数据库。Go语言和其他语言不同的地方是,Go官方没有提供数据库驱动,而是编写了开发数据库驱动的标准接口,开发者可以根据定义的接口来开发相应的数据库驱动;这样做的好处在于,只要是按照标准接口开发的代码,以后迁移数据库时,不需要做任何修改,极大方便了后期的架构调整。

mysql查询慢的因素除了索引,还有什么?mysql查询慢的因素除了索引,还有什么?Jul 19, 2022 pm 08:22 PM

mysql查询为什么会慢,关于这个问题,在实际开发经常会遇到,而面试中,也是个高频题。遇到这种问题,我们一般也会想到是因为索引。那除开索引之外,还有哪些因素会导致数据库查询变慢呢?

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)