题记部分

一、连接MySQL服务器

1、常规连接方式

# 连接本地mysql服务器
mysql -u 用户名 -p
# 连接到指定mysql服务器,回车执行该命令之后需要输入密码
mysql -h 主机名或IP地址 -P 端口号 -u 用户名 -p
# 连接到当前mysql服务器,回车执行该命令不需要另外输入密码
mysql -h$(hostname -i) -pharley -uroot -P3306

2、使用套接字

mysql --socket=/path/to/your/mysql.sock -u 用户名 -p

二、DDL

2.4、update

(1)修改字段的部分内容

有一张表harley.jdbc_config,表中有字段url,值的格式大致为jdbc:mysql://hostname:port/dbName?xxa=a&xxb=b&xxc=c的值。

现有如下需求:该表中hostname:port相同的记录有多条,但是hostname和port均发生变更,需要修改库中所有涉及到的记录。

关键SQL如下

update harley.jdbc_config
set url=concat(
substring_index(url, "://", 1),
"://",
"newHostname:newPort",
"/",
substring_index(substring_index(url, '://', -1), '/', -1))
where condition = XXX;

三、DML

DATABASE

-- 查询数据库
show databases;
-- 创建数据库
create database if not exists dbName;
-- 切换数据库
use dbName;
-- 删除数据库
drop database dbName;
drop database if exists dbName;
-- 查看当前使用的数据库
select database();

TABLE

-- 创建表
create table if not exists 表名(
字段名1 数据类型1,
字段名2 数据类型2,
...
字段名n 数据类型n
); -- 添加数据
insert into 表名(字段名1,字段名2,...) values (值1,值2,...);
insert into 表名 values (值1,值2,...);
insert into 表名(字段名1,字段名2,...) values (值1,值2,...),(值1,值2,...),(值1,值2,...)...;
insert into 表名 values (值1,值2,...),(值1,值2,...),(值1,值2,...)...; -- 修改数据(不加条件,则所有数据都修改)
update 表名 set 字段名1=值1,字段名2=值2,...[where 条件]; -- 修改表
alter table 表名 rename to 新表名; -- 重命名
alter table 表名 add 字段名 数据类型; -- 添加字段
alter table 表名 modify 字段名 新数据类型; -- 修改字段数据类型
alter table 表名 change 字段名 新字段名 新字段类型; -- 修改字段名以及字段的数据类型
alter table 表名 drop 字段名; -- 删除字段 -- 清空数据
truncate table 表名; -- 修改自增序列
alter table 表名 auto_increment = 1; -- 删除数据(不加条件,则所有数据都删除)
delete from 表名 [where 条件]; -- 删除表
drop table if exists 表名;

将version字段放到email字段后面

alter table user modify version int default 1 null comment '乐观锁' after email;

INDEX

-- 使用create index语句创建索引
create index 索引名称 on 表名(列名);
-- 在创建表的时候指定普通索引
create table table_index (
id int primary key auto_increment,
name varchar(20),
index index_name(name)
);
-- 使用alter table语句
alter table table_name add index index_name(column_name);

其他

-- 查看数据库harley_test的总数据大小(单位MB)
select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES where table_schema='harley_test'; -- 查看MySQL中所有表的记录数、数据容量、索引容量
select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
group by table_schema
order by sum(data_length) desc, sum(index_length) desc; -- 查看所有数据库及表的记录数、数据容量、索引容量
select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
order by data_length desc, index_length desc; -- 查看某个数据库所有表的记录数、数据容量、索引容量。
select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
where table_schema='harley_test'
order by data_length desc, index_length desc;

Q&A

Q1 | 查看当前是服务端还是客户端

Q2 | 查看系统时间&时区

Q3 | mysql的某用户被锁住,如何解锁?

Q4 | 如何根据表名查询该表所属的数据库

Q5 | mysqldump的使用

Q1 | 查看当前是服务端还是客户端

show variables like '%read%only%';

如果read_only为true,即为只读权限,客户端是只读权限。

Q2 | 查看系统时间&时区

-- 查看系统时间
select now();

-- 查看时区
show variables like '%time_zone%';

Q3 | mysql的某用户被锁住,如何解锁?

-- 使用root权限的用户
alter user 'username'@'localhost' ACCOUNT UNLOCK;

username是要解锁的用户的用户名,localhost 也可以改为 %

如果要查看用户是否被锁定,执行以下sql

select user,host,account_locked from mysql.user where user='username' and host='localhost';

Q4 | 如何根据表名查询该表所属的数据库

select table_schema from information_schema.TABLES where table_name = '表名';

Q5 | mysqldump的使用

mysqldump [options] database_name [table_name ...] > dump.sql
mysqldump -uroot -p test > dump.sql -- 备份全库数据
mysqldump -u用户名 -p密码 --all-databases > /保存路径/文件名.sql
mysqldump -u用户名 -p密码 数据库名 > /保存路径/文件名.sql

