一: 库(database(mysql))的操作

1. 建库

  1. CREATE SCHEMA `node-blog`;
  2. // or
  3. CREATE DATABASE `node-blog`;

2. 查看库

  1. SHOW DATABASES;

3. 选择库

  1. -- 选择表
  2. USE `node-blog`;

二: 表的操作

1. 建表

  1. CREATE TABLE IF NOT EXISTS `blogs`(
  2. `id` INT UNSIGNED AUTO_INCREMENT,
  3. `title` VARCHAR(100) NOT NULL,
  4. `content` VARCHAR(200) NOT NULL,
  5. `author` VARCHAR(100),
  6. `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP comment '创建时间',
  7. `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '更新时间',
  8. PRIMARY KEY ( `id` )
  9. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  10. CREATE TABLE IF NOT EXISTS `users`(
  11. `id` INT UNSIGNED AUTO_INCREMENT,
  12. `username` VARCHAR(100) NOT NULL,
  13. `password` VARCHAR(100) NOT NULL,
  14. `phone` INT,
  15. `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP comment '创建时间',
  16. `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '更新时间',
  17. PRIMARY KEY ( `id` )
  18. )ENGINE=InnoDB DEFAULT CHARSET=utf8;

3. 单表操作

3.1 增

  1. INSERT INTO users(username, `password`, phone) VALUES ('lisi2', '12', '12345678888');

3.2 删

  1. DELETE FROM users WHERE username="zhangsan";

3.3 改

  1. UPDATE users SET password='newpassword' WHERE username='你好';

3.4 查

  1. -- 查询全部
  2. SELECT * FROM users;
  3. -- 展示固定的列
  4. SELECT id, username FROM users;
  5. -- 交集查询
  6. SELECT id, username FROM users WHERE username='zhangsan' and `password`='123';
  7. -- 并集查询
  8. SELECT id, username FROM users WHERE username='zhangsan' or `password`='123';
  9. -- 模糊查询
  10. SELECT * FROM users WHERE username LIKE '%zhan%';
  11. -- 查询结果排序
  12. SELECT * FROM users WHERE username LIKE '%zhan%' ORDER BY id;
  13. -- 倒序查询
  14. SELECT * FROM users WHERE username LIKE '%zhan%' ORDER BY id DESC;