在windows系统上搭建MySQL

先去官网上下载mysql安装包 👉👉👉 前往下载

下载完解压安装包进行安装

按着下面图片安装即可

在这里插入图片描述

然后运行DOS命令进行测试

先链接数据库

– 链接数据库
mysql -uroot -pmysql

-- 不显示密码
mysql -uroot -p
mysql

注:-u 后面的是mysql的账号
-p 后面的是mysql密码

在这里插入图片描述之后便可进行数据库操作了。

数据库基本操作:

1.select version();     ------查看版本号

2.select now();           -----显示时间

3.show databases;     ----查看所有数据库

4.create database 数据库名 charset=utf8;     ----创建数据库

5.show create database 数据库名     —查看创建数据库的语句

6.use 数据库名     —使用数据库

7.drop database 数据库名;     — 删除数据库

8.exit/quti/ctrl + d      ----退出数据库

数据表的操作

1.show tables;     —查看所有表

2.创建表
– int unsigned 无符号整形
– auto_increment 表示自动增长
– not null 表示不能为空
– primary key 表示主键
– default 默认值
– create table 数据表名字 (字段 类型 约束[, 字段 类型 约束]);
– unique 唯一的

create table xxx(
		id int unsigned primary key not null auto_increment,
		name varchar(20) not null
	);

3.desc     -----查看表结构

4.创建class表(id,name)

create table classes(
		id int unsigned primary key auto_increment not null,
		name varchar(20) not null
	);
	

5.创建 students 表(id、name、age、high (decimal)、gender (enum)、cls_id)

create table students(
		id int unsigned primary key auto_increment not null,
		name varchar(20) not null,
		age int unsigned,
		high decimal(5,2),
		gender enum("男性","女性","中性","保密") default "保密",
		cls_id int unsigned
	);

6.show create table xxx;      —查看创建表语句

***关于表的增删改查 ***

单条插入

insert into classes values(1,"classroom");

批量插入

insert into classes values(2,"老李"),(3,"赵四");

修改

update students set high = 170.00;
		-- 按条件修改
		update students set high = 170.00 where id=3;
		-- 按条件修改多个值
		-- update students set gender ="",name = "xxx" ;
		update students set gender="中性",name="111" where id=3;

查询

– 查询基本使用
– 查询所有列
– select * from 表名;
select * from students;

    -- 定条件查询
   select * from students where id=1;


    -- 查询指定列
    -- select 列1,列2,... from 表名;
    select name,gender from students;


    -- 可以使用as为列或表指定别名
    -- select 字段[as 别名] , 字段[as 别名] from 数据表;
   select name as "姓名",gender as "性别" from students;


    -- 字段的顺序
    select gender as "性别",name as "姓名" from students;

– 删除
– 物理删除
– delete from 表名 where 条件
delete from students where id=3;
– 逻辑删除
– 用一个字段来表示 这条信息是否已经不能再使用了
– 给students表添加一个 is_delete 字段 bit 类型
alter table students add is_delete bit default 0;
update students set is_delete=1 where id = 1;

  1. – 修改表-添加字段 mascot (吉祥物)
    – alter table 表名 add 列名 类型;
alter table classes add chongwu varchar(20) default"蛇";
-- 修改表-修改字段:不重命名版
-- alter table 表名 modify 列名 类型及约束;
alter table classes modify mascot varchar(30) default"葫芦娃";
-- 修改表-修改字段:重命名版
-- alter table 表名 change 原名 新名 类型及约束;
alter table classes change chongwu mascot varchar(20) default"法拉利";
-- 修改表-删除字段
-- alter table 表名 drop 列名;
alter table classes drop mascot;

– 查询练习

	-- 查询所有字段
	-- select * from 表名;
	select * from students;

	-- 查询指定字段
	-- select 列1,列2,... from 表名;
	select name,gender from students;
	
	-- 使用 as 给字段起别名
	-- select 字段 as 名字.... from 表名;
	select name as "姓名",gender as "性别" from students;

	-- select 表名.字段 .... from 表名;
	select name as "姓名",gender as "性别" from students as s;

	
	-- 可以通过 as 给表起别名
	-- select 别名.字段 .... from 表名 as 别名;
	select s.name as "姓名",s.gender as "性别" from students as s;
	
	失败的select students.name, students.age from students as s;
	

	-- 消除重复行(查性别)
	
	-- distinct 字段 
	select gender from students;
	select distinct gender from students;

