1.MySQL数据库常用命
数据库常用命
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: 生产环境授权用户建议:1、博客,CMS等产品的数据库授权
select,insert,update,delete,create
库生成后收回create权限
2、生产环境主库用户授权
select,insert,update,delete
3、生产环境从库授权
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;