2019-07-18 MySql 命令

mysql官方测试库,点我

修改密码

mysqladmin -u root -p password 123

这条命令可以为用户设置密码,root位置为用户名,123 位置为密码,password不用改

查看连接线程

show processlist;

用户

create user hw@'%' identified by '123456'

  • hw 为用户名

  • % 可以访问的ip地址(% 代表所以ip地址)

  • 123456 为密码

select user, host from mysql.user; 可以查询用户创建成功没

alter user hw@'%' identified by '123456' 可以修改密码

drop user hw@'%'; 删除用户

权限

grant all on *.* to root@'%' identified by 'password'

  • all 代表所有权限,可以换成select

  • 第一个* 是数据库名称,可以改成允许访问的数据库名称

  • 第二个* 是数据库的表名称(*代表允许访问任意的表和数据库)

  • root代表远程登录使用的用户名,可以自定义

  • % 代表允许任意ip登录,如果你想指定特定的IP,替换掉就可以

  • password 是运程连接密码

  • with grant option命令可以加在后面,这只有root用户可以使用

show grants for root; 查看用户权限

revoke delete on *.* from root@'%' 回收delete权限

管理员忘记root用户密码

mysqld_safe --skip-grant-tables --skip-networking &

关闭连接层和tcp/ip连接

flush privileges;

alter user root@'%' identified by "123456";

DATABASE

create database hw CHARSET utf8mb4 collate utf8mb4_bin;

alter database hw charset utf8;

desc 表名 查看表的结构

create table test like stu; 创建一个和stu一样的表

表的修改

添加 alter table stu ADD qq varchar(20) NOT NULL UNIQUE comment "qq号";

指定位置添加 :

alter table stu ADD wechat varchar(20) NOT NULL UNIQUE AFTER sname COMMENT "微信";

alter table stu ADD num INT NOT null FIRST;

删除列 alter table stu DROP qq;

修改列的属性 alter table stu MODIFY sname varchar(12) NOT NULL;

修改列sgender为sg以及属性 alter table stu change sgender sg char(1) NOT NULL;

启动读文件

mysqld --help --verbose | grep my.cnf

可以查看读取的文件

查看支持字符集

show charset;

查看校对规则

show collation;

insert

INSERT INTO stu(sname,sage,sgender,sfz,intime) VALUES('hw',20,'f','123456',NOW());

同时录入多行数据

INSERT INTO stu(sname,sage,sgender,sfz)
VALUES
('hwe',20,'f','123456fe'),
('fe',43,'m','23');

update

UPDATE stu SET sname='zhao4' WHERE id=2; 修改id=2的sname为zhao4

delete

DELETE FROM stu WHERE id=3; delete 命令只是对信息标注不可见,不释放空间

TRUNCATE TABLE stu;

select

顺序:

select

from

where

group by

having

order by

limit

select @@xxx 查看系统参数

select @@port; 查看端口

select @@basedir; 查看目录

select @@datadir;

select @@socket;

select @@server_id;

select @@log_error;

select @@log_bin_basename;

show variables like 'innodb%';

select()函数

select NOW();

select DATABASE();

user() 当前用户

concat() 拼接字符串

group_concat() 一行显示

单表子句-where

where 等值查询

select * from city where country='china';

where 比较操作符

select * from city where population<100;

where 逻辑运算符(and, or)

select * from city where countrycode='CHN' AND population>500000;

where 模糊查询

select * from city where district LIKE 'guang%';

where in子句

select * from city where countrycode IN ('CHN', 'USA'); 和or功能相似

where between and

select * from city where population> 1000000 and population<2000000; =
select * from city where population BETWEEN 1000000 and 2000000;

单表子句-group by

聚合函数: max(),min()

avg() 平均数

sum() 总和

count() 个数

group_concat() 列转行

select countrycode,SUM(population) from city group by countrycode;

单表子句-having

select district,SUM(population)
from city
where countrycode='chn'
group by district
having sum(population)<1000000;

单表子句-order by

select * from city
where countrycode='chn'
order by population DESC;

DESC 降序(从大到小)

ASC 从小到大

select district,sum(population) from city
where coutrycode='chn'
group by district
having sum(population)>500000
order by sum(population) DESC;

单表子句-limit

前3名

select district,sum(population) from city
where coutrycode='chn'
group by district
having sum(population)>500000
order by sum(population) DESC
limit 3;

n 代表开始位置

m 代表显示数量

limit n,m

limit m offset n

单表子句-distinct

select distinct(countrycode) from city;

联合查询- union all

select * from city where countrycode='chn'
union all
select * from city where countrycode='usa';

一般情况下,会将 INOR 语句改写成UNION ALL,来提高性能

union 去重复

union all 不去重复

多表连接

select employees.emp_no,salaries.salary from
employees join salaries 
on employees.emp_no=salaries.emp_no 
order by salaries.salary desc 
limit 100;

你可能感兴趣的:(2019-07-18 MySql 命令)