一、MySQL数据库安装
sudo apt install mysql-server-5.7 sudo apt install mysql-client-5.7 sudo apt install libmysqlclient-dev 2. MySQL 服务的启动和停止 service mysql start; service mysql stop; 3. MySQL 数据库的登录和退出 mysql -u用户名 -p密码 eg. mysql -uroot -p205205 exit;
二、数据库操作
* 查看数据库 show databases; show create database 数据库名; //查看创建数据库时的语句* 创建数据库 create database 数据库名; //默认字符集是latin1(拉丁文,不支持中文) create database 数据库名 character set utf8; //创建一个数据库指定为utf8字符集 * 删除数据库 drop database 数据库名; * 查看当前数据库 select database();* 切换数据库 use 数据库名; * 修改数据库的字符集 alter database 数据库名 character set utf8;
三、用户操作
* 查看用户 select host,user from mysql.user; * 创建用户 create user '用户名'@'允许连接的主机IP' identified by '密码'; eg. create user 'test'@'%' identified by 'test'; create user 'test'@'127.0.0.1' identified by 'test'; create user 'test'@'localhost' identified by 'test';
* 删除用户 drop user '用户名'@'允许连接的主机IP'; delete from mysql.user where user = '用户名' and host = '允许连接的主机IP';
* 修改用户密码 update mysql.user set password = password('新密码') where user = '用户名' and host = '允许连接的主机IP';
* 设置用户权限 grant 权限 on 数据库.* to '用户名'@'允许连接的主机IP'; grant 权限 on 数据库.* to '用户名'@'允许连接的主机IP' identified by '密码'; eg. grant insert,delete,update,select on test.* to 'test'@'localhost'; grant all privileges on test.* to 'test'@'localhost'; * 刷新权限表 flush privileges;
四、数据表操作
* 创建数据表 create table 表名(字段名 字段类型,字段名 字段类型,...); create table 表名(字段名 字段类型,字段名 字段类型,...) character set utf8;
* 删除数据表 drop table 表名; * 修改数据表 1. 修改表名: rename table 原表名 to 新表名; 2. 增加字段: alter table 表名 add column 字段名 字段类型; //column 可省略 3. 修改字段类型: alter table 表名 modify column 字段名 新字段类型; 4. 修改字段名和字段类型: alter table 表名 change column 原字段名 新字段名 新字段类型; 5. 删除字段: alter table 表名 drop column 字段名; 6. 修改表的字符集 alter table 表名 character set utf8; * 显示数据库中的表 show tables;
* 显示数据表的结构 desc 表名; describe 表名;
* 查看创建表时的语句 show create table 表名;
五、数据类型
* 整数类型 tinyint 1字节 smallint 2字节 int或integer 4字节 bigint 8字节 float(m,n) 4字节 //不指定m,n有可能无法通过where条件查询 double 8字节 decimal(m,d) m>d?m+2:d+2 m代表数据的宽度,d代表小数宽度 * 字符串类型 char 定长字符串 varchar 变长字符串 text 长文本数据 blob 二进制长文本数据 * 日期和时间类型 date 4字节 yyyy-mm-dd 日期 time 3字节 hh:mm:ss 时间 year 1字节 yyyy 年份 datetime 8字节 yyyy-mm-dd hh:mm:ss 混合日期时间 timestamp 4字节 yyyy-mm-dd hh:mm:ss 混合日期时间 如果未明确赋值或赋值为null,则自动使用当前系统时间 * 复合类型 enum 只能使用集合中的一个值或null set 只能使用集合中的多个值或null
六、数据操作
* 插入数据 insert into 表名(字段名,字段名,...) values(字段值,字段值,...); * 删除数据 delete from 表名; delete from 表名 where 字段名 = ?; truncate 表名; * 修改数据 update 表名 set 字段名 = ?; update 表名 set 字段名 = ?,字段名=?; update 表名 set 字段名 = ? where 字段名 = ?;
* 查询数据 1. 查询所有数据 select * from 表名; 2. 查询指定字段 select 字段名,字段名 from 表名; 3. 根据条件查询 select 字段名 from 表名 where 字段名 = ?; * * * 4. 字段别名 select 字段名 as 别名 from 表名;` 5. 去掉重复数据 select distinct 字段名 from 表名; 6. 滤空修正 select ifnull(字段名,替换值) from 表名; 7. 使用算术表达式 select 字段名 + 字段名 as '别名' from 表名; 8. 字符串拼接 select concat(str1,str2,...) as '别名' from 表名; 9. 常用函数 min,max,avg,count select min(字段名) from 表名; * * * 10. 条件查询运算符 select * from 表名 where 字段名 > 3; select * from 表名 where 字段名 <> 3; select * from 表名 where 字段名 between 3 and 5; select * from 表名 where 字段名 is null; select * from 表名 where 字段名 is not null; select * from 表名 where 字段名 = ? and 字段名 = ?; select * from 表名 where 字段名 = ? or 字段名 = ?; 11. 模糊查询 select * from 表名 where 字段名 like '%xxx'; select * from 表名 where 字段名 like '_xxx'; select * from 表名 where 字段名 rlike '[0-9]abc'; select * from 表名 where 字段名 rlike '[^0-9]abc'; select * from 表名 where 字段名 like '%\%%'; 12. 分组查询 select * from 表名 where 查询条件 group by 字段名 having 查询条件; 13. 排序查询 select * from 表名 where 查询条件 order by 字段名 asc; select * from 表名 where 查询条件 order by 字段名 desc; 14. 嵌套查询 select * from 表名 where 字段名 = (select 字段名 from 表名 where 字段名=?); * 当子查询返回的值只有一个时,才可以直接使用 =,>,< 等运算符; * 当子查询返回的值是一个结果集时,需要使用 in,not in,any,some,all 等操作符; * all : 匹配结果集中的所有结果, 例如 > all 表示比所有结果都大; * any : 匹配结果信中任意一个结果, 例如 < any 表示只要比其中任意一个小就符合条件; * in : 查询的数据在子查询的结果集中,相当于 = any; * not in : 查询的数据不在子查询结果集中,相当于 <> all; * some : 匹配结果集中的部分结果,基本等同于 any,不常用; * 子查询通常只用于返回单个字段的结果集,如果需要匹配多个字段,可以使用多表联查(后面讲); eg. select * from 表名 where 字段名 > all (select 字段名 from 表名);
七、多表联查
* 数据表的别名 select * from 表名 as 别名; * 交叉连接(笛卡尔积) select * from 表1 cross join 表2 on 连接条件 where 查询条件; select * from 表1,表2 where 查询条件; * 内连接 select * from 表1 inner join 表2 on 连接条件 where 查询条件; * 左外连接 select * from 表1 left outer join 表2 on 连接条件 where 查询条件; * 右外连接 select * from 表1 right outer join 表2 on 连接条件 where 查询条件; * 联合查询 select id,name from 表1 union select id,name from 表2; * 全外连接(MySQL不支持) select * from 表1 full outer join 表2 on 连接条件 where 查询条件; select * from 表1 left join 表2 on 连接条件 where 查询条件 union select * from 表1 right join 表2 on 连接条件 where 查询条件;
八、字符串函数
* upper 和 ucase select upper(name) from 表名; * lower 和 lcase select lcase(name) from 表名; * replace(str, from_str, to_str) select replace(字段名,替换前的值,替换后的值) from 表名; * repeat(str,count) select repeat('abc',2) from 表名; * reverse(str) select reverse('abc') from 表名; * insert(str,pos,len,newstr) select insert('abcdef',2,3,'hhh'); * substring(str from pos) select substring('abcdef',3); * substring_index(str,delim,count) select substring_index('abacadae','a',3); * ltrim(str) select ltrim(' abc'); * rtrim(str) select rtrim('abc '); * trim(str) select trim(' abc '); * mid(str,pos,len) select mid('abcdef',2,3); * lpad(str,len,padstr) select lpad('abc',8,'de'); * rpad(str,len,padstr) select rpad('abc',8,'de'); * left(str,len) select left('abcd',2); * right(str,len) select right('abcd',2); * position(substr in str) select position('c' in 'abcdc'); * length(str) select length('abcd'); * concat(str1,str2,...) select concat('abc','def','gh');
九、日期时间函数
* dayofweek(date) select dayofweek('2017-04-09'); * weekday(date) select weekday('2017-04-09'); * dayname(date) select dayname('2017-04-09');* dayofmonth(date) select dayofmonth('2017-04-09');* dayofyear(date) select dayofyear('2017-04-09'); * month(date) select month('2017-04-09'); * monthname(date) select monthname('2017-04-09'); * quarter(date) select quarter('2017-04-09'); * week(date,first) select week('2017-04-09'); select week('2017-04-09',1); * year(date) select year('2017-04-09'); * hour(time) select hour('18:06:53'); * minute(time) select minute('18:06:53'); * second(time) select second('18:06:53'); * period_add(p,n) select period_add(201702,2); * period_diff(p1,p2) select period_diff(201605,201704); * date_format(date,format) select date_format('2017-04-09','%d-%m-%y'); * time_format(time,format) select time_format('12:22:33','%s-%i-%h'); * curdate() 和 current_date() select curdate(); select current_date();* curtime() 和 current_time() select curtime(); select current_date(); * now(),sysdate(),current_timestamp() select now(); select sysdate(); select current_timestamp(); * unix_timestamp() select unix_timestamp(); * sec_to_time(seconds) select sec_to_time(3666); * time_to_sec(time) select time_to_sec('01:01:06'); 18. top N 问题 select * from 表名 limit 0,N;
十、索引
* 普通索引 create index 索引名 on 表名(字段名(索引长度)); alter table 表名 add index 索引名 (字段名(索引长度)); create table 表名(字段名 字段类型,字段名 字段类型,index 索引名 (字段名(索引长度)); * 唯一索引 create unique index 索引名 on 表名(字段名(索引长度)); alter table 表名 add unique 索引名 (字段名(索引长度)); create table 表名(字段名 字段类型,字段名 字段类型,unique 索引名 (字段名(索引长度)); * 全文索引 //只支持 MyISAM 引擎 create fulltext index 索引名 on 表名(字段名); alter table 表名 add fulltext 索引名(字段名); create table 表名(字段名 字段类型,字段名 字段类型,fulltext (字段名); * 组合索引 create index 索引名 on 表名(字段名(索引长度),字段名(索引长度),...); alter table 表名 add index 索引名 (字段名(索引长度),字段名(索引长度),...; create table 表名(字段名 字段类型,字段名 字段类型,index 索引名 (字段名(索引长度),字段名(索引长度)); * 查看索引 show index from 表名; * 删除索引 alter table 表名 drop index 索引名;
十一、约束
* 主键约束 create table 表名(字段名 字段类型 primary key,字段 字段类型,...); //一个表只能有一个主键,这个主键可以由一列或多列组成 create table 表名(字段1 字段类型,字段2 字段类型,primary key(字段1,字段2); * 唯一键约束 create table 表名(字段名 字段类型 unique,字段名 字段类型,...); * 外键约束 create table 表1(字段1 字段类型,字段2 字段类型,foreign key(字段1) references 表2(字段名),...); * 非空约束 create table 表名(字段名 字段类型 not null,字段名 字段类型,...); * 默认值约束 create table 表名(字段名 字段类型 default 默认值,字段名 字段类型,...); * check约束(MySQL 不支持) create table 表名(字段1 字段类型,字段2 字段类型,check(字段1 > 30),...);
十二、事务
begin; 或者 start transaction; 要执行的操作; commit; 或 rollback;
十三、触发器
create trigger 触发器名 {before|after} insert|update|delete} on 表名for each row //行触发器,MySQL不支持语句触发器 begin 触发器执行的操作 //如果要获取表中的数据当作条件,insert时用NEW,delete时用OLD,update时都可以 end;
十四、存储过程
* 创建存储过程 create procedure 存储过程名(参数) begin 要执行的操作 end; * 调用存储过程 call 存储过程名(); * 删除存储过程 drop procedure 存储过程名;
十五、视图
create view 视图名 as 查询的SQL语句
十六、数据的导入导出
* 导入 source xxx.sql; * 导出 mysqldump -u root -p 数据库名[表名] > xxx.sql;🧐分享、点赞、在看,给个3连击呗!👇