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 ...
随机推荐
- Flutter 设置安卓启动页报错 java.lang.RuntimeException: Canvas: trying to draw too large(106,975,232 bytes) bitmap.
设置安卓启动页报错 首先设置安卓启动页 在android/app/src/main/AndroidManifest.xml中添加这一行 <meta-data android:name=" ...
- Envoy 官网,中文指南,Envoy 实现 .NET 架构网关
收集一些 Envoy 的资料 Envoy 实现 .NET 架构的网关系列 Envoy实现.NET架构的网关(一)静态配置与文件动态配置 Envoy实现.NET架构的网关(二)基于控制平面的动态配置 E ...
- 【单片机】初次实验:Keil51的使用
哔哩哔哩/CSDN/博客园:萌狼蓝天 延时器 delay(int count){ int i,j; for(i=0;i<count;i++){ for(j=0;j<1000;j++); } ...
- 【MyBatis】学习笔记10:添加功能获取自增的主键
[Mybatis]学习笔记01:连接数据库,实现增删改 [Mybatis]学习笔记02:实现简单的查 [MyBatis]学习笔记03:配置文件进一步解读(非常重要) [MyBatis]学习笔记04:配 ...
- Uninstall or delete MariaDB completely for re-installation
I am new to this forum so pse forgive me if I am asking a question which already has been answered. ...
- Win10正式专业版激活方法
首先,我们先查看一下Win10正式专业版系统的激活状态: 点击桌面左下角的"Windows"按钮,从打开的扩展面板中依次点击"设置"-"更新和安全 ...
- Qt编写地图综合应用17-地址经纬度互转
一.前言 地址和经纬度互相转换的功能也经常用到,比如上次的路线方案查询的功能,之前官网是提供了直接输入出发地点和目的地的中文汉字,就可以查询到最优的路线,后面只支持输入出发地点和目的地的经纬度坐标了, ...
- springboot的yml文件中如何配置redis?
springboot的yml文件中如何配置redis? 解决方法: spring: #redis配置 redis: database: 0 timeout: 0 # Redis服务器地址 host: ...
- Element库的Vue版本ElementUI的本地引入方法
最近刚接触ElementUI,发现官方介绍的使用方法中只有npm安装和CDN引入这两种方式,没有本地引入的方法. 因为我的学习环境有时候是断网状态的,所以自己研究了一下本地引入的方法,记录在此. 1. ...
- COCI 2024/2025 #3
T1 P11474 [COCI 2024/2025 #3] 公交车 / Autobus 愤怒,从红升橙足以说明其恶心,考场上调了半小时才过. 这道题的车能够开 \(24\) 小时,并且他能从前一天开到 ...