数据库

什么是数据库?
数据库是一个以某种方式存储数据的电子仓库。数据库是保存有组织的数据的容器
我们学的是? —-mysql
它的优点:
1.学习成本低,mysql是开源,免费的
2.代码运行性能快,mysql执行很快
3.简单易用,容易安装和使用
4.信赖度高,某些非常重要的和声望很高的公司,站点使用mysql这些公司和站点都用mysql来处理自己的重要数据
 
mysql数据库安装
步骤1:打开虚拟机,xshell 连接linux,先清除预装的mariadb或mysql    
rpm -aq|grep -iE 'mariadb|mysql'|xargs yum erase -y;
rm -rf `find / -name mysql`;
rm -rf `find / -name my.cnf`;
步骤2:rz 上传 数据库4个rpm包
 
步骤3:安装数据库4个rpm包,有依赖需要按顺序安装:common-libs-client-server
rpm -ivh mysql-community-common-5.7.33-1.el7.x86_64.rpm;
rpm -ivh mysql-community-libs-5.7.33-1.el7.x86_64.rpm;
rpm -ivh mysql-community-client-5.7.33-1.el7.x86_64.rpm;
rpm -ivh mysql-community-server-5.7.33-1.el7.x86_64.rpm;
 
步骤4:启动mysql服务
systemctl start mysqld; #启动mysql服务
 
步骤5:进入MySQL
1.初始化密码在mysqld的日志中
grep 'temporary password' /var/log/mysqld.log
2.用初始密码进入
mysql交互页面 mysql -uroot –p
3.设置root用户的密码
alter user'root'@'localhost'identified by'Aa123456.’;
4.退出数据库
quit;
 
Mysql管理工具Navicat
 
Navicat 连接服务器数据库步骤
1、打开xshell,进入服务器数据库 mysql -uroot -p
2、给root用户授权 并刷新
mysql> grant all privileges on *.* to root@'%' identified by '数据库密码';
赋予最高权限
grant select on *.* to root@'%' identified by '数据库密码'; 赋予查询权限
mysql> flush privileges; #刷新权限
3、开放防火墙端口(测试环境/本地,可以关闭防火墙)
4、打开Navicat,输入ip、用户名、数据库密码,连接
 
连接出问题
1.检查端口是否开放
2.服务器数据库是否打开
3.服务器ip、用户名、密码,是否正确
 
 
常用的Mysql语句
 
mysql -uroot -p 进入mysql
show databases; 显示所有数据库
show tables; 查看数据库中的表
use 数据库名; 进入某个数据库
select database(); 查看当前所在的数据库
create database 库名称; 创建新的数据库
drop database 库名称; 删除数据库
drop table 表名; 删除表
 
create table 表名(name varchar(10),age int,sex char(5),hobby varchar(10) ); 创建表以及表字段 字段及字符类型可以自己设置
create table 表名( )charset=utf8; 设置写中文字段
 
desc 表名称; 查看表头
select * from 表名; 查看表数据 *可以换
insert into 表名(name,age,sex,hobby) values('zhangsan',18,'boy','linux'); 插入表的数据
insert into 表名 values ('lisi ' ,88,'MM', 'java ' ) ; 已知表字段,直接插入数据
insert into 表名 (name ) values ( ' wangwu ' ) , ( ' zhuqi ' ); 指定插入表的某个字段数据
 
update 表名 set sex='girl',hobby='JAVA' where name='wangli'; 修改表数据
delete from 表名 清空表数据
delete from 表名 where 条件 删除表中指定的数据
 
数据库中的运算
整数运算:
1、查询id=1的数据
select * from 表名 where id=1;
2、查询id不等于1的数据
select * from 表名 where id!=1;
select * from 表名 where id<>1;
3、查询id>1的数据
select * from 表名 where id>1;
4、查询id>=1的数据
select * from 表名 where id>=1;
 
 
select * from 表名 where 条件1 and 条件2; 查询同时满足条件1和2的表数据
select * from +表名 where 条件1 or 条件2; 查询至少满足一个条件的表数据
select * from +表名 where 字段 between 1 and 2; 查询一个范围内的数据
select * from +表名 where 字段 not between 1 and 2; 查询一个范围以外的数据
 
 
在集合里:in
select * from +表名 where 字段 in(值1,值2,值3); 查询在集合里的数据
不在集合里:not in
select * from +表名 where 字段 not in(值1,值2,值3);
 
 
排序:
从大到小: order by ...desc (降序)
select * from 表名 order by 字段 desc;
从小到大: order by ...asc (升序)
select * from 表名 order by 字段 asc;
 
先升序,再降序 order by ...asc,...desc
 
limit 0,1 从0开始数,取1个 limit 2, 2 看3-4行数
select * from 表名 order by id desc limit 0,1;
select * from 表名 limit 1,3; 指定查看第2-4行的数据
 
模糊匹配:like
select * from 表名 where 字段 like'%值%';
% 匹配0或多个字符 _ 匹配一个字符
 
不匹配:not like
select * from 表名 where 字段 not like'%值%';
 
聚合函数:
最大值:select max(字段) from 表名;
最小值:select min(字段) from 表名;
平均值:select avg(字段) from 表名;
求和:select sum(字段) from 表名;
统计:select count(字段) from 表名;
去重:select distinct(字段) from 表名;
 
