# 重启
service mysql restart
# 登录
mysql -u root -p
# 授权 root 用户允许所有人连接
grant all privileges on *.* to 'root'@'%' identified by '你的 mysql root 账户密码';
lower-case-table-names = 1
select @@validate_password_policy;
set global validate_password_policy=0;
select @@validate_password_length;
set global validate_password_length=1;
docker exec -it CONTAINER env LANG=C.UTF-8 bash
-- 登录
mysql -u root -p
-- 指定ip和端口登录
mysql -h 127.0.0.1 -u root -p -P 3306
-- 查看 MySQL 版本
select version();
-- 查看所有数据库:
show databases;
-- 应用某个数据库:
use databaseName;
-- 查看所有数据表:
show tables;
-- 查看数据表结构:
describe tableName;
-- 创建数据库:
create database databaseName;
-- 创建数据表:
create table tableName;
-- 删除数据库:
drop database databaseName;
-- 删除数据表:
drop table tableName;
-- 导出建表语句
show create table tableName;
-- 查看编码方式
show variables like 'char%';
show variables like 'character%';
-- 设置编码
SET NAMES 'utf8';
-- 它相当于下面的三句指令:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
-- 查询某一列为空或不为空的记录
select * from pay_order where ip IS NULL;
select * from pay_order where ip IS NOT NULL;
-- 求差集
select distinct ip from pay_order where ip not in (select ip from pay_client);
-- 插入列
alter table clients
add column `reception_mode` tinyint(2) NOT NULL DEFAULT 0 COMMENT '接待模式(0 轮询,1 平均)'
after `auto_reception`;
-- 删除列
alter table clients drop column `reception_mode`;
-- 修改字段长度
alter table `iptables` modify column `white_ip` varchar(1024);
-- 显示正在运行的线程
show processlist;
-- 查看时区
show variables like '%time_zone%';
SELECT AVG(score) average FROM students WHERE gender = 'M';
SELECT class_id, COUNT(*) num FROM students GROUP BY class_id;
SELECT s.id, s.name, s.class_id, c.name class_name, s.gender, s.score
FROM students s
INNER JOIN classes c
ON s.class_id = c.id;
<join_type> ::=
[ { INNER | { { LEFT | RIGHT | FULL } [ OUTER ] } } [ <join_hint> ] ]
JOIN
REPLACE INTO students (id, class_id, name, gender, score) VALUES (1, 1, '小明', 'F', 99);
INSERT INTO students (id, class_id, name, gender, score) VALUES (1, 1, '小明', 'F', 99) ON DUPLICATE KEY UPDATE name='小明', gender='F', score=99;
INSERT IGNORE INTO students (id, class_id, name, gender, score) VALUES (1, 1, '小明', 'F', 99);
-- 对 class_id=1 的记录进行快照,并存储为新表 students_of_class1:
CREATE TABLE students_of_class1 SELECT * FROM students WHERE class_id=1;
CREATE TABLE statistics (
id BIGINT NOT NULL AUTO_INCREMENT,
class_id BIGINT NOT NULL,
average DOUBLE NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO statistics (class_id, average) SELECT class_id, AVG(score) FROM students GROUP BY class_id;