數據庫經常使用命
1: show databases; ==》查看數據庫
2: CREATE DATABASE <數據庫名> ==》建立數據庫
3: show create database <數據庫名> ==》查看建立數據庫
4: SHOW VARIABLES like 'character%'; ==》數據庫字符集查看
5: SHOW COLLATION; ==》便可查看當前MySQL服務實例支持的字符序
6: create database school DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
7: create database school DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
======》建立數據庫並添加字符集的命名規則
8: DROP DATABASE <數據庫名> ==》刪除數據庫
9: USE <數據庫名> ==》鏈接數據庫
10: select database(); --查看當前鏈接的數據庫
11: SELECT VERSION();
12: SELECT USER();
13: SELECT User,HoST FROM mysql.user; ==》查看數據庫信息
13.1: show procsslist;或這條命令show full processlist(看的具體)
==》查看數據負載【若是負載高,這裏面就會有不少語句(慢語句)】
13.2:show variables like '%pro%';(能夠開啓profiling 開關,查詢語句運行通過)
14: mysql 用戶受權:
方法1:create和grant結合:CREATE USER <'用戶名'@'地址'> IDENTIFIED BY ‘密碼’;
查看用戶權限:show grants for '用戶名'@'地址';
方法2:直接grant(授全部權) grant all on *.* to 'root'@'%' identified by '123456'; 受權
flush privileges; 更新
收回權限:REVOKE
15: 生產環境受權用戶建議:一、博客,CMS等產品的數據庫受權
select,insert,update,delete,create
庫生成後收回create權限
二、生產環境主庫用戶受權
select,insert,update,delete
三、生產環境從庫受權
select
15.1》查看mysql的最大鏈接數:mysql>show variables like '%max_connections%';
查看服務器響應的最大鏈接數:mysql> show global status like 'Max_used_connections';
設置MySQL最大鏈接數值(方法1):mysql> set GLOBAL max_connections=10000;
方法2:修改mysql配置文件my.cnf,在[mysqld]段中添加或修改max_connections值:
max_connections=10000
16: MySQL表操做
1. 查看錶
SHOW TABLES;
SHOW TABLES FROM <數據庫名>;
show create table <表名>\G; ====》查看錶結構】如:show create table z_money\G;
1.1:select count(distinct 字段名(列)) from <表名>; =====》查看條件字段列的惟一性:如select count(distinct type) from z_money;
2. 建立表------》命令——>CREATE TABLE <表名>(<字段名1><類型1>[,..<字段名n><類型n>]);
示例:CREATE TABLE student (
id int(4) NOT NULL AUTO_INCREMENT,
name char(20) NOT NULL,
sex enum('M','F'),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
3. ---查看錶結構 desc student;
4. 表插入數據---->命令:insert into <表名>[(<字段名1>[,…<字段名n>)] values (值1)[,(值n)]
單條插入 :insert into student values(2001,'z3','M');
insert into student(name,sex) values('smith','F');
批量插入:insert into student values(2003,'t1','M'),(2004,'t2','F');
5. 查詢數據----》查詢前幾行:select * from 表名 limit n;
多字段查詢:select id 編號,name 姓名 from student;
能夠爲查詢字段指定別名
*查詢全部字段
指定查詢條件
where
in指定集合查詢
select * from student where id in (2003,2004);
not in
指定範圍查詢
[not] between and
select * from student where id between 2001 and 2003;
字符串匹配查詢
[not] like
select * from student where name like 't%';
空查詢
is null
多條件查詢
and
or
去除重複查詢
distinct
select distinct sex from student;
查詢結果排序
order by 字段
select * from student order by id; 默認升序
select * from student order by id asc; 升序
select * from student order by id desc; 降序
分組排序
group by
select sex,count(name) from student group by sex;
css