分组:group by
select 字段 from 表名 group by 字段; 字段一致
 
having 过滤 只能跟在 group by 后面
select 字段 from 表名 group by 字段 having 条件; 过滤满足条件的表字段数据
例如:select name from cjb group by name having count(name)>=2;
查询出现过2次的姓名
 
 
空对象查询:
select * from 表名 where 字段 is null; 空白的用 字段=' ';
select * from 表名 where 字段 is not null;
 
alter table 原表名 rename 新表名 修改表名称
alter table 表名 change 原字段 新字段 类型 comment '商品编号';
修改表字段和类型 comment 为备注,可不加
alter table 表名 add 新字段 类型; 新增表字段
alter table 表名 add (新字段1 类型,新字段2 类型...) 新增多个表字段
alter table 表名 add 新字段 类型 first; 新增表字段并放在第一行
alter table 表名 add 新字段 类型 after 指定字段;新增表字段,并放在指定的字段后面
 
alter table 表名 drop 字段1, drop 字段2; 删除多个字段
alter table 表名 comment'...'; 添加表备注
 
外键:foreign key
 
创建主键:
create table nj(id int primary key, name char(20))engine=innodb;
创表,并给表字段创建主键约束
 
主键 primary key
engine=innodb; 设置库类型是INNODB类型
 
alter table 表名 change 原字段 新字段 类型 auto_increment comment'学号';
修改表字段并增加自增约束 注意:字段必须有主键或外键约束
auto_increment 自增约束
 
alter table 表名 modify 字段 类型; 去除自增约束
alter table 表名 drop primary key; 删除主键 有自增约束,要先去除自增,再删除主键
 
索引
含义:索引是一种数据结构,索引是保存某个列上的所有数据的数据结构
作用:提高查询的效率和性能
分类:
普通索引:index
唯一索引:unique
主键索引:primary
 
创建唯一索引
alter table 表名 add unique(id);
show index from 表名; 查看索引
 
删除索引
1、alter table 表名 drop index id;
2、drop index id on 表名;
 
创建主键索引
alter table 表名 add primary key(id); 字段不能为空
alter table 表名 drop primary key; 删除索引
 
 
创建视图:
create view 视图名 as(select id from xsb);
搜索视图:
select * from 视图名; 原表修改,视图也改变
 
视图
含义:一个虚拟的临时表,由一行一列组成,在使用视图时查询数据
作用:1、数据保密 2、安全,只能查看
 
特点:
1、依赖于基础表
2、比平时常使用的多表创建简单,子查询sql创建一个视图,方便操作。
3、视图可以对视图内数据增删改查,可以影响到原表,但对原表其他数据无影响
4、修改原表的数据,视图有影响
5、单表可以删除数据,多表不行
 
ps:
1、使用的时候,尽量不要用select *
2、使用视图尽可能不用from后加sql子查询
3、不能给视图加索引
 
补充:
count(*) count(1) count(列) 区别
执行结果:count(*)、count(1)统计表中所有的总数
count(列)统计时会忽略null值
执行速度:count(1) >count(列) > count(*)
 
where、group by、order by、having 之间的关系?
1.group by子句必须出现在where子句之后,order by子句之前,having 语句必须在order by 子句之后。(where先执行,再执行group by分组,group by先分组,having再执行)
2.除聚合函数计算语句外,select 语句中的每一个列都必须在group by子句中给出
 
 
数据库事务
事务是一个不可分割的数据库操作序列,也是数据并发控制的基本单位,其执行结果将使数据库从一种一致性状态变迁到另一种一致性状态,事务是逻辑上的一组操作,要么全部执行,要么全部不执行
 
四个特性:原子性、一致性、隔离性、持久性
 
 
关系型数据库和非关系型数据库对比
1、数据存储方式不同
2、数据模式不同
3、拓展方式不同
4、对事务性的支持不同
5、查询速度不同(非关系型数据库更快,因为关系型数据库(sql)存储在硬盘中,查询速度慢,非关系型数据库(nosql)不需要sql层,解析速度快)
 
数据库备份:
备份表结构:create table 备份表 like 原表;
备份表数据:insert into 备份表 select * from 原表;
一步到位备份表结构和数据:create table 备份表 as select * from 原表;
备份数据库:mysqldump -uroot -p 库名> 备份文件名称.sql
还原库:mysql -uroot -p 还原的库名< 备份文件名称.sql
 
 
select * from cjb,xsb where cjb.id=xsb.id; -- 基本连接
select * from cjb inner join xsb on cjb.id=xsb.id; -- 内连接 行数相同
select * from cjb left join xsb on cjb.id=xsb.id; 左连接 以左边表为基准表
select * from cjb right join xsb on cjb.id=xsb.id; 右连接
 
左连接一般左边放数据多的表,右连接右边放数据多的表
数据一样多,用内连接
 
子查询:
select score from score where id=(select id from student where name='zhangsan') 标量子查询 查询姓名是zhangsan的成绩
 
表子查询:
select * from (select name from xsb)a;
=select * from a; 一个全是name 的表a
=select name from xsb;
 
列子查询:
select name from xsb a where a.id in (select id from cjb where cj=88);
where ... in +返回的是一列数据
 
 
 
 
 
 
posted @ 2022-07-01 09:53  爱coding的果妈  阅读(55)  评论(0)    收藏  举报