命令
數據庫密碼
建立密碼 | mysqladmin -u root password '123456' |
---|---|
修改密碼 | 命令行 一、mysqladmin -u root -p'123456' password 'oldboy' |
sql語句 | 二、set password=password('123123'); flush privileges; |
sql語句 | 三、update mysql.user set authentication_string=PASSWORD('123456') where user ="root" and host ="localhost"; |
ml
基本命令 | 命令格式 |
---|---|
切換進入庫 | use oldboy; |
刷新 | flush privileges; |
受權新建用戶 | show grants for 'wordpress'@'172.16.1.%'; |
受權遠程鏈接 | create user jyt@'172.16.1.%' identified by '123456'; |
修改用戶密碼 | alter user jjj@'172.16.1.%' identified by '123123'; |
查看當前數據庫的字符集 | show charset; |
收回oldboy用戶的drop權限 | revoke drop on oldboy.* from oldboy@'lolcation'; |
向表中插入數據 | INSERT INTO stu(name,age) VALUE('oldguo','18'); |
刪除
刪除 | 命令格式 |
---|---|
刪除無用的庫 | drop database oldboy; |
刪除用戶 | drop user root@'oldboy'; |
刪除一張表 | drop table jyt; |
刪除表中的列 | alter table oldguo drop state; |
$刪除全部用戶$ | delete from mysql.user; |
添加
添加 | 命令格式 |
---|---|
建立庫 | create detabase jyt; |
建庫 | create database oldguo charset utf8mb4; |
增長用戶並將用戶設爲超級管理員 | grant all privileges on . to jyt@'localhost' identified by '123456' with grant option; |
建立表 | create table oldguo ()charset=utf8mb4 engine=innodb; |
表 | - |
添加一列到最後 | alter table jyt add num char(11) not null unique comment '手機號'; |
添加一列到指定列後 | alter table jyt add qq varchar(255) not null unique comment 'qq' after name; |
添加一列到第一列 | alter table oldguo add sid varchar(255) not null unique comment '學生號' first; |
修改
修改 | 命令格式 |
---|---|
修改庫的格式 | alter database oldguo charset utf8mb4; |
修改列的屬性 | alter table oldguo modify name varchar(128) not null ; |
show語句
show語句 | 命令格式 |
---|---|
show databases; | 查看全部庫 |
show tables; | 查看當前庫下的表名 |
show tables from world; | 查看world庫下的全部表 |
show create table; | 查看建表語句 |
show grants for root@% | 查看用戶權限 |
show charset | 查看全部字符集 |
show collation | 查看校對規則 |
show full processlist | 查看數據庫的鏈接狀況 |
show status | 查看數據庫總體狀態 |
show variables | 查看數據庫全部變化狀況 |
show variables | 查看數據庫全部變化狀況 |
show engines | 查看全部存儲引擎 |
show engine innodb status | 查看存儲引擎狀態狀況 |
show binary logs | 查看二進制日誌狀況 |
show binlog events in | 查看二進制日誌事件 |
show relalog events in | 查看relay日誌事件 |
show slave status | 查看從庫狀態 |
show master status | 查看數據庫binlog位置信息 |
show index from | 查看錶的全部狀況 |
查看全部庫 | show databases; |
查看當前庫中的表 | show tables; |
查看特定庫中的表 | show tables from jyt; |
查看stu表中數據 | show create table stu; |
查看用戶權限 | show grants for 'wordpress'@'172.16.1.%'; |
查看連接線程 | show processlist; |
匹配查詢庫 | show databases like 'oldboy'; |
匹配查詢庫以xx開頭的全部 | show databases like 'oldboy'; |
查看建立的用戶oldboy擁有哪些權限 | show grants for oldboy@'localhost'; |
查詢select語句
基本命令(select) | 命令格式 |
---|---|
查看當前所在庫 | select database(); |
查看當前登陸用戶 | select user(); |
查看錶名對應主機 | select user,host from mysql.user; |
查看錶名對應主機和密碼 | select user,host ,authentication_string from mysql.user; |
查看錶 | select user,host from mysql.user where user="jyt"; |
在db表裏查看權限 | select * from mysql.db where user='wordpress' and host='172.16.1.%'\G |
表相關 | - |
order by | 排序 |
查詢統計總數 | select district,sum(population) from city where countrycode='chn' group by district; |
查詢統計總數並排序降序 | SELECT district,sum(population) FROM city WHERE countrycode='chn' GROUP BY district ORDER BY SUM(Population) DESC; |
查詢中國全部的城市,並以人口數降序輸出 | select*from city where countrycode='chn' order by population desc; |
limit m,n 跳過m行顯示n行 | limit x offset y 跳過y行顯示x行 |
前5行 | SELECT*FROM city WHERE countrycode='chn' ORDER BY population DESC LIMIT 5; |
顯示6-10行 | SELECT*FROM city WHERE countrycode='chn' ORDER BY population DESC LIMIT 5,5; |
顯示6-10行 | select*from city where countrycode='chn' order by population desc limit 5 offset 5; |
函數 | 例 |
avg()平均數 | select district,avg(population) from city where countrycode='chn' group by district; |
count()計數 | select countrycode,count(name) from city group by countrycode; |
sum()求和 | select countrycode,sum(population) from city group by countrycode ; |
max()最大值 | - |
min()最小值 | - |
group_concat()聚合 | select countrycode,group_concat(district) from city group by countrycode; |
where | 至關於grep | 說明 |
---|---|---|
where配合等值查詢 | select * from world.city where countrycode='chn'; | 查詢表中的中國城市信息 |
where配合不等值查詢 | select * from world.city where Population<100; | 人口小於100人的城市 (>,<,<=,>=,<>) |
where配合模糊查詢 | select * from world.city where CountryCode like 'c%'; | 國家以c開頭 禁止%開頭 |
where配合邏輯鏈接符(AND or) | select * from world.city where Population > 10000 AND Population < 20000; | select * from world.city where population between 10000 and 20000; |
select * from world.city where CountryCode='chn' OR CountryCode='usa'; | select * from world.city where countrycode in ('chn','usa'); | |
SELECT * FROM world.city WHERE CountryCode='chn' UNION ALL SELECT*FROM world.city WHERE CountryCode='usa'; | 推薦 union 去重 加all不去重 默認去重 |