— 要养成终身学习的习惯 —

MySQL - [02] 常用SQL的更多相关文章

  1. MySQL的常用SQL语句.md

    修改密码 这是常见的大家一般都要用的 首先     安装成功了打开cmd --> mysql -u root -p -->输入你的密码     修改mysql root用户密码    格式 ...

  2. mysql整理-常用sql语句

    一.常用sql show variables like 'character_set_client';#查询字符集 show databases;#列出所有的服务器上的数据库alter create ...

  3. MySQL 存储过程常用SQL语句收集

    1,select curdate() /*2016-10-08*/ 2,select date_sub(curdate(), INTERVAL 6 DAY) /*2016-10-02*/ 3,case ...

  4. mysql 【常用sql】

    修改过mysql数据库字段内容默认值为当前时间 --添加CreateTime 设置默认时间 CURRENT_TIMESTAMP ALTER TABLE `table_name` ADD COLUMN ...

  5. MySQL之常用SQL语句

    1) 分表之后统计数据的总量 SELECT (a0.total + a1.total + a2.total + a3.total + a4.total + a5.total + a6.total + ...

  6. Mysql 常用 SQL 语句集锦

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  7. Mysql 常用 SQL 语句集锦 转载(https://gold.xitu.io/post/584e7b298d6d81005456eb53)

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  8. 常用sql语句整理:mysql

    ## 常用sql语句整理:mysql1. 增- 增加一张表```CREATE TABLE `table_name`(  ...  )ENGINE=InnoDB DEFAULT CHARSET=utf8 ...

  9. MySQL用户管理、常用sql语句、MySQL数据库备份恢复

    1.MySQL用户管理 给远程登陆用户授权:grant all on *.* to 'user1'@'127.0.0.1' identified by '123456' (这里的127.0.0.1是指 ...

  10. 常用sql commands以及mysql问题解决日志

    mysql workbench常用命令快捷键 ctrl+T ->创建新的sql query tab ctrl+shift+enter->执行当前的sql命令 https://dev.mys ...

随机推荐

  1. AD使用插件生成交互式BOM

    AD使用插件生成交互式BOM 效果图镇楼: 下面来说一下怎么搞,过程其实也很简单,就加载一个脚本的事儿. 1.下载AD用交互式BOM插件 首先前往GitHub下载这位大佬开发的插件 地址:https: ...

  2. 理解 ASP.NET Core:Cookie 认证

    理解 ASP.NET Core:Cookie 认证 ASP.NET Core 内置提供了基于 Cookie 的认证支持.在使用 Cookie 验证的时候,相关的三要素: 认证模式名称:CookieAu ...

  3. 2024年1月Java项目开发指南7:增删改查与接口测试

    我们之前,是从Controller层写到Service层,然后mapper层. 接下来我们反过来,从mapper层写到Controller层 两种方式都可以,你喜欢就行,甚至你先写service层也可 ...

  4. 【前端】2024年 前端Base64编码的中文处理问题

    window.btoa() 遇到中文要出问题 localStorage.setItem("token",window.btoa(unescape(encodeURIComponen ...

  5. GitHub主页3D图表显示

    效果如图,项目来源于 yoshi389111/github-profile-3d-contrib: This GitHub Action creates a GitHub contribution c ...

  6. Go channel 原理

    作用 Go 语言的 channel 是一种 goroutine 之间的通信方式,它可以用来传递数据,也可以用来同步 goroutine 的执行. chan 是 goroutine 之间的通信桥梁,可以 ...

  7. Qt/C++音视频开发54-视频监控控件的极致设计

    一.前言 跌跌撞撞摸爬滚打一步步迭代完善到今天,这个视频监控控件的设计,在现阶段水平上个人认为是做的最棒的(稍微自恋一下),理论上来说应该可以用5年不用推翻重写,推翻重写当然也是程序员爱干的事情,这个 ...

  8. Qt自定义控件大全文章导航

    文章 链接 Qt编写自定义控件1-汽车仪表盘 https://qtchina.blog.csdn.net/article/details/89407746 Qt编写自定义控件2-进度条标尺 https ...

  9. SuperMap Objects .NET知识库:SQL查询以及通配符

    1     SQL 语句的构建 在SuperMap组件产品中,有许多接口都用到了过滤条件,也就是标准 SQL 语句中的 WHERE 子句部分,比如各种涉及属性查询的接口.网络分析中弧段的过滤条件.拓扑 ...

  10. 跟着源码一起学:手把手教你用WebSocket打造Web端IM聊天

    本文作者芋艿,原题"芋道 Spring Boot WebSocket 入门",本次有修订和改动. 一.引言 WebSocket如今在Web端即时通讯技术应用里使用广泛,不仅用于传统 ...