express数据库
# 数据库
数据库database - 数据表table - 数据行row - 字段field
SQL
: 结构化查询语言,专门用来访问和处理数据库的编程语言
# MYSQL
# 数据类型
int
整型varchar(len)
字符串tinyint(1)
布尔值
# 字段特殊标识
PK (Primary Key)
主键、唯一标识NN (Not Null)
值不允许为控UQ (Unique)
值唯一AI (Auto Increment)
值自动增长
# SQL基本操作
# 查询
注意: SQL语句对关键字的大小写不敏感
-- 查询表中所有列的数据
SELECT * FROM table_name
-- 查询表中指定列的数据
SELECT col_name FROM table_name
1
2
3
4
2
3
4
# 插入
INSERT INTO table_name (col1,col2,col3...) values ('cal1_value','cal2_value','cal2_value')
1
# 修改
UPDATE table_name SET col1 = new_value1,col2 = new_value2 WHERE id = 1
1
# 删除
DELETE FROM table_name WHERE id = 1
1
# WHERE子句
用于限定选择的标准
可用的运算符
操作符 | 描述 |
---|---|
= | |
<> | != | 不等于 |
> | |
< | |
>= | |
<= | |
BETWEEN | 在某个范围内 |
LIKE | 搜索某种模式 |
# AND和OR
可以在子句中使用 ,把多个条件结合起来
select * from users where status = 0 and id < 3
select * from users where status = 1 or username = 'zs'
1
2
2
# ORDER BY
子句对查询的结构集进行排序
- 默认是升序 (ASC)
- 降序(DESC 关键字)
select * from users ORDER BY status ASC;-- 升序
select * from user ORDER BY status DASC;-- 降序
1
2
2
多重排序
根据排序的不同字段进行多重排序
场景:先对status进行降序排序,然后按照username 进行升序排序
select * from user order by status desc, username asc
1
# COUNT(*)
用于查询结构的总条数
select count(*) from users where status = 0;
1
# AS
给查询的列其别名
select username as uname ,password as upwd from users
1
# express的mysql模块
# 连接数据库
const mysql = require('mysql')
const db = mysql.createPool({
host: '127.0.0.1',
user: 'root',
password: 'root',
database: 'study_demo01',
})
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 查询
let checkTable = 'select * from users'
db.query(checkTable, (err, results) => {
if (err) {
return console.log(err.message)
}
console.log(results)
})
1
2
3
4
5
6
7
2
3
4
5
6
7
# 插入
let user = { username: 'ioioio3', password: '2223333' }
let insertTable = 'insert into users (username,password) value (?,?)'
let easyInser = 'insert into users set ?'
db.query(insertTable, [user.username, user.password], (err, results) => {
if (err) {
return console.log(err.message)
}
console.log(results)
})
// 简化版
db.query(easyInser, user, (err, results) => {
if (err) {
return console.log(err.message)
}
console.log(results)
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 更新
const updateTable = 'update users set username=?,password=? where id=?'
const user = { id: 8, username: 'testxx2', password: '000000' }
db.query(
updateTable,
[user.username, user.password, user.id],
(err, results) => {
if (err) {
return console.log(err.message)
}
console.log(results)
}
)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
简化版
const easyUpdate = 'update users set ? where id = ?'
db.query(easyUpdate, [user, user.id], (err, results) => {
if (err) {
return console.log(err.message)
}
console.log(results)
})
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 删除
const deleteTable = 'delete from users where id = ?'
db.query(deleteTable, 5, (err, results) => {
if (err) {
return console.log(err.message)
}
console.log(results)
})
1
2
3
4
5
6
7
2
3
4
5
6
7
# 操作
const sqlStr = 'update users set status=? where id = ?'
db.query(sqlStr, [1,4], (err, results) => {
if (err) {
return console.log(err.message)
}
console.log(results)
})
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
编辑 (opens new window)
上次更新: 2023/02/07, 14:51:48