– 条件查询

-- 比较运算符
		-- select .... from 表名 where .....
		-- >
		-- 查询大于18岁的信息
		select * from students where age > 18;

		-- <
		-- 查询小于18岁的信息
		select * from students where age < 18;
		

		-- >=
		-- <=
		-- 查询小于或者等于18岁的信息
		select * from students where age <= 18;
		-- =
		-- 查询年龄为18岁的所有学生的名字
		(区别)select * from students where age = 18;


		-- != 或者 <>
		select * from students where age != 18;
-- 逻辑运算符
-- and
		-- 18和28之间的所以学生信息
		select * from students where age >=18 and age <=28;
		
		失败 select * from students where age>18 and <28;


		-- 18岁以上的女性
		select * from students where age > 18 and gender = 2;

		-- or
		-- 18以上或者身高高过180(包含)以上
		select * from students where age > 18 or height >= 180;


		-- not
		-- 不在 18岁以上的女性 这个范围内的信息
		-- select * from students where not age>18 and gender=2;
		select * from students where not (age > 18 and gender = 2)

;

-- 模糊查询(where name like 要查询的数据)
-- like 
		-- % 替换任意个
		-- _ 替换1个
		-- 查询姓名中 以 "小" 开始的名字
		select * from students where name like "小%";
		

		-- 查询姓名中 有 "小" 所有的名字
		select * from students where name like "%小%";

		-- 查询有2个字的名字
		select * from students where name like "__";
		

		-- 查询有3个字的名字
		select * from students where name like "___";
		

		-- 查询至少有2个字的名字
		select * from students where name like "__%";
-- 范围查询
-- in (1, 3, 8)表示在一个非连续的范围内
		-- 查询 年龄为18、34的姓名
		select * from students where age =18 or age = 34;
		select * from students where age in (18,34);
		
		-- not in 不非连续的范围之内
		-- 年龄不是 18、34岁之间的信息
		select * from students where age not in (18,34);
		
		(注意)select name from students where not age in (18,34);


		-- between ... and ...表示在一个连续的范围内
		-- 查询 年龄在18到34之间的的信息
		select * from students where age between 18 and 34;
		
		-- not between ... and ...表示不在一个连续的范围内
		-- 查询 年龄不在在18到34之间的的信息
		select * from students where age not between 18 and 34;
		
		失败的select * from students where age not (between 18 and 34);
-- 空判断
	-
- 判空is null
		-- 查询身高为空的信息
		select * from students where height is null;
		
		-- 判非空is not null
		select * from students where height is not null;
		
		失败select * from students where height not is  null;

– 排序

-- order by 字段
	-- asc从小到大排列,即升序
	-- desc从大到小排序,即降序

	-- 查询年龄在18到34岁之间的男性,按照年龄从小到大到排序
	select * from students where (age between 18 and 34) and gender = 1 order by age asc;
	-- 查询年龄在18到34岁之间的女性,身高从高到矮排序
	select * from students where (age between 18 and 34) and gender = 2 order by height desc;
	

	-- order by 多个字段
	-- 查询年龄在18到34岁之间的女性,身高从高到矮排序, 如果身高相同的情况下按照年龄从小到大排序
	select * from students where (age between 18 and 34) and gender = 2 order by height desc,age asc;

	-- 查询年龄在18到34岁之间的女性,身高从高到矮排序, 如果身高相同的情况下按照年龄从小到大排序,
	-- 如果年龄也相同那么按照id从大到小排序
	select * from students where (age between 18 and 34) and gender = 2 order by height desc,age asc,id desc;

– 聚合函数

-- 总数
	-- count
	-- 查询男性有多少人,女性有多少人
	select count(*) from students where gender = 1;
	select count(*) from students where gender = 2;

	-- 最大值
	-- max
	-- 查询最大的年龄
	select max(age) from students;

	-- 查询女性的最高 身高
	select max(height) from students where gender = 2;

	-- 最小值
	-- min
	select min(height) from students where gender = 2;
	
	-- 求和
	-- sum
	-- 计算所有人的年龄总和
	select sum(age) from students;

	
	-- 平均值
	-- avg
	-- 计算平均年龄
	select avg(age) from students;


	-- 计算平均年龄 sum(age)/count(*)
	


	-- 四舍五入 round(123.23 , 1) 保留1位小数
	-- 计算所有人的平均年龄,保留3位小数
	select round(avg(age),3) from students;

	-- 计算男性的平均身高 保留2位小数
	select round(avg(height),2) from students where gender = 1;

