码迷,mamicode.com
首页 > 数据库 > 详细

mysql 常用

时间:2014-05-28 01:29:48      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:des   cWeb   style   c   code   tar   

1.复制表

  create table t2 like t1;

  insert into t2 select * from t1;

2.索引

  a. ALTER TABLE 用来创建普通索引,UNIQUE 索引和 PRIMARY KEY 索引

    ALTER TABLE table_name ADD INDEX index_name(column_list)

    ALTER TABLE table_name ADD UNIQUE(column_list)

    ALTER TABLE table_name ADD PRIMARY KEY (column_list)

  b. alter table drop 删除索引

    ALTER TABLE table_name DROP INDEX index_name

    ALTER TABLE table_name DROP PRIMARY

  c. 查看索引

    show index from table_name;

3.视图

  creatr view v_t1 as select * from t1 where id>3 and id<10;

  drop view v_t1;   //删除

4.内置函数

  a.RAND() 随机取数据

    select * from t1 order by rand();

    select * from t1 order by rand() limit 3;  //随机取三条数据从t1表中

    select * from t1 where id<10 order by rand() limit 3;

  b.UNIX_TIMESTAMP() 返回时间邮戳

    select unix_timestamp()

5.MYSQL 优化

  a. 使用 show status + SQL 语句 了解数据库的各种状态

    show session status......   //表示当前登陆后 (默认)

    show global  status......//表示自数据库启动至今

   例子: 

    show global status like "com_select%";   //从数据库启动以来的查询数

    show global status like "com_insert%";  //从数据库启动以来的插入数量

    show global status like "com_update%";  //从数据库启动以来的更新数量

    show global status like "com_delete%";   //从数据库启动以来的删除数量

   其他选项:

    connections   //连接 mysql 的 数量

    Uptime   //已经工作的秒数

    Slow_queries    //慢查询的次数

      show variables  like ‘%slow%‘;   //查询慢查询日志功能是否开启

      bubuko.com,布布扣

      show_query_log  //功能是否开启

      slow_query_log_file //文件的保存位置

      

      show variables  like ‘%long%‘;   //查询慢查询定义的时间  默认为10秒

  b.解析sql 语句

    desc .....

    explain ....

    bubuko.com,布布扣

      

 

 

mysql 常用,布布扣,bubuko.com

mysql 常用

标签:des   cWeb   style   c   code   tar   

原文地址:http://www.cnblogs.com/mr-amazing/p/3753751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!