首页 > 技术文章 > 2016-08-09 随笔总结mysql相关

shuguoqing 2016-08-22 21:02 原文

mysql -h127.0.0.1 -uroot -p密码        // 命令行连接mysql
show  databases;                              // 查看所有数据库
use  数据库名;                                   // 选择数据库
show  tables;                                     // 查看所有数据表
describe  数据表名;                           // 查看表结构
 
mysqldump -uroot -p密码  study(数据库名) > c:\mystudy.sql     // 导出数据库
                                                                                                             注意:此命令要在dos命令行执行,不能在mysql命令行执行
create  databases  study1;        // 导入数据库
use study1;
mysql -uroot -p密码 <c:\mystudy.sql
                                                                              
select  *  from  数据表名;                   // 查询显示所有记录
select  count(*)  from  数据表名;        // 统计记录条数
select  id, name, age  from  数据表名  where  sex='男'          // 所有男生的id,姓名,年龄
select  avg(age)  from  数据表名  where  sex='男'                  // 所有男生的平均年龄
select  sum(age)  from  数据表名  where  sex='男'                 // 所有男生的总年龄
select  id, name, age  from  数据表名  where  sex='男'  order  by  age  desc  limit 0,3      // 查询男生中年龄最大的3个人的信息 
                                                                                                                                                    order by 排序 
                                                                                                                                                    desc 降序
select  id, name, age  from  数据表名  where  sex='男'  order  by  age  asc  limit  0,3       //  查询男生中年龄最小的3个人的信息
                                                                                                                                                    asc 升序  
select  count(distinct  age)  from 数据表名           // 年龄有多少阶段
                                                                                     distinct 去掉重复值                       

select  avg(age)  from  数据表名  group  by  sex         // 按性别分组,统计出每组用户的平均年龄 
                                                                                            group by 分组
select  avg(age)  from  数据表名  where  age<25 group  by  sex  having  avg(age)>20   // 年龄小于25的用户中,按性别分组且每                                                                                                                                                                       组平均年龄必须大于20,查出每组用户                                                                                                                                                                       的平均年龄
update  数据表名  set  age=22, sex='女'  where  id=5      // 将id等于5的用户年龄改为22,性别改为女
delete  from  数据表名  where  age>=25                           // 删除所有年龄大于等于25的用户
delete  from  数据表名
insert  into  数据表名(name, sex, age)  values('张三', '男', 25)  

创建表:
命令:create table <表名> (<字段名 1> <类型 1> [,..<字段名 n> <类型 n>]);
例子:
mysql> create table MyClass(
          > id int(4) not null primary key auto_increment,
          > name char(20) not null,
          > sex int(4) not null default '0',
          > degree double(16,2));

Mysql 数据库补充内容: 
四种常用索引:【1. 索引主要作用:对部分关键字段建立索引,也相当于排序,能提高查询速度
                                   注意:索引也会有代价的,不是索引字段越多越好。建立索引能提高上10倍的查询速度。对大数据表特别有用。一                                                             般是针对 where, order by 中常用的字段建立索引 
                             】
1. 主索引         primary key 一个表只能创建一个主索引,主键默认主索引,具有唯一性
2.唯一索引       unique key 字段中的值具有唯一性,一个表可以有多个
3.普通索引       index key 一般的索引,没有唯一性要求,一个表可以有多个
4.全文索引       fulltext key 针对 text 等类型字段创建,像新闻内容字段 

数据库引擎:
Myisam:默认引擎,适合 select 查询操作,查询速度快。不支持事务。表锁机制。
InnoDB:支持事务,行锁机制 ,适合 update, insert 等操作。

推荐阅读