MySQL学习笔记

: : 这是一篇十分详细的MySQL学习笔记

一、运行MySQL的相应命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
##运行MySQL的相应命令
#启动mysql
systemctl start mysqld

#查看状态
systemctl status mysqld

#开机启动
systemctl enable mysqld

#查看密码
grep 'password' /var/log/mysqld.log

#登录MySQL的root账号
mysql -u root -p

二、修改MySQL密码策略:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
##修改MySQL密码策略
## 临时有效,登录MySQL后输入相应命令
# 修改策略为0,即关闭MySQL的密码策略
set global validate_password_policy=0;

# 修改密码长度
set global validate_password_length=1;

#修改密码
alter user root@"localhost" identified by "密码";
#虚拟机修改密码为root

## 永久生效,在Linux系统中修改配置文件
vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=1

三、MySQL的基本使用:

1、查看所有数据库

1
2
3
4
5
#查看所有数据库 ps:所有命令应当用英文;结尾
show databases;

#查看表的引擎
show create table '表名';

2、创建自己的数据库

1
2
3
#创建自己的数据库
create database "库名" default charset="默认编码";
create database school default charset=utf8;

3、进入相应库

1
2
#进入相应库
user "库名";

4、查看目前在哪个数据库

1
2
#查看目前在哪个数据库
select database();

5、创建数据表

1
2
3
#创建数据表
create table "表名" (字段1 字段类型,字段2 字段类型....) 默认编码;
create table t1 (id int,user char(长度)) default charset=utf8;

6、查看所有的数据表

1
2
#查看所有的数据表
show tables;

7、查看数据表有哪些字段

1
2
#查看数据表有哪些字段
desc "表名";

8、添加数据

1
2
3
4
5
6
##添加数据
#单条添加
insert into "表名" (字段1,字段2...) values (字段1的值,字段2的值...);

#多条添加
insert into "表名" (字段1,字段2...) values (字段1的值,字段2的值...),(字段1的值,字段2的值)...;

9、查看数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#查看数据
select * from "表名";

#查看十年后的年龄
select uname,age+10 from stu;

#给字段起别名
select uname,age+10 as 00xx from stu;
select uname,age+10 00xx from stu;

#连接字段合并输出
select uname,concat(sex,'===',age) from stu;

#between 15 and 20; 介于15到20之间
select * from stu where age between 15 and 20 && sex='女';

#not between 20 and 30; 不介于20到30之间
select * from stu where age not between 20 and 30 && sex='女';

#in (2,7,34,49,81);在2,7,34,49,81中
select * from stu where uid in (2,7,34,49,81);

#not in(20-30);不介于20到30之间
select * from stu where age not in(20-30) && sex='女';

#查询姓孙的人,%代表任意字符
select * from stu where uname like '孙%';

#查询名称为两个字的人,_代表一个字符
select * from stu where uname like '__';

10、修改指定数据

1
2
3
#修改指定数据
update "表名" set 字段="修改内容" where 条件;
update stu set username='tomme' where id=2;

11、删除某些内容

1
2
3
4
5
6
7
8
9
#删除指定数据
delete from "表名" where 条件;
delete from stu where id=2;

#删除指定数据表
drop table "表名";

#删除指定数据库
drop database "库名";

12、模式设置

1
2
#设置为宽松模式
set sql_mode=ANSI;

13、添加用户并且设置权限

1
2
#添加用户并设置权限和密码
grant 具体权限 on 库名.表名 to '新建用户名'@'%' identified by '用户密码';

四、数据库条件语句:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
##数据库条件语句
#给字段起别名
#列出名字和100年后的年龄并起别名为ages
select "字段","字段"+或-数字 as "字段别名" from "表名";
select uname,age+100 as ages from stu;

#列合并
#列出名字,并通过concat合并性别与年龄(中间用====连接)
select "字段",concat("字段","自定义字符","字段"...) "字段别名" from "表名";
select uname,concat(sex,'======',age) ages from stu;

#介于x与y之间
select * from "表名" where "字段" between x and y;
select * from stu where age between 15 and 30;

#查找在字段里面特定的值
select * from "表名" where "字段" in (值1,值2...);
select * from stu where uid in (105,117,128);

#查找不在字段里面特定的值
select * from "表名" where "字段" not in (值1,值2...);
select * from stu where uid not in (105,117,128);

五、字符查找:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
##字符查找
#查找名字最后是“文”的人
select * from stu where uname like '%文';

#查找名字里有“文”的人
select * from stu where uname like '%文%';

#查找名字姓为“文”的人
select * from stu where uname like '文%';

#查找名字为n个字的人(n个下划线)
select * from stu where uname like '__';

#数据统计
select count(*) from stu where age=20;

六、MySQL数学函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
##MySQL数学函数
#总计
sum()

#最大值
max()

#最小值
min()

#平均值
avg()

#统计
count()

#四舍五入,小数点后一位
round()

#向下取整
floor()

#向上取整
ceil()

#查询年龄最大的人并显示
select * from stu where age=(select max(age) from stu);

#统计每个班男生女生有多少人。用group by分组
select classid,sex,count(*) from stu group by classid,sex;

#列出分组人数为20以上的
select classid,count(*) from stu group by classid having count(*)>20;

七、字段约束:

unsigned 无符号
zerofill 前导零填充配合int(长度)使用
primary key 主键索引且唯一
unique 唯一
auto_increment 自增
int(4) 数据宽度
default 默认值
enum(w,m,x) 多选一,添加值为其中的值,否则返回为空
set(c,t,r) 多选多

八、数据库导出数据:

1
2
3
4
5
6
7
#在cmd窗口执行命令
#导出数据表
mysqldump -u root -p '库名' '表名' > '导出的文件名.sql'
#导出数据库
mysqldump -u root -p --databases '库名' > '导出的文件名.sql'
#导出所有数据库
mysqldump -u root -p --all-databases > '导出的文件名.sql'

九、导入数据表:

1、登录MySQL

2、选择数据库

3、导入,输入命令:source stu表的路径

1
2
3
4
#在cmd窗口执行命令
mysql -u root -p < '要导入的文件名(一般为数据库备份文件)'

mysql -u root -p '已存在的库名' < '要导入的文件名(一般为数据表备份文件)'

十、MySQL关键字与优先级:

1、条件关键字

1
2
3
4
where

#查询user表下年龄为18的人并将信息显示出来
select * from user where age=18;

2、分组关键字,常与count()连用

1
2
3
4
group by

#统计users表中的男女总数并分组显示
select sex,count(*) from users group by sex;

3、排序关键字

1
2
3
4
5
6
7
order by

#将users表中的信息按照ID升序排列显示
select * from users order by id;

#将users表中的信息按照ID降序排列显示
select * from users order by id desc;

4、分页显示关键字

1
2
3
4
limint

#查询users表中数据,并分页显示第x行的后y行(即显示:x+1行 - x+y行,共y行)
select * from suers limit 0,1;

5、优先级

1
2
3
4
5
#优先级依次递减,where优先级最高例如:	

select * from where group by;(可行)

select * from group by where;(不可行)

十一、MySQL数据库查看相关信息命令:

1、查看data文件路径

1
select @@datadir;

2、查看数据库文件路径

1
select @@basedir;

3、查看数据库版本

1
2
3
select @@version;

select version();

4、查看操作系统

1
select @@version_compile_os;

5、查看当前库

1
select database();

6、查看用户

1
2
3
4
5
6
7
8
9
10
11
#查看用户
select user();

#查看当前用户
select current_user();

#查看系统用户
select system_user();

#连接数据库的用户名
select session_user();