2021-06-11MySQL两天的记录

数据库--data base--DB

 

关系型数据库:在数据库里面,表和表是可以发生关系,可以通过某些列的数据关联进行拼接查询,可以直接通过数据库的内置指令或者语句进行表和表的关联操作非关系型数据库:在数据库里面,表和表是独立的,但是可以通过一些外部代码让表和表发生关系

Redis数据库介绍:https://www.duoceshi.cn/information/Tech_article/343.html

hbase数据库介绍:

https://www.duoceshi.cn/information/Tech_article/354.html

启动mysql服务:service mysqld start

查看mysql服务状态:service mysqld status

重启mysql服务:service mysqld restart

停止mysql服务:service mysqld stop

 

安装好mysql后首次设置root用户的密码:

mysqladmin -uroot password '123456'

 

设置密码后登陆MySQL的两种方法:

密码以密文方式:mysql -uroot -p (加回车键,再输入密码)

密码以明文方式:mysql -uroot -p123456

 

 

数据类型

1,int 整型,只能保存4个字节的数据,保存的数字范围是-2^31~2^31-1 ±2147483647,超过这个值的数字需要保存要使用bigint型

2,bigint 大数字 保存的范围比int更大,可以保存8个字节的数据

3,float 浮点型,保存小数,4个字节

4,double 浮点型,保存小数,8个字节

5,varchar 可变长字符串,可变长度,最多可以占2^16字节,性能会差点,节约资源

6,char 字符串 保存固定长度的字符串,固定的占用255个字节,查询性能会好点,但是不节约资源

7,date 日期,固定的保存格式 YYYY-MM-DD

 

 

约束:

1,primary key 主键约束,不能重复,不能为空

2,auto_increment 自增长约束,可以自动的在对应列最大值+1插入数据,一般是结合主键去使用

3,not null 非空约束,必须填

4,default 默认值约束,可以定义默认值,当不输入时候自动填入默认值

 

 

 

常见的中文字符在计算机里的编码格式有GBK的汉字占2个字节位,UTF-8的汉字占3个字节位

 

 

 

数据库的操作步骤:

1,进入mysql

2,show databases;==查看当前数据库工具有什么数据库

3,use +库名;==进入数据库

4,create database +新库名;==创建一个新的数据库;

5,create table +新表名(字段名1及类型及约束,字段名2及类型及约束);

==》创建一个新表,包含字段1和字段2

6,desc +表名 ;

==查看表结构

7,show tables;

==》查看当前库有什么表

* select datebase();

==》查看当前在哪个库操作

8,alter table test rename test1;

==》 修改test为test1

9,alter table test change id sid int(20);

==》修改表test表改变id字段为sid字段int(20)数据类型为 去掉自增长约束

10,alter table test change sid sid int(20) auto_increment;

==》修改id字段为sid字段 in(20)int(20),增加自增长约束

11,alter table test add class int(10);

==》修改表test表增加class字段,数据类型为int(10)增加的字段默认加在表格结构的最后面

12,alter table test add sex varchar(5) first;

==》修改表test表增加sex字段类型为varchar(5) 在表结构的第一位 first为第一位

13,alter table test add age int(10) after sex;

==》修改表test表增加age字段类型为int(10)加在sex字段后面 after sex

14,alter table test drop sex;

==》修改表test表删除sex字段

15,alter table test drop age,drop class;

==》修改表test表删除age字段和删除class字段

16,alter table test add(class int(10),sex varchar(5));

==》修改表test表增加class和sex字段

17,alter table test modify sex char(5) after name;

==》修改表test表改变sex字段的类型为char (5)以及位置在name字段后面

 

 

*modify和change的区别:

它们都是可以修改表字段的定义,但是change需要写两次字段名,change可以修改字段名,modify不行

 

1删除主键:

alter table test drop primary key;

ERROR 1075 (42000): Incorrect table definition; there can be only oneauto column and it must be defined as a key (直接删除会报错)

1,1首先要先删除自增长约束:

alter table test modify sid int(20);2,

删除主键:

alter table test drop primary key;

ERROR 1075 (42000): Incorrect table definition; there can be only oneauto column and it must be defined as a key (直接删除会报错)

1,2首先要先删除自增长约束:

alter table test modify sid int(20);2,

2增加主键:

alter table test change sid sid int(20) primary key;

注意:删除主键约束时不能通过修改字段属性进行,只能直接使用drop;添加主键约束则可以直接通过修改字段属性的方法进行

