https://blog.csdn.net/weixin_44051608/article/details/85163823
一、启动MySQL
1、 win+R或者CMD中输入“services.msc”打开服务,在里面找到MYSQL服务进行控制

2、cmd下我们输入 net stop mysql80 来禁用MYSQL服务

3、cmd下我们输入 net start mysql80 来启用MYSQL服务

二、连接MySQL服务器
- 连接本地MySQL
mysql -uroot -p

2、指令集
连接好后我们输入 \h 来查看一下命令集:

3、退出:
\q
exit
quit


4、连接远程MySQL
mysql -h127.0.0.1 -uroot -p

或者使用:
mysql --host=127.0.0.1 --user=root --password

- 查看mysql端口
show global variables like 'port';
三、MySQL目录结构
1、安装目录

2、数据目录


数据库:文件夹
表:文件夹中的文件
数据记录:文件内存储的内容
四、SQL通用语法
1、SQL可以单行或多行书写,以分号结尾。遇到分号之前随意换行

2、使用空格或者Tab提高可读性
3、不区分大小写,但关键字推荐用大写
4、单行注释 :① -- (两个横杠一个空格)② # (mysql特有)
5、多行注释:/* 。。。。 */
五、MySQL数据类型
1、数值类型

double(5,2) 表示总共5位,小数点后2位;
2、日期和时间类型

如果使用TIMESTAMP类型,则如果不赋值或者赋值为NULL,则默认保存系统当前时间。
3、字符串类型

varchar(20) 表示最大20个字符
六、SQL分类
1 DDL :用来定义数据库对象:数据库、表、列等;关键字:create drop alter
2 DML:用来对数据库表中的数据进行增删改;关键字:insert delete update
3 DQL:用来查询数据库表中的记录;关键字:select where
4 DCL:定于数据库的访问权限和安全级别及创建用户 关键字:GRANT REVOKE

七、DDL操作:数据库
1、创建
①、创建数据库 create database 数据库名;
create database db1;

②、防止重复创建 create database if not exists 数据库名;
create database if not exists db1;

③、指定字符集创建 create database 数据库名 character set 字符集名;
create database db3 character set gbk;

④、判断存在否并指定字符集:create database if not exists 数据库名 character set 字符集名;
create database if not exists db4 character set gbk;

2、查询
①、show databases; 显示所有数据库
show databases;

数据库对应文件夹:

其中information_schema数据库是特殊视图,并没有对应的物理文件。
mysql对应的是核心数据库
performance_schema对应的是性能数据库
以上数据库不要乱动。
②、查询创建某数据库时使用的语法(主要为了查看字符集)
show create database 数据库名;
show create database mysql;

3、修改
①、修改字符集:alter database 数据库名 character set 字符集名;
alter database db4 character set utf8;

4、删除
①、删除数据库:drop database 数据库名;
drop database db3;

②、先检查有没有再删除:drop database if exists 数据库名;
drop database if exists db3;

5、使用
① 查询当前正在使用的数据库名称:select database();
select database();

②、使用数据库: use 数据库名;
use db1;

八、DDL操作:表
1、创建
①、创建表:create table 表名(列名1 数据类型1,列名2 数据类型2,...,列名n 数据类型n);
create table student(id int,name varchar(32),age int,score double(4,1),brithday date,insert_time timestamp);

完成后查一下:

2、查询
①、查询表集合: show tables;
show tables;

②、查询表结构: desc 表名;
desc student;

3、修改
①、修改表名
alter table 表名 rename to 新表名;
alter table student rename to students;

②、修改表的字符集
alter table 表名 character set 字符集名;
alter table students character set utf8;

③、添加一列
alter table 表名 add 列名 数据类型;
alter table students add sex varchar(10);

④、修改一列
alter table 表名 change 列名 新列名 新数据类型;
alter table students change sex gender varchar(20);

⑤、只修改列的类型
alter table 表名 modify 列名 新数据类型;
alter table students modify gender varchar(10);

⑥、删除列
alter table 表名 dorp 列名;
alter table students drop gender;

4、删除
drop table 表名;
drop table students;
drop table if exists 表名;
drop table if exists students;
九、一个图形化软件mysql workbench
使用图形化软件来更加直观方便的操作数据库,现在MySQL下载会自带
连接数据库后如图:

数据库在哪?

如何直观的看到表格?


如何操作数据库?
全部执行:

快捷键 ctrl +enter
单步执行:

如何图形化为表中添加数据?


创建RE图:

一路next,选择自己的数据库

十、DML
1、添加数据
①、添加数据
insert into 表名(列名1,列名2.。。。列名n) values (值1,值2,。。。,值n);
insert into stu(id,name,nianling,score,sex) values(1,'范冰冰',18,59,'女');
②、如果给所有列添加值,可以不写列名;
insert into stu values(2,'佟丽娅',24,99,null,null,'优秀','女');
注意:除了数字类型,其他类型需要使用单/双引号 引起来。

