数据库
概述
数据库是“按照数据结构来组织、存储和管理数据的仓库”。是一个长期存储在计算机内的、有组织的、可共享的、统一管理的大量数据的集合。
关系型数据库(SQL)
- mysql(小型数据库,中小型公司使用)
- oracle(大型数据库 安全性较高,数据较多时使用)
- sql server(配合.net一起使用,多存在于老项目)
- db2(erp项目使用 二次开发)
- sqlit(物联网)
非关系型数据库(NOSQL)
规范
关系型数据库遵从sql1999协议,内部采用SQL语句来进行相关操作。非关系型数据库采用指令华操作。
mysql
DDL语句
创建数据库
查看数据库
删除数据库
使用数据库
创建表
1
| create table 表名(字段名 类型 约束,字段名 类型 约束...)
|
查看表
修改表
1
| alter table 表名 rename 新表名;
|
删除表
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;
|
查
node mysql模块
导入mysql模块
1 2
| 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'
connection.query('insert into t_user values(null,?,?)',[username,password],(err,res)=>{ if(err) throw new Error(err) console.log(res); })
connection.query('delete from t_user where id=?',101,(err,res)=>{ if(err) throw new Error(err) console.log(res); })
connection.query('update t_user set username=? where id=?',['李四',100],(err,res)=>{ if(err) throw new Error(err) console.log(res); })
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
|
const mysql = require('mysql')
const fs = require('fs')
const { join } = require('path')
const config = JSON.parse(fs.readFileSync(join(__dirname, '../config.json')))
let pool = mysql.createPool(config)
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('连接成功!'); let db = connection.db('user') let tb = db.collection('t_user')
})
|
mongoose模块