目录
MySql是关系型数据库:采用了关系模型来组织数据的数据库
MySQL数据类型
int
- double
- char(n) 定长字符串,不管存几个字符,长度就设定为n,不够长的话,后面用空格补全(0-255)
适合存储数据长度固定的数据(身份证号),不会浪费空间,效率略高
- varchar(n) 不定长字符串,长度也是n,只不过不够长的话,后面剩余的还给数据库,留给别的数据(0-65535)
效率略低,也不会浪费空间
- text 65535个字符
- bigtext 4G
- 日期类型
date 年月日
time 时分秒
datetime 年月日时分秒
timestamp 时间戳 1970年到指定日期之间的毫秒值,可以自动插入和更新
数据库命令
首先打开命令行窗口
连接数据库 mysql -u root -p mysql8.0+版本的,修改密码 alter user 'root'@'localhost'identified by '123456' 退出 exit
注意:修改默认产生的随机密码,打开mysql安装目录下,data目录下的.err文件,找到A temporary password is generated for root@localhost: *********。此为随机密码,进行修改
查询当前所有库
show databases;
查询当前数据库中所有表
第一步 use mysql //这一步不用分号
第二步 show tables;
创建数据库
create database 数据库名字 character utf8; //创建utf-8编码格式的数据库
create database if not exists 数据库名字 character utf8; //如果该数据库不存在,则创建utf-8编码格式的数据库
删除数据库
drop database if exists 数据库名字 //如果数据库存在就删除
drop database if not exists 数据库名字 //则创建该数据库
创建表
use 数据库名;
id name age 1 小明 14 2 小华 16 create table 表名(
id int,
name varchar(20),
age int
);
表建好之后,添加新的列alter,删除列
在表建好之后,插入新的列 alter table 表名 add 列名 varchar(20); 在表建好之后,删除列 alter table 表名 drop 列名;
查看表结构
desc 表名;
设置约束
主键约束
把某一列设置为主健,该列不能重复且不能为空,作为表记录的唯一标识
create table 表名(
id int primary key, //设置主键约束
name varchar(20),
age int
);
唯一约束
如果为一个列添加唯一约束,该列的值就不能重复,可以为空
create table 表名(
id int unique, //设置唯一约束
name varchar(20),
age int
);
非空约束
如果为一个列添加非空约束,该列的值不能为空,可以重复
create table 表名(
id int not null, //设置非空约束
name varchar(20),
age int
);
外键约束
指定主键自增 auto_increment
create table student(id int primary key auto_increment,name varchar(20),age int);
auto_increment自增,如果我们在插入数据的时候,传null,则从1开始,前提是数据类型是int
例如 insert into student(id,name,age)values(null,'小明',24);
插入数据
insert into 表名(id,name,age)values(12033101,'小明',24);
修改数据
update 表名 set age=26 where name='小明';
- where条件语句,如果不加where,那么所有的数据的age都会被设置为26
如果某一列是数值类型的,比如int,double,这时候可以对该列全部加或减操作
update 表名 set 列名=列名+数值; 例如update people set sal=sal+8000;
删除所有数据
delete from 表名;
- 删除所有数据之后,那设置的自增的主键是不会重制的,假如原来主键从1到3,删除了所有数据之后,主键会从4开始;
- 如果想清空表,并且把主键也清除,要这样写 truncate 表名;
查询表
基础查询
select * from 表名;
或者写成
select id,name,age from student;
注意:*也是转换成所有列,相对来说,下面的写法效率更高
查询表中所有数据,所有列 select name,age from student;
查询表中所有数据,指定列 select name,age from student where gender='男'; 根据条件,查询表中所有数据,指定列 select name,gender from people where sal>=20000 and sal<=38000;
上面的语句等价于
select name,gender from people where between 20000 and 38000;
根据区间条件,查询表中所有数据,指定列 select name,gender from people where sal>20000 or sal<38000;
根据区间条件,查询表中所有数据,指定列 select * from people where salary in(3000,2000);
select * from people where salary not in(3000,2000);
查询特定数值的数据
查询除了特定值以外的数据
select * from people where bonus is null;
select * from people where bonus is not null;
查询某一列的值为null的数据
查询某一列的值不为null的数据
select distinct *from student;
distinct去除重复数据 关于null值
假如我查询的时候,发现某条数据有null值,这时候是查不到他的
例如:select name,salary+bonus from people where salary+bonus>0;这时候查询到的数据
这时候就要通过 ifnull进行列判断 ifnull(标值,替换值)
select name,salary+ifnull(bonus,0) from people where salary+ifnull(bonus,0)>0;
as起别名
select name,salary+ifnull(bonus,0) as 总薪资 from people where salary ull(bonus,0)>0;
模糊查询 like
%表示0或多个任意字符
_下划线表示一个任意的字符,查询多个的话,要用多个下划线
select * from people where name like "%乔";//查询第二个字为乔的数据
select * from people where name like "%乔%";//查询名字包含乔的数据
select * from people where name like "张_";//查询张某
select * from people where name like "张__";//查询张某某
select * from people where name like "张___";//查询张某某某 依次类推
聚合查询
会自动剔除null值的数据
目前全部数据如下:
group by 只能看到每组的第一个人
select * from people group by home;
count(*)统计每组有多少行数,通过这个就可以统计每组的数量
select *,count(*) from people group by home;
max(salary) 查询该分组中该列最大的值
min(salary) 查询该分组中该列最小的值
select home,max(salary) from people group by home;
select home,min(salary) from people group by home;
sum(salary)求和
select sum(salary) from people;
avg(salary)求平均值
select avg(salary) from people; 或者自己计算 select sum(salary)/count(*) from people;
year(birthday) month(birthday) day(birthday) 查询出生日期的数据
select * from people where year(birthday)>1992;
curdate()查询当前年月日 curtime() 当前时间时分秒 sysdate()当前年月日是分秒
查询出生年份是1992年的数据
select name,birthday from people where year(birthday)=year(curdate())-28;
having数据分组之后,再筛选
比如我要查询每个省最低薪资中大于3000的,就要先分组,再筛选
select name,min(salary) from people group by home having min(salary)>3000;
- where:分组之前筛选,不能使用别名和聚合函数
- having:分组之后筛选,可以使用别名和聚合函数
排序
order by asc 升序排序
order by desc 降序排序
select * from people order by salary asc;
select * from people order by salary desc;
子查询
比如我要查询薪资比张飞高的数据,
后面加粗部分的查询语句,用小括号括起来,作为前面查询语句的条件
select * from people where salary>(select salary from people where name="张飞");
首先查询张飞的工资,然后结果作为子条件
分页查询
limit后面的两个参数 (页码-1)*每页数据数量, 每页数据数量
select * from people limit 0,2;
select * from people limit 2,2;
select * from people limit 4,2;
查询第一页,每页显示2条数据;
查询第二页数据,每页显示2条数据;
查询第三页数据,每页显示2条数据;
删除表
use 数据库名;
drop table if exists 表名;
查看当前在哪个库中操作
select database();
取消SQL语句执行
在sql语句之后加 \c 这个一定要在分号前面