1. 配置文件: my.ini
2. 在Windows启动MySQL: net start 服务名(mysql) / net stop mysql
3. 连接到本机上的MySQL,可通过执行下列3个中任意一个登录:
4. 连接到远程主机 上的MySQL,加入远程主机IP为:110.110.11.100
- mysql -u root -p
- mysql -uroot -p
- mysql -uroot -proot (注:-p与密码间没有空格)
5. 修改密码 :mysql> set password for root@localhost = password('新密码');
- mysql -h 110.110.11.100 -u root -p 密码(之间可不加空格)
6. 解决乱码问题: set names gbk; //设置mysql客户端用gbk显示数据,不影响鼠标当中的数据
7. show variables like '%char%';
8. 显示当前的用户: select user();
9. 显示所有数据库名: show databases();
10. 使用某个数据库:use 数据库名;
11. 显示当前选择的数据库:select database();
12. 创建数据库: create database 库名 [character set='utf-8'];13. 修改数据库: alter database 库名 character set='utf-8';
14. 查看数据库创建时所使用的指令: show create database 库名;
15. 删除数据库: drop database 库名;
16. 导出数据库 : mysqldump -u用户名 -p密码 数据库名(表名) > 路径 备份.sql;
17. 导入数据库 : mysql -u用户名 -p密码 数据库名 < 路径 备份.sql
18. 将 文本数据 写入 数据库 中
mysql> load data local infile "d:\\temp\\a1.txt"into table 表名;
注:本文数据应符合的格式:字段数据之间用tab键隔开,null值用\n来代替
19. 显示数据表的结构:
describe表名;(或desc 表名)
20. 建立数据表:
- use 库名(进入数据库)
- create table 表名 (字段1 varchar(20), 字段2 char(1));
SELECT类似于其他编程语言里的print 或 write
21. 显示MySQL的版本:
select version();
22.显示当前时间: select now();
23. 显示日期: select dayofmonth(current_date) ;
24. 显示月份:select month(current_date);
25. 显示年份:select year(current_date);
26. 显示字符串:select ("welcome to my blog!");
27. 当计算器用:select ((4*4) /10 ) + 25;
28. 串接字符串: select concat(f_name, " ", l_name);
29. 显示当前数据库下所有数据表: show tables();
30. 创建数据表: create table 表名 (字段1 类型1,字段2 类型2...);
31. 创建数据表同时将查询结果写入数据表:
create table 表名1(字段1,数据类型) select 字段2 from 表名2 group by 字段2;
32. 删除数据表:
drop table 表名;
33. 查看数据表结构: show columns from 表名;
34. 查看数据表定义: show create table 表名;
35. 查找记录: select 字段1,... from 表名;
36. where进行条件查询:select 字段1 from 表名 where 条件表达式;
37. group by分组查询:select 字段1 from 表名 group by 字段1; //或 group by 1
38. having设置分组条件:select 字段1 from 表名 group by 字段1 having 字段2>30;
39. order by对分组结果进行排序:select * from 表名1 order by 字段1 DESC; //降序DESC,升序ASC
40. limit语句限制查询结果返回的数量:select * from 表名 limit 2,2; //第3条记录开始,显示2条记录,即显示3-4记录
41. 子查询 : select 字段 from 表名1 where 字段1=(select 字段2 from 表名2);
- 使用比较运算符的子查询:=, >, <, >=, <=, <>, !=, <=>
- 由 IN 或NOT IN 引发的子查询:select 字段from 表名1where 字段1IN (select 字段2 from 表名2);
修改数据表
42. 修改数据表名:
alter table
旧表名 rename
新表名;
43. 增加列: alter table 表名 add 字段 类型;
44. 删除列: alter table 表名 drop 字段1,字段2;
45. 修改某列位置: alter table 表名 modify 字段 类型 first ;
46. 修改某列数据类型: alter table 表名 modify 字段 新类型;
47. 修改某列名称: alter table 表名 change 旧字段 新字段 原数据类型;
48. 表插入数据: insert into 表名 [(字段1...)] values (值1...);
49. 将查询结果写入另一个表中: insert 表名1 (字段1) select 字段2 from 表名2 where 条件;
50. 单表删除记录: delete [ 表名1 ] from 表名1 where 条件; //从表1删除满足条件的数据行
51. 修改表中数据: update 表名 set 字段=新值 where 条件表达式;
52. 多表更新:
53. 多表删除:update 表名1 as a1 INNER JOIN 表名2 as a2 ON a1.字段1=a2.字段1 SET a1.字段1=a2.字段2;
delete t1 from 表1 as t1 left join(select 字段1 from 表1 group by 字段1 having count(字段2)>=2) as t2ON t1.字段1=t2.字段1 where t1.字段2 > t2.字段2;
索引
是
加快查询
的最重要的工具
54. 添加普通索引:
alter table
表名
add index
索引名 (字段);
55. 添加多列索引: alter table 表名 add index 索引名 (字段1, 字段2,...);
56. 添加主键约束: alter table 表名 add primary key (字段);
57. 添加唯一约束: alter table 表名 add unique (字段);
58. 添加外键约束: alter table 表名1 add foreign key (字段1) references 表名2 (字段2);
59. 删除普通索引: alter table 表名 drop index 索引名;
60. 删除主键约束: alter table 表名 drop primary key ;
61. 删除唯一约束:
62. 删除外键约束:
- show indexes from 表名; //查看 Key_name的值,该值为唯一约束名
- alter table 表名 drop index 索引名;
- show create table 表名; //查出外键约束名
- alter table 表名 drop foreign key 外键约束名;
函数
63.
字符函数
64. 数值运算符与函数
- concat(),字符连接
- concat_ws(), 使用指定的分隔符进行字符连接
- format(), 数字格式化
- lower(), 转换成小写字母
- upper(), 转换成大写字母
- left(), 获取左侧字符
- right(), 获取右侧字符
- length(), 获取字符串长度
- ltrim(), 删除前导空格, left, 字符最左侧为前导
- rtrim(), 删除后续空格,right, 字符最右侧为后续
- trim(), 删除前导哦和后续空格
- substring(), 字符串截取
- [not] like, 模式匹配
- replace(), 字符串替换
65. 比较运算符与函数
- ceil(), 进一取整,即向上取整
- floor(), 舍一取整,即向下取整
- div, 整数除法
- mod, 取余数(取模)
- power(), 幂运算,如power(3,3), 即3的3次方,得27
- round(), 四舍五入
- truncate(), 数字截取
66. 日期时间函数
- [not] between...and... ,[不]在范围之内
- [not] in(),[不]在列出值范围内
- is [not] null,[不]为空
67. 信息函数
- now(),当前日期和时间
- curdate(),当前日期
- curtime(),当前时间
- date_add(),日期变化
- datediff(),日期差值
- date_format(),日期格式化
68. 聚合函数(只有一个返回值)
- connection_id(),连接ID
- database(),当前数据库
- last_insert_id(),最后插入记录的ID号
- user(),当前用户
- version(),版本信息
69. 加密函数
- avg(),平均值
- count(),计数
- max(),最大值
- min(),最小值
- sum(),求和
- md5(),信息摘要算法
- password(),密码算法