MySQL - [02] 常用SQL
题记部分
一、连接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 | 查看当前是服务端还是客户端
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的更多相关文章
- MySQL的常用SQL语句.md
修改密码 这是常见的大家一般都要用的 首先 安装成功了打开cmd --> mysql -u root -p -->输入你的密码 修改mysql root用户密码 格式 ...
- mysql整理-常用sql语句
一.常用sql show variables like 'character_set_client';#查询字符集 show databases;#列出所有的服务器上的数据库alter create ...
- MySQL 存储过程常用SQL语句收集
1,select curdate() /*2016-10-08*/ 2,select date_sub(curdate(), INTERVAL 6 DAY) /*2016-10-02*/ 3,case ...
- mysql 【常用sql】
修改过mysql数据库字段内容默认值为当前时间 --添加CreateTime 设置默认时间 CURRENT_TIMESTAMP ALTER TABLE `table_name` ADD COLUMN ...
- MySQL之常用SQL语句
1) 分表之后统计数据的总量 SELECT (a0.total + a1.total + a2.total + a3.total + a4.total + a5.total + a6.total + ...
- Mysql 常用 SQL 语句集锦
Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...
- Mysql 常用 SQL 语句集锦 转载(https://gold.xitu.io/post/584e7b298d6d81005456eb53)
Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...
- 常用sql语句整理:mysql
## 常用sql语句整理:mysql1. 增- 增加一张表```CREATE TABLE `table_name`( ... )ENGINE=InnoDB DEFAULT CHARSET=utf8 ...
- MySQL用户管理、常用sql语句、MySQL数据库备份恢复
1.MySQL用户管理 给远程登陆用户授权:grant all on *.* to 'user1'@'127.0.0.1' identified by '123456' (这里的127.0.0.1是指 ...
- 常用sql commands以及mysql问题解决日志
mysql workbench常用命令快捷键 ctrl+T ->创建新的sql query tab ctrl+shift+enter->执行当前的sql命令 https://dev.mys ...
随机推荐
- git commit之后,如何撤销commit
git reset --soft HEAD^ 仅仅是撤回commit操作,您写的代码仍然保留. HEAD^的意思是上一个版本,也可以写成HEAD~1 如果你进行了2次commit,想都撤回,可以使用H ...
- QPushButton长度固定,不随文字变化
QPushButton不随text长度变化 设置SizePolicy中的水平策略 没有设置为 忽略
- cas5配置redis
POM文件加载redis依赖,重新maven clean package <dependency> <groupId>org.apereo.cas</groupId&g ...
- dockerfile实现tomcat以及java的war包自动部署
1. 下载jdk和tomcat wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.93/bin/apache-tomcat-8.5.93.tar.g ...
- R机器学习:朴素贝叶斯算法的理解与实操
最近又看了很多贝叶斯算法的一些文章,好多的文章对这个算法解释起来会放一大堆公式,对代数不好的人来说真的很头疼.本文尝试着用大白话写写这个算法,再做个例子,帮助大家理解和运用. Naive Bayes ...
- 工作中这样用MQ,很香!
前言 消息队列(MQ)是分布式系统中不可或缺的技术之一. 对很多小伙伴来说,刚接触MQ时,可能觉得它只是个"传话工具",但用着用着,你会发现它简直是系统的"润滑剂&quo ...
- K8S学习笔记之卸载K8S集群
阅读目录 0x00 概述 0x01 操作 0x00 概述 有时候需要卸载已安装在本机的K8S服务和服务,本文卸载的K8S面向使用kubeadm或者二进制方法安装的,不涉及使用rpm包安装的集群: 主 ...
- Mac netstat 查看端口报错 netstat: option requires an argument -- p 解决
netstat -anvp |grep 10001 查询端口的时候报错提示 意思是缺少协议. 解决方案在Mac上正确使用的方法是:即-f需要加上地址族,-p需要加上协议TCP或者UDP等 a)如果需要 ...
- java 实现N进制转M进制
1. 把10进制转成N进制:除N取余,逆序排列 这里逆序排列使用StringBuilder类的reverse()函数来实现. /** * 10进制整数转换为N进制整数. 10进制转换为N进制 ...
- 跟着 8.6k Star 的开源数据库,搞 RAG!
过去 9 年里,HelloGitHub 月刊累计收录了 3000 多个开源项目.然而,随着项目数量的增加,不少用户反馈:"搜索功能不好用,找不到想要的项目!" 这让我意识到,仅仅收 ...