Mysql基础操作速查

1.连接数据库,退出数据库
 

cd 到mysql安装路径E:\mysql\mysql-8.0.18-winx64\bin 目录下,mysql -uroot -p 输入root密码进入

eixt, quit, \q 退出数据库

2.创建数据库,删除数据库,修改数据库编码 ,查看数据库(crud),使用数据库

create database if not extists 数据库名称 charset utf8; create dateabase student;创建学生数据库

drop database 数据库名称; drop database student;

show databases;  查找全部数据库
select database() ; 查看当前使用的是哪个数据库

show databases like ‘pattern’ ; show databases like 'my%'模糊查询

%: 表示匹配多个

_: 表示匹配单个

数据库名称是不可以修改的,数据库的修改操作只限于对字符集和校对集的修改,而校对集依赖字符集。

alter database 数据库名称 charset gbk;

use 数据库名;切换数据库 use student;

3.表结构操作

1.创建表
 

Create table [if not exists] 表名(

字段名称  数据类型  [ COMMENT 字段1注释 ],

字段名称  数据类型,

字段名称  数据类型   -- 注意最后一行不要加逗号

)  [ COMMENT 表注释 ] charset UTF8;

create table if not exists student(id int not null primary key ,name varchar(11) not null,age int not null) charset utf8;

2.删除表

drop table 表名; drop table student;

3.修改表--表重命名,新增字段,删除字段,修改字段值,修改字段名
 

表重命名: rename table student to mystudent; 将表名从student 修改为mystudent

修改表的字符集 alter table mystudent charset gbk;

表字段修改,字段修改主要包括:新增,修改重名,删除,修改字段的属性值

新增字段:alter table student add column id int not null after age ;新增id字段在age字段前,还有first 表示最前边

修改字段名称: alter table student modify age varchar(10) after name; 将age字段的属性修改为varshar10并放在 那个字段后边。

重命名字段:alter table student change gender sex varcahr(10) first; 将gender字段名修改为sex 并放在最前边

删除字段名 alter  table student  drop sex;删除字段sex

4.查询数据表

show tables;

show tables like 'my%'

show create table student;查询建表语句

desc student;查看表结构

4.表数据操作

1.插入数据
 

insert into student (字段列表) values (值列表)

当字段列表不写时,默认填写所有字段,并且与建表字段顺序一致

eg :student 表有 id ,name ,age 字段

insert into student values(1,‘lilei’,10),(2,'wangwu',20);一次可以添加多个数据

insert into student (id,name) values(3,'lisi'),(4,'jack');

2.删除数据

delete from student where id=1;
删除id为1的表数据

3.修改表数据

update student set age=18 where id=1;修改id为1的用户的年龄为18

5.查询表数据

查询全部数据
select * from student;
根据条件查询
select * from student where sex=man;
查询name,age两个字段 
select name,age from student where sex=woman;
基本语法:select 字段列表/* from 表名 [where 条件];

完整语法:select [select选项] 字段列表[字段别名]/* from 数据源[where 条件子句] [group by条件子句] [having 子句] [order by 子句] [limit 子句];

select选项:select选项是值select对查出来的结果的处理方式,主要有两种。

All:默认的,保留所有的结果

Distinct: 对查询结果进行去重(将重复的给去除)

select all * from student;
select distinct * from student;


别名
select name as '姓名' from student;
where 子句
比较运算符:>, <, >=, !=, <>, =, like, between and, in, not in like is null。

逻辑运算符:&&(and),||(or), !(not)

select * from student where height between 165 and 180;
select * from emp where idcard is not null;
select * from emp where gender = '女' and age < 25;
select * from emp where name like '__';

LIKE 占位符 模糊匹配(_匹配单个字符, %匹配任意个字符)
group by 子句
SELECT 字段列表 FROM 表名 [ WHERE 条件 ] GROUP BY 分组字段名 [ HAVING 分组后过滤条件 ];
在SQL中分组是为了统计数据,SQL提供了一系列统计函数供我们使用。

1)      Count(): 统计分组后的记录数,每一组有多少记录

2)      Max(): 统计每组中最大值

3)      Min(): 统计最小值

4)      Avg(): 统计平均值。

5)      Sum(): 统计和

select gender,count(*),max(height),min(height),avg(age),sum(age) from students group by gender;
分组后统计性别种类,每个性别人数,最大身高,最低身高,平均年龄,年龄和
分组默认升序排序
asc ,desc

多个字段分组
select class,gender,count(*),group_contact(name) from student group by gender,class;

 where与having区别
执行时机不同:where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组
之后对结果进行过滤。
判断条件不同:where不能对聚合函数进行判断,而having可以


注意事项:
• 分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义。
• 执行顺序: where > 聚合函数 > having 。
• 支持多字段分组, 具体语法为 : group by columnA,columnB

GROUP BY X, Y意思是将所有具有相同X字段值和Y字段值的记录放到一个分组里,x和y必须相同,一个不同就不是一组的
having子句和order by字句,和where字句一样,是用来进行条件判断的。Where是针对磁盘数据进行判断,进入到内存之后,会进行分组操作,分组结果就需要Having来处理。有这么一个结论,having能做where能做的几乎所有事情,但是where却不能做having能做的事情。Order by主要就是用来排序操作。


求出所有班级人数大于等于2的学生人数
select class,count(*) from student group by class having count(*)>=2;
Having能够使用字段别名,where不能

       因为where是从磁盘取数据,而名字只可能是字段名,别名是在字段进入到内存之后才会产生。下面就是利用字段别名举例,total就是count(*)的别名

select class,count(*) as total from student group by class having total>=2;
排序,先按第一个字段排序,然后按第二个字段排序
ASC : 升序(默认值)
DESC: 降序
select * from student order by name desc;
select * from student order by name,age desc;
select * from emp order by age asc , entrydate desc;
 
limit子句
select * from student limit 10;查询前10条

select * from student limit 10,10;从第10条开始查询10条
内连接

select * from  student inner join class on student.classid=class.id;
两张表都有id和name字段,怎么区分哪个id是哪个表的呢?所以这个时候就需要使用字段别名

select s.id,s.name,c.id,c.name from student as s inner join class as c on s.classid=c.id;

select count (*) from emp; -- 统计的是总记录数
select count (idcard) from emp; -- 统计的是 idcard 字段不为 null 的记录数

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值