Jacky's blog
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)

Jack Yang

编程; 随笔
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)
  • 服务器tutorial
  • spring

  • 数据库

    • database
    • mysql

      • MySQL 学习指南
      • mysql command
        • help
        • common
        • SELECT
          • key
        • DELETE
        • UPDATE
        • INSERT
      • mysql case
      • MySQL 高频面试问答
    • sqlite
    • PostgreSQL
    • redis 使用指南
    • mysql2pgsql
  • 运维

  • other

  • 《server》
  • 数据库
  • mysql
Jacky
2024-06-19
目录

mysql command

# help

  1. mysql --verbose --help
  2. 进入 mysql shell 中, 通过 help 查看帮助命令, For server side help, type help contents.
For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
   https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context.
query_attributes Sets string parameters (name1 value1 name2 value2 ...) for the next query to pick up.
ssl_session_data_print Serializes the current SSL session data to stdout or file

For server side help, type 'help contents'
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

# common

  • login
    • mysql -u [username] -p[password] -h [host] -P [port] [database]: 登陆. 注意 -p后面没有空格~
  • enter
    • mysql -h 127.0.0.1 -u root -p: 使用密码登录 MySQL 服务器
    • EXIT;: 退出 MySQL 数据库服务器
  • database
    • SHOW DATABASES;: 显示所有数据库
    • CREATE DATABASE <datebase>;: 创建数据库
    • DROP DATABASE <datebase>;: 删除数据库, 如果 database 名称包含了 -, 需要使用反引号 "`" 包围数据库名称
    • USE <datebase>;: 切换数据库
  • tables
    • SHOW TABLES;: 显示数据库中的所有表
    • CREATE TABLE <tablename>: create table 表名(列名 1: 列的类型 列名 2: 列的类型 列名 3: 列的类型 )
    • DESC <tablename>: 显示表结构
    • SHOW CREATE TABLE <tablename>;: 显示创建表的语法
    • alert
      • ALTER TABLE <tablename> ADD COLUMN 列名 类型;: 添加列
      • ALTER TABLE <tablename> MODIFY COLUMN name VARCHAR(255);
  • other
    • ROLLBACK: 执行回滚. 在开启非自动提交的时候有作用, 如果已经 commit 的事物, rollback 是没有效果的

# SELECT

SELECT * FROM <tablename>;
SELECT * FROM <tablename> WHERE id = 1;
SELECT name FROM author WHERE name LIKE '%曾%';
# 查询近10天的交易数据
SELECT * FROM t_daily WHERE trade_date >= CURDATE() - INTERVAL 10 DAY AND trade_date <= CURDATE();
#
SELECT * FROM t_daily WHERE trade_date BETWEEN '2024-01-02' AND '2024-01-04';

#   查询, 排序
SELECT * FROM your_table ORDER BY column_to_sort DESC LIMIT 10;
1
2
3
4
5
6
7
8
9
10

# key

  • LIKE
  • LIMIT
  • DESC
  • ASC: 升序

# DELETE

  • DELETE FROM <tablename> where name = 'squirrel';
  • TRUNCATE TABLE <tablename>; TRUNCATE 是一个 DDL 操作,比 DELETE 更高效,因为它不记录每一行的删除操作
  • 删除数据库内数据-保留表结构

# UPDATE

UPDATE <tablename> SET name = 'squirrel' where owner = 'Diane';
1

# INSERT

INSERT INTO `test`.`orm_user`(`name`,`password`,`salt`,`email`,`phone_number`) VALUES ('{name}', '{password}', '{salt}', '{email}', '{phone_number}');

# key 冲突
ON DUPLICATE KEY UPDATE:
  INSERT INTO t_daily (id, ts_code, trade_date, open, high, low, close, pre_close, changed, pct_chg, vol, amount) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE ts_code = VALUES(ts_code), trade_date = VALUES(trade_date), open = VALUES(open), high = VALUES(high), low = VALUES(low), close = VALUES(close), pre_close = VALUES(pre_close), changed = VALUES(changed), pct_chg = VALUES(pct_chg), vol = VALUES(vol), amount = VALUES(amount)
1
2
3
4
5
#tool
上次更新: 2025/10/08, 16:24:59
MySQL 学习指南
mysql case

← MySQL 学习指南 mysql case→

最近更新
01
npx 使用指南
10-12
02
cursor
09-28
03
inspect
07-20
更多文章>
Theme by Vdoing | Copyright © 2019-2025 Jacky | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式