MySQL 原生操作-速查

MySQL的详细用法,内容包括:SQL语法,SQL数据类型,常用的SQL语句,复杂查询包括,连接查询,子查询,联合查询;无论在什么框架中使用库,但实际上是原生操作才是根本。

一,常规操作

mysql -h 123.123.123.1 -P 端口 -u用户 -p密码     
# 连接数据库

show databases;     	 						
# 显示所有库

use 库名;			  							   
# 进入某个库

show tables; 			 						 
# 显示所有表

set password for 用户名@localhost = password('新密码');
# 修改用户密码 方式一

mysqladmin -u用户名 -p旧密码 password 新密码
# 修改用户密码 方式二

# 忘记密码?
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

# 在 [mysqld] 下添加 skip-grant-tables

[mysqld]

skip-grant-tables

免密码登录后 mysql -u root

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123'; 
# 修改用户密码 方式三

update mysql.user set authentication_string=PASSWORD('123456'),plugin='mysql_native_password',host='%' where user='root';
# 最新方式


# 远程工具连不上本地mysql?
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

# 下面这行注释
bind-address            = 127.0.0.1

二,表操作(创建表,删除表,修改表,查看表)

describe 表名;
# 显示表结构(简写 desc 表名)

show create table 表名;
# 显示表结构语句

alter table old rename new;
# 修改表名

CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '测试表',
  `createtime` datetime DEFAULT NULL COMMENT '时间',
  `title` varchar(10) CHARACTER SET latin1 DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
# 创建表(字段可自定义);

DROP TABLE IF EXISTS `表名`;
# 删除表

ALTER TABLE 表名 DROP 字段;
# 删除字段

primary key # 定义主键
not null # 非空
unique # 唯一
default # 默认值
auto_increment # 自动


# 函数
rand() # 随机数
concat('AAA','BBB') # 连接字符串
trim(str) # 去除空格


month('2019-09-28')
# 月份

QUARTER('2016-04-28')
# 季度

DAYOFWEEK('2016-04-28') 
# 1是星期日

#聚合函数
avg(col) # 平均值
count(*) # 记录数
min(col) max(col) # 最小 最大值
sum(col) # 求和

SET NAMES utf8 # 设置编码

三,增,删,改,查(CURD)

insert into Table(id,name) values (null,'Li') ; 
# 插入数据

insert into Table(id,name) values (null,'Li'),(null,"Wang");
# 插入多行数据

insert into Table values (null,"Li",null,"数据"); 
# 插入所有列

insert into Table(id,name) select id,name from T2;
# 查询插入
insert ignore into Table(id,name) values (null,'Li') ; 
# 唯一索引插入 有效防止重复数
delete from Table where id =1;
# 删除

delete from Table where id in(1,2,3);
# 删除多条

truncate  table  test;
# 清空表
update Table set name='Li' where id=1; 
# 更新

update Table left join T1 on T.id=T1.id set u='1',u1='2' where id=1;
# 多表更新

update Table set user=replace(user,'a','a1') 
# 替换表字段
select * from 表名

select * from 表名 where id=1
# 条件

select distinct id from 表名
# 结果字段不重复

select  * from 表名 order by id;
select  * from 表名 order by id desc ,time asc;
# 排序,默认升序asc,降序desc


select  * from 表名 group by sex;
select  * from 表名 group by sex having;
# 分组查询,having 限定条件

select  * from 表名 limit 4 
# 返回4条

select  * from 表名 4,3   
# 返回3条,从第5条记录开始

select a,b,c from A inner join B on A.id = B.id;
select a,b,c from A,B where A.id=B.id;
# 内连接两个功能相同

select * from A left join B on   A.id=B.id;
select * from A right join B on   A.id=B.id;
# 左连接,右连接

select id from Table where id2 in(select id3 from Table2);
# 嵌套查询

select * from A union all select * from B;
select * from A union select * from B; 去重复
# 合并结果集

select id as ID from A as a;
# 表或字段起别名

sql文件

mysqldump -u用户名 -p密码 数据库名 > 数据库名.sql
# 导出表结构和数据

mysqldump -u用户名 -p密码 -d 数据库名 > 数据库名.sql
# 只导出表结构


------------导入数据---------------

1. 方法一
mysql>use abc;
# 选择数据库

mysql>set names utf8;
# 设置数据库编码

mysql>source /home/abc/abc.sql;
导入数据(注意sql文件的路径)

2. 方法二
mysql -u用户名 -p密码 数据库名 < 数据库名.sql

其他

常用的查询:
select * from ims_rank where DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= date(createtime); 
# 七天之前数据 30天

select * from ims_rank where to_days(createtime) =to_days(now()); 
# 今天数据

select * from ims_rank where TO_DAYS( NOW( ) ) -TO_DAYS( createtime) <= 1 # 昨天今天数据

select * from ims_rank where DATE_FORMAT( createtime,'%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
# 本月

select * from ims_rank where PERIOD_DIFF( date_format(now( ) , '%Y%m' ) , date_format( createtime, '%Y%m' ) ) =1  
# 上月

select * from ims_rank where YEARWEEK(date_format(createtime,'%Y-%m-%d')) =YEARWEEK(now()); 
# 本周(第一天星期日)

select * from ims_rank where YEARWEEK(date_format(createtime,'%Y-%m-%d')) = YEARWEEK(now())-1; 
# 上周数据

您的支持是对我最大的鼓励!

发表于: 作者:吕倡
博主经历空降兵部队8年军旅生涯,退伍后到北京IT兄弟连学习软件编程开发,工作不到一年后IT进入兄弟会,一晃在IT行业混迹了5、6年的时间。。。从一名小白也进入到了IT管理者的位置。博客,是博主学习知识分享以及私人笔记(未发布的博客)留存的地方,方便随时观看。
我的站点 Github 新浪微博 Email联系我