数据库

概述

数据库是“按照数据结构来组织、存储和管理数据的仓库”。是一个长期存储在计算机内的、有组织的、可共享的、统一管理的大量数据的集合。

关系型数据库(SQL)

  • mysql(小型数据库,中小型公司使用)
  • oracle(大型数据库 安全性较高,数据较多时使用)
  • sql server(配合.net一起使用,多存在于老项目)
  • db2(erp项目使用 二次开发)
  • sqlit(物联网)

非关系型数据库(NOSQL)

  • redis
  • MongoDb

规范

关系型数据库遵从sql1999协议,内部采用SQL语句来进行相关操作。非关系型数据库采用指令华操作。

mysql

DDL语句

创建数据库
1
create database;
查看数据库
1
show databases;
删除数据库
1
drop database 数据库名;
使用数据库
1
use 数据库名;

创建表
1
create table 表名(字段名 类型 约束,字段名 类型 约束...)
查看表
1
show tables;
修改表
1
alter table 表名 rename 新表名;
删除表
1
drop table 表名;

DML语句(数据库模型语句)

1
insert into t_user (id,username,password)VALUE(100,'hello1','10086111');
1
delete from t_user where id=100;
1
UPDATE t_user SET password='abc' where id=2;
1
select * from t_user;

node mysql模块

导入mysql模块

1
2
// 导入mysql模块
const mysql = require('mysql')

创建连接对象

1
2
3
4
5
6
7
8
// 创建连接对象
let connection = mysql.createConnection({
host:'localhost',
port:3306,
user:'root',
password:'123456',
database:'db_student'
})

建立链接

1
2
3
4
5
// 建立链接
connection.connect((err)=>{
if(err) throw new Error(err)
console.log('mysql连接成功!');
})

查询

1
2
3
4
5
6
7
// 查询
connection.query('select * from t_user;',(err,res)=>{
if(err) throw new Error(err)
console.log(res);
//查询后关闭连接
connection.destroy()
})

关于查询的更多示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
let username = 'zs'
let password = '123456'
// 使用query方法执行增删改查
// 第一个参数为执行的SQL语句,第二个为占位符(防止sql注入),第三个参数为回调函数
// 增
connection.query('insert into t_user values(null,?,?)',[username,password],(err,res)=>{
if(err) throw new Error(err)
// 增删改返回的都是OKPacket对象
console.log(res);
})

// 删
connection.query('delete from t_user where id=?',101,(err,res)=>{
if(err) throw new Error(err)
// 增删改返回的都是OKPacket对象
console.log(res);
})

// 改
connection.query('update t_user set username=? where id=?',['李四',100],(err,res)=>{
if(err) throw new Error(err)
// 增删改返回的都是OKPacket对象
console.log(res);
})

//查
// 根据id查询
connection.query('select * from t_user where id=?',1,(err,res)=>{
if(err) throw new Error(err)
console.log(res);
})

// 统计查询
connection.query('select count(*) as 用户累计 from t_user',(err,res)=>{
if(err) throw new Error(err)
console.log(res);
})


// 查询所有用户
connection.query('select * from t_user;',(err,res)=>{
if(err) throw new Error(err)
console.log(res);
connection.destroy()
console.log('mysql连接已销毁!');
})

通过连接池操作 mysql.createPool()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 导入
const mysql = require('mysql')
// 创建连接池
let pool = mysql.createPool({
host:'localhost',
port:3306,
user:'root',
password:'123456',
database:'db_student'
})
//
pool.getConnection((err,connection)=>{
if(err) throw new Error(err)
connection.query('select * from t_user',(err,result)=>{
if(err) throw new Error(err)
console.log(result);
connection.destroy()
})
})

封装一个dbUtil.js模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 封装的一个操作对应的数据库工具类
// 导入mysql模块
const mysql = require('mysql')
// 导入fs模块
const fs = require('fs')
// 导入path模块
const { join } = require('path')

// 通过fs模块读取config.json的配置内容
const config = JSON.parse(fs.readFileSync(join(__dirname, '../config.json')))

// 创建连接池
let pool = mysql.createPool(config)

// 封装一个执行sql语句的方法
const query = function (sql, params = null) {
return new Promise((resolve, reject) => {
// 获取连接池操作
pool.getConnection((err, connection) => {
if (err) reject(err)
connection.query(sql, params, (err, result) => {
if (err) reject(err)
resolve(result)
connection.destroy()
})
})
})
}

module.exports = query

config.js 数据库配置文件

1
2
3
4
5
6
7
{
"host": "localhost",
"port": 3306,
"user": "root",
"password": "123456",
"database": "db_student"
}

MongoDB

概述:

MongoDB是一个key-value数据库,存储的数据格式为BSON,属于内存数据库,数据存储在内存上。

mongodb模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const MongoClient = require('mongodb').MongoClient
let clint = new MongoClient('mongodb://127.0.0.1:27017/')
clint.connect().then((connection) => {
// console.log(connection);
console.log('连接成功!');
let db = connection.db('user')//没有数据库会自动新建
// 创建数据表 获取数据表,如果没有就会创建
let tb = db.collection('t_user')
// 数据表操作
// 插入一条数据
// tb.insertOne({
// // _id 主键 自动生成
// username: 'tom11',
// password: '123456'
// }).then((res)=>{
// console.log(res);
// })
// // 插入多条数据
// tb.insertMany([
// {
// // _id 主键 自动生成
// username: 'tom0',
// password: '123456'
// },
// {
// // _id 主键 自动生成
// username: 'tom1',
// password: '123456'
// },
// {
// // _id 主键 自动生成
// username: 'tom2',
// password: '123456'
// },
// {
// // _id 主键 自动生成
// username: 'tom3',
// password: '123456'
// },
// ]).then((res)=>{
// console.log(res);
// })

// 查询所有
// tb.find().toArray().then((res)=>{
// console.log(res);
// });

// 查询用户名为tom的
// tb.find({
// username: 'tom3'
// }).toArray().then((res) => {
// console.log(res);
// });

// 查询一个
// tb.findOne({
// username:'tom3'
// }).then((res)=>{
// console.log(res);
// })

// 删除
// tb.deleteOne({
// username: 'tom3'
// }).then((res)=>{
// console.log(res);
// })


// connection.close()
})

mongoose模块