# about-mysql **Repository Path**: cqgvip/about-mysql ## Basic Information - **Project Name**: about-mysql - **Description**: 关于数据库相关的东东 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-03-06 - **Last Updated**: 2021-11-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### about-mysql ### 命令行快捷键 ```textmate ctrl+A 跳转到行首 ctrl+E 跳转的行末 ctrl+y 恢复 ctrl+w 删除单词 ctrl+u 删除之前 ctrl+k 删除之后 ``` ### 修复表数据3种方式 ``` 1.check table ; 2.repair table ; 3.alter table engine=innodb; ``` ### 查看索引 ``` show indexes from ``` ### 查看执行计划 ``` explain ``` ### 创建db ``` CREATE DATABASE [IF NOT EXISTS] <数据库名> [[DEFAULT] CHARACTER SET <字符集名>] [[DEFAULT] COLLATE <校对规则名>]; show databases; # 查看当前所有的数据库 show create database ; #查看某个数据库 ex: create database if not exists mytest default CHARACTER SET utf8 DEFAULT COLLATE utf8_chinese_ci; ``` ### 创建表 ``` create table ( id int not null, name varchar(20) not null default '' comment '名称', addr varchar(20) not null default '' comment '地址', ... primary key (id), key idx_name(name,addr) ) engine=innodb comment '我的表'; show tables; show create table ; show full columns from ; show indexes from ; alter table comment 'mytable'; alter table
modify column id int not null comment 'myId'; 常用的语法格式如下: ALTER TABLE <表名> [修改选项] 修改选项的语法格式如下: { ADD COLUMN <列名> <类型> | CHANGE COLUMN <旧列名> <新列名> <新列类型> | ALTER COLUMN <列名> { SET DEFAULT <默认值> | DROP DEFAULT } | MODIFY COLUMN <列名> <类型> | DROP COLUMN <列名> | RENAME TO <新表名> } [具体可参考](http://c.biancheng.net/view/2433.html) alter table
add primary key (id); alter table
add unique key idx_name_addr(name,addr); alter table
add key idx_name_addr(name,addr); alter table
drop primary key(id); alter table
drop key idx_name; ``` ### 关于索引的优化 #### 高频使用的 区分度又很低的字段 可以加索引 如果某个查询sql 没有该字段的过滤条件可以变相的绕过 select * form where sex in ('m','f') #### 对于需要limit offset 很多行的化 可以想办法绕过 (延迟关联) select * from tab inner join (select id from tab where index_filed>val limit 10000,10) as s using(id); #### 针对范围查找的优化 ``` col between var1 and var2 col > var1 and col