– 分组

-- group by
-- 按照性别分组,查询所有的性别
select gender from students group by gender;
-- select name,gender from students group by gender;
-- 失败select * from students group by gender;
select * from students group by gender;
-- 计算每种性别中的人数
select count(*),gender from students group by gender;

-- group_concat(...)
-- 查询同种性别中的姓名
select group_concat(name),gender from students group by gender;

-- 查询每组性别的平均年龄
select avg(age),gender from students group by gender;

-- having(注意having和group by)
(!)-- 查询平均年龄超过30岁的性别,以及姓名 having avg(age) > 30
select gender,group_concat(name) from students 
group by gender 
having avg(age) > 30;

-- 查询每种性别中的人数多于2个的信息
select gender,group_concat(name) from students group by gender having count(*) > 2;

-- with rollup 汇总的作用(了解)
select gender,count(*) from students group by gender with rollup;

– 分页

-- limit start, count
	
	-- 限制查询出来的数据个数
	-- 查询前5个数据
	select * from students limit 5;
	
	-- 每页显示2个,第1个页面
	select * from students limit 0,2;

	-- 每页显示2个,第2个页面
	select * from students limit 2,2;

	-- 每页显示2个,第3个页面
	select * from students limit 4,2;

	-- 每页显示2个,第4个页面
	select * from students limit 6,2;
	

	-- 每页显示2个,显示第6页的信息, 按照年龄从小到大排序(limit 必须写到sql语句的最后)
	错误1 select * from students limit 10,2 order by age asc;
	
	-- 工作错误的写法
	错误2 select * from students limit 2*(6-1),2;
	
	-- limit 放在最后面(注意)

– 连接查询

-- inner join ... on
-- select ... from 表A inner join 表B;
select * from students inner join classes;

-- 查询 有能够对应班级的学生以及班级信息
select * from students inner join classes on students.cls_id = classes.id;

-- 按照要求显示姓名、班级
select students.name,classes.name from students inner join classes on students.cls_id = classes.id;

-- 给数据表起名字
select s.name,c.name from students as s inner join classes as c on s.cls_id = c.id;

-- 查询 有能够对应班级的学生以及班级信息,显示学生的所有信息 students.*,只显示班级名称 classes.name.
select s.*,c.name from students as s inner join classes as c on s.cls_id = c.id;

-- 在以上的查询中,将班级姓名显示在第1列
select c.name,s.* from students as s inner join classes as c on s.cls_id = c.id;

-- 查询 有能够对应班级的学生以及班级信息, 按照班级进行排序
-- select c.xxx s.xxx from students as s inner join clssses as c on .... order by ....;
select c.name,s.* from students as s inner join classes as c on s.cls_id = c.id order by c.name;

-- 当时同一个班级的时候,按照学生的id进行从小到大排序
select c.name,s.* from students as s inner join classes as c on s.cls_id = c.id order by c.name,id asc;

-- left join
-- 查询每位学生对应的班级信息
select * from students left join classes on students.cls_id = classes.id;
-- select * from students right join classes on students.cls_id = classes.id;

-- 查询没有对应班级信息的学生
-- select ... from xxx as s left join xxx as c on..... where .....
-- select ... from xxx as s left join xxx as c on..... having .....
select * from students left join classes on students.cls_id = classes.id where classes.name is null;

(注意)不建议使用 select * from students left join classes on students.cls_id=classes.id having classes.id is null;

-- right join   on
-- 将数据表名字互换位置,用left join完成

– 子查询
– 标量子查询: 子查询返回的结果是一个数据(一行一列)
– 列子查询: 返回的结果是一列(一列多行)

– 行子查询: 返回的结果是一行(一行多列)

-- 查询出高于平均身高的信息(height)
-- 1 查出平均身高
select avg(height) from students ; -- 172
-- 2 查出高于平均身高的信息
select * from students where height >(select avg(height) from students) ;

-- 查询学生的班级号能够对应的 学生名字
-- select name from students where cls_id in (select id from classes);
-- 1 查出所有的班级id
select id from classes; -- 1,2
-- 2 查出能够对应上班级号的学生信息
select * from students where cls_id in (select id from classes);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值