mysql的使用
常见命令
```
show databases; 查看所有数据库
mysql 注释用 --
create databases 数据库名 --创建数据库
drop databases 数据库名 --删除数据库
use 数据库名 --使用数据库
show tables --展示表
-- auto_increment是否自增 primary key 添加主键 not null 不为空
create table 表名 (
id int not null auto_increment primary key,
name varchar(255) not null,
title varchar(255),
);
```
1. 增加数据的三种方式 insert into
-
(1) insert into 表名 (键1, 键2, 键3, ...) values (值1, 值2, 值3, ...)
insert into lyb (name, title, time, content) values ('刘备', '不错', '2019年08月03日', '啦啦'); insert into lyb (name, title) values ('关羽', '不错'); -
(2) insert into 表名 values (值1, 值2, 值3, ...) 必须和表格的结构一一对应, 缺一不可!!
insert into lyb values (null, '张飞', '大汉粗人', '2018年06月06日', '啦啦');
- (3) insert into 表名 set 键=值, 键=值, ...
insert into lyb set name='花花', title='草草';
2. 更新(修改) update
update 表名 set 键=值, 键=值 (全改)
update lyb set name='大鹏';
update 表名 set 键=值, 键=值 where 条件
update lyb set name='大鹏鹏' where id=3;
update lyb set name='曹操' where id>2;
3. delet from 表名
delete from lyb; 将整个表的所有的数据都清空了
delete from 表名 where 条件
delete from lyb where id=10;
delete from lyb where name='鹏鹏大';
delete from lyb where name='花花1';
select 字段名, 字段名 from 表名 where 条件
select id, name, title from lyb;
select * from 表名; 查询表的所有数据
select * from lyb;
查询 id 为 8 的用户
select * from lyb where id=8;
select * from lyb where name='鹏鹏';
组合查询
- (1) and 并且, 两个条件都要满足
select * from lyb where name='张飞' and title='1';
select * from lyb where age > 10 and age < 15;
- (2) or 或者, 两个条件满足其一即可
select * from lyb where name='张飞' or title='不错';
- (3) 模糊查询 搜索功能 like 像 % 表示占位符, 可以表示任意多个字符 搜索跟name的值, 鹏鹏相关的
select * from lyb where name like '%鹏鹏'; -- 获取以鹏鹏结尾的
select * from lyb where name like '鹏鹏%'; -- 获取以鹏鹏开头的
select * from lyb where name like '%鹏鹏%'; -- 找包含鹏鹏, 搜索
- (4) 指定多个值 in 批量删除
select * from lyb where id=15 or id=14 or id=17;
select * from lyb where id in (14, 15, 17);
delete from lyb where id in (14, 15, 17); -- 批量删除
- (5) 排序 order by
select * from lyb order by id; 升序排列 从小到大
select * from lyb order by price;
select * from lyb order by id desc; -- 降序排序
- (6) 分页 每页5条 limit 跳过几条, 选中几条
获取第一页 1-5
select * from lyb limit 0, 5;
获取第二页 6-10 跳过的条数=前面页一共有多少条 => 前面的页码 * 每页的条数
select * from lyb limit 5, 5;
select * from lyb order by id desc limit 5, 5;
- (7) 总条数
select count(*) from lyb;
select count(*) as total from lyb;
node 使用mysql
node 使用mysql
1.首先安装mysql
npm install mysql
2.基本使用
// 导入第三方包
const mysql = require('mysql')
// 创建连接
var connection = mysql.createConnection({
// 本地
host: 'localhost',
user: 'root',
password: 'root',
// 数据库名称
database: 'mydb',
port: 3306
})
// 连接数据库
connection.connect()
// 执行sql语句
connection.query('select * from user where id = 8', (err, result) => {
if (err) return console.log('查询失败', err)
// result返回的是数组, 数组中是一个对象
console.log(result)
})
// 关闭连接
connection.end()
3.查询语句
var name = '关羽'
// 使用?表示占位,可以防止sql注入
connection.query(`select * from user where name=?`, name, (err, result) => {
if (err) return console.log('错误了', err)
console.log(result)
})
4.查询语句
connection.query(
'insert into user (name, age, gender, content) values (?, ?, ?, ?)',
['鹏哥', 18, '男', '哈哈哈哈'],
err => {
if (err) return console.log('错误', err)
console.log('添加成功了')
}
)
// 方式2
connection.query(
'insert into user set ?',
{
name: '鹏鹏123',
age: 30,
gender: '男',
content: '哈哈哈'
},
(err, result) => {
if (err) return console.log('错误', err)
console.log('添加成功了', result)
}
)
5.修改语句
connection.query(
'update user set ? where id = ?',
[
{
name: '鹏456',
age: 30,
gender: '男',
content: '哈哈哈'
},
10
],
(err, result) => {
if (err) return console.log('错误', err)
console.log('添加成功了', result)
}
)
6.删除语句
connection.query('delete from user where id = ?', [10], (err, result) => {
if (err) return console.log('失败', err)
console.log(result)
})