删除表: drop table +表名;

通过绝对方式的方法操作其他库中表:alter table库名.表名addr . 。。。

 

alter table ===》改变表的结构

insert into ===》插入数据

select * from ==》查询数据

 

插入数据时,注意:

1,varchar、char是字符串数据类型,所以插入数据的时候需要加 '' 引号;

2,date 日期型也要加引号

3,非空约束中,类型为字符串的字段默认值为' ',类型为数字的字段默认值是0,与其他没有非空约束的字段对比,其他字段的NULL是不占空间的,但是非空约束字段的值,默认是占一个字节0的空间

 

 

插入数据有三种方式

第一方式:插入数据时,完整的说明表中字段,值的位置要和说明字段的位置一一对应

insert into test(sid,name,sex,score,phone,time,class) values(1,'xiaomai','228888,'2021-06-11',2130);

 

第二种方式:插入数据时,单独对某一个或者某多个字段插入,值的位置和说明字段的位置一一对应

insert into test (sid) values (2);

insert into test(class,sid) values(88,2130);

 

第三种方式:插入数据时,不说明表中字段情况,默认的往所有的字段插入数据,而且字段的位置也是和表结构顺序一致,所以这种方式要求对表的结构非常熟悉

insert into test values(100,'damai','boy',90.12345,18999990000,'20210718',2130);

 

 

插入数据可以同时插入多行值:

insert into test(time) values('2021-06-11'),('20210611'),(2021/06/11'),(2021-6-1'),('2021/6/1');

insert into test values

(100,'damai' ,'boy',90.12345,18999990000,20210718',2130),

(99, 'laomai','M',88.88888,13188884444,20213345',13588886666);

 

删除表数据:

delete

delete from test1 where id=3;

delete from test1 where phone=17688889999 and id=2;delete from test1;

 

更新(修改)表数据:update

update test1 set phone=18899998888;

update test1 set phone=14466667777 where id=1;

 

删除表数据有三种方式:

第一种: delete from表名;

 

删除表中数据,但是不释放空间,自增长还是按照原来最大的值继续加一

第二种: truncate表名

 

第三种: drop table+表名;===》完全删除一张表,释放空间,且清除表结构

查询表数据:select

 

select *from test1;

查询所以的字段的值来自于test1 表;

 

select * from test1 where id=1;

查询所有字段的值 来自于test1表当id为1的值

 

select name from test1;

查询name字段的值来自test1表

 

select name from test1 where id=6;

查询name字段的值来自于test1表id为6的数据

 

select name from test1 where phone=17688889999 andtime= '20210619;

查询name字段的值 test1 表 phone = 1768889999和 time=20210619

 

select name,id,phone from test1 where id=1 or time= '20210610;

查询name,id,phone字段的值来自test1表 当id=1或者time=2021060

 

select name,id,phone from test1 where id=1 or id=6 or id=10;

查询 name id phone from 的数据条件为 id等于1 或者id=6 或者id=10

查询name,id,phone字段的值来自于test1表当id=1或者time=20210610

 

select name from test1 where id=1 and id=6;

不能同时对—列的数据进行and条件查询,否则结果返回空

 

select id,name from test1 where id<>1;

查询id不等于1的数据

 

select id,name from test1 where id != 1;

也是查询不等于1的条件

select * from test1 where score is null;

查询所有字段的值来自于test1表当score字段值为空(is null)

 

select *from test1 where score is not NULL;

查询所有字段的值来自于test1表当score字段不为空(is not null)

 

select * from test1 where name = 'NULL‘;

因为NULL是一个字符串,字符串的查询需要加引号

 

select * from test1 where score >80;

查询所有字段的值来自于test1表当score 大于80

select * from test1 where score <80;

查询所有字段的值来自于test1表当score 小于80

select *from test1 where score <=78.65;

查询所有字段的值来自于test1表当score小于等于78.65

select *from test1 where score > =78.65;

查询所有字段的值来自于test1表当score大于等于78.65

select *from test1 where score <=78.65;

查询所有字段的值来自于test1表当score 小于等于78.65

select *from test1 where score >=78.65;

查询所有字段的值来自于test1表当score大于等于78.65

select * from test1 where score >=78.65 and score <90;

查询所有字段的值来自于test1表当score大于78.65且score小于90

 

select * from test1 where score between 78.65 and 90;

查询所有字段的值来自于test1表当score在78.65和90之间,包含78.65和90

between 78.65 and 90等同于大于等于78.65且小于等于90

select *from test1 where id in (1,6,10);

查询所有字段的值来源test1表当id 满足(1,6,10)

select * from test1 where id not in (1,6,10);

查询所有字段的值来源test1表当id不在(1,6,10)

 

select *from test1 where phone =13188887777 or phone=17688889999 and time='20210619';

满足(条件一phone=13188887777)或者(条件二phone=17688889999 且 time为20210619)的数据

 

select * from test1 where name like '%o%';

查询所有字段的值来自于test1表当name字段的值包含‘o'

select * from test1 where name like 'x%';

查询所有字段的值来自于test1表当name字段的值为‘×’开头

 

%号在mysql里面的作用:匹配0个或者多个字符 (有点像Linux的*号)

 

 

 

select * from test1 limit 0,3;

查询所有字段的值来源test1表列出第一行开始的3行

select * from test1 limit 3,2;

查询所有字段的值来源test1表列出第4行开始的2行

 

*在生活中,排序都是从1开始的,但是在计算机程序里是从0开始

 

select *from test1 order by score;

通过order by对score字段查询

select * from test1 order by score asc;

通过order by对score字段进行升序(从小到大)排序

select * from test1 order by score desc;

通过order by对score进行降序(从大到下)排序,使用decs

select *from test1 where score is not null order by score desc limit 0,2;

先过滤出score不为空的数据,再对score字段的值进行排序,再取前面2条

select *from test1 where score is not null order by score desc;

先过滤出score不为空的数据,再对score字段的值进行排序

 

select *from test1 group by time;

group by 分组之后,其他匹配出来的字段值,是随机匹配出来的,一般是表中匹配到的第一条

 

select *from test1 where score is not null group by time;

mysql中允许使用where条件过滤数据之后,再进行group by分组

 

select *from test1 group by time where score is not null;

不允许先使用group by分组再使用where条件过滤,否则会执行报错

 

select *from test1 group by time having score is not null;

使用group by分组之后,需要进行条件过滤则使用having接条件

 

面试题:现在有一张学生成绩表,里面有字段id、name、score、class,统计每个班级成绩合格的人数

1,先过滤出成绩合格的数据

2,对第一步的结果进行分组

3,统计分组之后的人数情况

select class,count(class),from 学生成绩表 where score>=60 group by class;

 

mysql中的聚合函数

count():统计函数

select count(*) from test1;

select count(1) from test1;

统计整个表中一共有多少行数据

 

select count(score) from test1;

统计score字段,在test1表中有多少条数据

 

select count(score) from test1 where time = '20210610';

通过过滤time=20210610的数据之后,再统计score字段的条数

 

sum():求和函数

select sum(score) from test1;

求分数的总和

select phone,sum(score) from test1 group by phone;

分组之后进行求和

avg():求平均函数

select avg(score) from test1;

求score字段的平均值

其中只对3条数据进行求平均,因为只有3条有值

max():最大值函数

select max(score) from test1;

求score字段的最大值

min():最小值函数

select min(score) from test1;

求score字段的最小值只对有值的结果进行求值

distinct():去重函数

select distinct(time) from test1;

对time字段进行去重

 

 

在sql中进行四则运算:

select max(score)-min(score) from test1;

select max(score),min(score),max(score)-min(score) as jg from test1;

对字段名称进行取别名操作max(score)-min(score) 在jg字段显示

select max(score),min(score),max(score)-min(score)*2 as jg from test1;

乘法运算

select max(score),min(score),(max(score)-min(score))*2 as jg from test1;

select max(score),min(score),(max(score)-min(score))/2 as jg from test1;

除法运算

having后面可以接聚合函数,where后面不能接聚合函数

数据库备份的两种方式:

第一种方式:在数据库中进行备份

1,创建一个备份表,表结构与需要备份的表一致

create table 新表 like 旧表;

insert into test2(id,name) select id,name from test1;

对test2表的id和name字段插入test1表的id, name字段的值

2,往新表插入旧表的所有数据

insert into 新表 select * from 旧表;

 

 

 

第二种方式:在数据库以外进行备份(在linux系统进行)

1,通过mysql数据库的备份指令进行备份数据库备份文件

mysqldump -uroot -p 需要备份的库 > ./sql脚本文件.sql

2,恢复还原备份

首先要在数据库里面新建一个库,用来存放稍后还原的数据

在linux执行:mysql -uroot -p123456 新库 < ./sql脚本文件.sql

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值