教程地址:https://www.bilibili.com/video/av19956343
mysql-5.7.17-winx64压缩版的安装包下载和安装配置:https://blog.csdn.net/qq_35164169/article/details/76566267
MySQL相关操作:
'''
一、基本命令:
以管理员身份运行cmd
1、启动服务的命令:net start 服务名称
2、停止服务:net stop 服务名称
3、连接数据库 :mysql -u 用户名 -p
4、 退出登录:quit 或者 exit
5、查看版本链接:select version();
6、显示当前的时间:select now();
7、远程连接:mysql -h <对方ip地址> -u <对方用户名> -p
输入对方密码
二、数据库操作:
1、创建数据库:create database 数据库名 charset=utf8;
2、删除数据库:drop database 数据库名;
3、使用数据库:use 数据库名;
4、查看当前数据库:select database();
三、表操作:
1、创建表:create table 表名(列及类型)
(id int auto_increment primary key,name varchar(20) not null,age int not null,gender bit default 1,address varchar(20),isDelete bit default 0);
说明:创建格式为 (列名 数据类型 (auto_increment表示自增长) (primary key 表示设置为主键) (not null 表示不能为空))
2、删除表:drop table 表名;
3、查看所有表:show tables;
4、查看表结构:desc 表名;
5、查看建表语句:show create tabel 表名;
6、重命名表:rename table 表名 to 新表名;
7、修改表格式:alter table 表名 add或change或drop 列名 类型;
四、数据操作:
1、添加数据:
a、全列插入:
insert into 表名 values(列以及类型);
注意:主键自动增长,但是在全列插入的时候通常需要使用0占位
b、缺省插入:
insert into 表名 (列...) values(值...);
c、插入多条数据:
insert into 表名 vlaues()()()...;
2、删除数据:
delete from 表名 where 条件 ;
注意:没有条件会删除所有数据
3、修改数据:
update 表名 set 列=值,....... where 条件;
五、查找数据:
1、全部查找:select * from 表名;
说明:1、from 后面的表名,是数据来源
2、select 后面是表中的列名。*表示全部列
3、在select 引用列名有可以使用as为列起别名。
4、引用多个列的时候列之间要用","隔开
2、消除重复行:select distinct 列名 form 表名;
3、条件查找:select * from 表名 where 条件;
a、比较运算符:
大于 >
小于 <
等于 =
大于等于 >=
小于等于 <=
不等于 != 或者<>
b、逻辑运算符:
and
not
or
c、模糊查询:like
selecte * from 表名 where 列名 like ‘%_’
%表示任意多个字符
_表示一个任意字符
d、范围查找:
in 表示一个非连续的范围内
between ...and ...表示在一个连续的范围内
select * from 表名 where 列名 in (相关数据);
select * from 表名 where 列名 between (相关数据)and (相关数据);
e、空判断:
判断是空:is null
判断非空:is not null
select * from 表名 where 列名 is null;显示所有列是空的数据行
f、优先级
() not 比较运算 逻辑运算
and 比 or 优先级高
4、聚合:
为了快速得到统计数据提供了。五个聚合函数。
1、count(*) 计算总行数,括号中可以写*或者列名
2、max(列) 求此列的最大值
3、min(列) 求此列的最小值
4、sum(列) 求此列的和
5、avg(列) 求此列的平均值
select count(*) from 表名;
5、分组
按照字段分组,表示此字段相同的数据会被放到一个集合中。分组后只能查询相同数据的数据列,对于有差距的数据列无法显示有几种。可以对分组后的数据进行统计,然后分组运算。
语法:select 列1,列2,聚合 ...from 表名 group by 列1,列2,列3... having 列1,列2,...,聚合;
6、排序
语法:select * from 表名 order by 列1 asc|desc,列2 asc|desc......
说明:
1、将数据按照列1进行排序,如果某些列1的值相同按照列2进行排序,以此类推。
2、默认从小到大排序
3、acs升序
4、desc降序
7、分页
语法:select * from 表名 limit start,count;
说明:
start 索引从0开始
count表示观看数量
六、关联
外键去关联其他的表。
建表语句:
1、班级表:
creat table class(id int auto_increment primary key ,name varchar(20) not null ,stuNum int not null);
2、学生表:
creat table students(id int auto_increment primary key ,name varchar(20) not null ,stuNum int not null,gender bit default 1,classid int not null,foreign key(classid) references class(id));
插入一些数据:
insert into class values(o,'python01',55),(o,'python02',60),(o,'python03',65),(o,'python04',70),(o,'python05',75),(o,'python06',80)
insert into students values(0,'tom',1,1);
insert into students values(0,'lilie',1,10);#class外键没有10会报错
select students.name,class.name form class inner join students on class.id=students.classid;
分类:
1、表A inner join 表B
表A与表B匹配的行会出现在结果集中
2、表A left join 表B:
表A与表B匹配的行会出现在结果集中,外加表A中独有的数据,未对应的数据使用null填充
3、表A right join 表B:
'''