2、删除数据
如果MySql运行在safe-updates模式下,该模式会导致非主键条件下无法执行update或者delete命令。输入如下命令退出此模式:
SET SQL_SAFE_UPDATES = 0;
①删除数据
delete from 表名 where 条件;

②、删除所有数据
delete from 表名;

③、删除整个表
truncate table 表名;
3、修改数据
①、修改数据
update 表名 set 列名1=值1,列名2=值2,......列名n=值n,where 条件;

注意:1、执行此命令同样需要退出SAFE_UPDATES模式
SET SQL_SAFE_UPDATES = 0;
2、如果不加where就会把表里的数据都改了
十一、DQL
1、语法
select
字段列表
from
表名列表
where
条件列表
group by
分组字段
having
分组之后的条件限定
order by
排序
limit
分页限定
2、基础查询
①、多个字段查询
select 列名1,列名2 from 表名;

②、去除重复
SELECT DISTINCT 列名1,列名2 from 表名;

③、计算列
SELECT 列名1+列名2 from 表名;

上面例子中柳青的成绩相加不对,原因处在null上,所以应引入ifnull判断

附:判断null ifnull(列名,替换值); 当列名出现null时替换为替换值
④、起别名
select 字段名 as 别名 from 表名; #as也可以用空格代替

起别名后任何位置都可以用别名代替此名。
3.条件查询
①、where后跟条件

②、运算符
< >= <= = != <>(这个也是不等于)
....(1) and 并且
select id,name from student where age>10 and age <20;
....(2) between 条件1 and 条件2
select id,name from student where age between 10 and 20;
....(3) or 或者
select id,name from student where score>90 or age<10;
....(4) in (22,18,15); #在。。。中
select id,name from student where name in ("西施","貂蝉","王昭君");
....(5) is null //判断是null
select id,name from student where name is null;
....(6) is not null //判断不是null
select id,name from student where name is not null;
注意:null不能用 =来判断 需要用 is来判断

③、LIKE 模糊查询运算符
占位符:_单个任意字符 %多个任意字符


4.排序查询
①语法
order by 排序字段1 排序方式1,排序字段2 排序方式2,。。。。
其中排序方式: ASC:升序(默认) DESC:降序


5、聚合函数
将一列数据按照一个整体进行计算 #列的纵向计算
①、count 计算个数
select count(列名) from 表名;

②、max 最大值
select max(列名) from 表名;

③、min
select min(列名) from 表名;
④、sum
select sum(列名) from 表名;

⑤、avg
select avg(列名) from 表名;

注意:聚合函数会排除null
6、分组查询
①、语法
group by 列名;

注意:分组后应该查询 分组或聚合函数才有意义。
②、分组加条件where

③、分组加条件where,完成分组后再加having筛选

where和having的区别:
-where在分组前限定,having再分组后限定
-where后不可以跟聚合函数 having后可以
④、分页查询
limit 开始的索引 ,每页查询的条数;
开始的索引=(当前页码-1)*3

注意:分页操作是一个“方言”,只能用于MySQL。
十二、约束
约束是对输入数据库的数据进行限定,为了保证数据的正确性、有效性、完整性!
1、主键约束 primary key
主键表示非空且唯一,每张表只能有一列主键,主键是记录的唯一标识
①、创建时约束

②、追加约束
alter table stu modify id int PRIMARY KEY;
③、删除约束
alter table stu drop primary key;
④、主键自动增长
如果一个列是数值类型的,那么可以使用auto_increment 让其自动增长,一般配合主键来用。

注意:自动增长只与上一条数据有关系
⑤、追加主键自动增长
alter table stu modify id int AUTO_INCREMENT;
⑥、删除主键自动增长(其实就是修改列的数据类型)
alter table stu modify id int;
2、非空约束 not null
①、创建时约束

②、追加约束(其实就是修改列的数据类型)
alter table stu modify name varchar(10) NOT NULL;
③、删除约束 (其实就是修改列的数据类型)
alter table stu modify name varchar(10);
3、唯一约束 unique
①、创建时约束

②、追加约束(其实就是修改列的数据类型)
alter table stu modify phone varchar(10) unique;
③、删除约束 #此语法有些特殊
alter table stu drop index phone;
注意,唯一并不限定null
4、外键约束 foreign key
外键:让表格中的一列数据关联其他表的数据
①、创建时约束
create table 表名(
。。。。
外键列,
constraint 外键名称 foreign key(要连接外键的列名) references 主表名称(主列表名称)
);

②、追加约束
alter table 表名 add constraint 随意取一个外键约束名 foreign key (本表列名) references 外键(外键列名);
③、删除约束
alter table stu drop foreign key 外键名;
④、级联更新
设置级联更新 来确保被外键数据改动,本表的关联外键的数据一起改动
在外键后添加级联ON UPDATE CASCADE

此时修改外键数据,本表数据会随之改变

⑤、级联删除
在外键后添加级联ON DELETE CASCADE
网友评论