找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
第7讲 数据库mysql


数据库分关系型数据库和非关系型数据库
1、什么是关系型数据库?
依据关系模型创建的数据库,把数据保存在不同的表中,
表 与表存在着某些关系。

2、什么是非关系型数据库?
非关系型数据库也叫nosql数据库,全称not only sql。
通常 数据以对象的形式存储在数据库中,不固定结构,例如列模型,键值对模型。

3、关系型数据库特点
1.安全
2.保持数据的一致性
3.实现对表与表进行复杂的数据查询

4、非关系型数据库特点
1.效率高
2.容易扩展
3.使用更加灵活

5、关系型数据库有哪些
1.db2
2.oracle ==》收费  大型公司 金融公司
3.mysql ==》开源 免费 体积小支持多系统
4.sql server  ==》微软

6、非关系型数据库有哪些
1.hbase(列模型) ==》大数据
2.redis(键值对模型) ==》缓存
3.mongodb(文档类模型)

7、安装 数据库mysql
1)yum install  mysql  ==》安装mysql 数据库客户端
2)yum insatll  mysql-server  ==》安装mysql数据库服务端
3)service mysqld status ==》查看mysql数据库的状态(d==》daemon表示一个守护进程)
4)service mysqld start  ==》启动数据库
5)service mysqld stop   ==》关闭MySQL数据库
6)service mysqld restart  ==》重启数据库  ==>进入Linux 需要登录是先执行这个命令

8、数据库的一个操作
1)[root@localhost /]# mysql -uroot -p
Enter password:
-u 表示user root 是用户
-p 是密码 password 的意思

2)use mysql; ==》进入到mysql 这个数据库

3)mysql> show databases;   ==》查看 数据库有哪些库  注意这里需要英文分号

4)mysql> exit;   ==》退出数据库,退到linux 页面,也可以用ctrl +c 退出数据库,退到linux

5)[root@localhost /]# mysqladmin -uroot password "123456"  ==》在linux下修改密码
只能修改初始密码,刚进入到mysql 数据库不需要密码,按enter键跳过密码

6)[root@localhost /]# mysql -uroot -p     ==》可以这样登录数据
Enter password: 123456
==》还可以[root@localhost /]# mysql -uroot -p123456  ==》这样登录数据库

7)create table 表名(字段1 数据类型 约束,字段2 数据类型 约束,字段3 数据类型 约束)
注:约束可以不要,但是数据类型一定要

create table tab(id int(10)primary key auto_increment,score float(20,2) not null,
name varchar(10),phone bigint(10)default 13977778888,time date);

8)desc tab;  ==》查看表结构(description )
+-------+-------------+------+-----+-------------+----------------+
| Field | Type        | Null | Key | Default     | Extra          |
+-------+-------------+------+-----+-------------+----------------+
| id    | int(10)     | NO   | PRI | NULL        | auto_increment |
| score | float(20,2) | NO   |     | NULL        |                |
| name  | varchar(10) | YES  |     | NULL        |                |
| phone | bigint(10)  | YES  |     | 13977778888 |                |
| time  | date        | YES  |     | NULL        |                |
+-------+-------------+------+-----+-------------+----------------+

field ==字段
type  ==》类型
null  ==》是否为空
key ==》primary key  ==>主键可以单独使用
default ==》默认值约束
extra ==》额外的备注 auto_increment ==》自增长 只能和主键一起使用

数据库中的数据类型
数值型
int  ==》整型 最大存储的值是2147483647超过这个值就用bigint
bigint ==》整型 手机号就用它
float ==》浮点型  float(20,2) ==》20代表整数部分和小数部分一起是20位,精确到两位小数

文本型  ==》存储的值需要用双引号或者单引号
varchar  
char
这两个有什么区别?
varcha(20)
char(20)
1)varcha(20)和char(20)同样存储‘abc’ ,char存储字符串3个字节,剩下的17个以空格补上,
varchar只会存储3个,其他的空着
2)char的效率高varchar高,
3)varchar 节省空间

日期型 ==》存储的值需要用双引号或者单引号
date ==》‘2022-5-31’


数据库约束
not null ==》非空约束
default  ==》默认值约束
auto_increment ==》自增长 只能和主键一起使用,不给值时,默认加一
primary key ==》主键约束 ,不能为空,唯一值 身份证
foreign key ==》外键约束 表与表之间的约束


9)修改表结构 alter table +表名

1.查看表结构 desc +表名 显示字段名称,数据类型,约束,备注

2.修改表名alter table +表名 rename + 新表名
alter table tab1 rename tab; ==》把tab1这个表重命名为tab

3.修改表字段 alter table +表名 change +原字段名 +新字段名 数据类型,约束
alter table tab change id sid int(10);==》把id字段改为sid,同时去掉自增长
alter table tab change sid id int(10) auto_increment; ==》把sid字段改为id,同时加上自增长

4.添加表字段,并放到第一个字段前 alter table +表名 add +字段名 数据类型 约束 first
alter table tab add class int(10) first; ==》增加一个字段class 字段放在第一位

5.添加表字段,并放到某个字段后 alter table +表名 add +字段名 数据类型 约束 after +字段名
alter table tab add sex int(10) after id; ==》增加一个字段sex,放在id 的后面

6.同时添加两个字段,默认添加到字段最后 alter table +表名 add(字段1 数据类型,字段2 数据类型)
alter table tab add(age1 int(10),age2 int(10));==》同时给tab表增加两个字段age1 age2

7. 删除表字段 alter table +表名 drop + 字段
alter table tab drop class;==》把class字段删除

8.删除表两个字段 alter table +表名 drop 字段1,drop 字段2
alter table tab drop age1,drop age2;==》同时删除两个字段age1 age2

9.修改主键id为自增长 alter table +表名 change +字段名 数据类型 auto_increment
alter table tab change id id int(10) auto_increment;==》不修改id字段增加自增长

10.删除主键
alter table tab change id id int(10); ==》先删除自增长
alter table tab drop primary key;  ==》删除主键

11.alter table tab change id id int(10) primary key auto_incerment;==>同时增加主键和自增长

10.删除表drop table +表名
drop table tt; ==》删除tt这个表


11、数据库增删改查
1)增加
单条数据插入
insert into tab(id,score,name,phone,time)values(1,66.88,'xiaoduan',13988886666,'2022-5-31');
insert into tab values(1,88.66,'xiaoliu',13466668888,'2022-5-31');

多条数据插入
insert into tab(id,score,name,phone,time)values(3,66.88,'xiaoduan',13988886666,'2022-5-31'),
(4,66.33,'xiaoyi',13456789876,'2022-5-31'),(5,66.66,'xiaduan',13499998888,'2022-1-3');

insert into tab values(6,66.34,'xiaoming',13666668888,'2022-5-12'),(7,66.99,'xiaohe',13866667777,'2022-5-30'),
(8,66.68,'xiaohu',13734567890,'2022-5-31');

单个字段插入
insert into tab(name)values('xiaotao');==》只给name 插入字段时,id字段自动加一,
可以为空的字段以null填充,score字段以0.00填充

insert into tab(score)values(66.7899);==》只给score 插入字段时

0 不等于null,0时一个值,null是一个属性,空

数据库怎么去测试?
数据库类型
约束类型
存储的大小  边界值

2)删  delete from 表名

delete from tab where id =10;==》把id等于10 的数据删除
delete from tab where id > 6;==》把id大于6的数据删除
truncate tab;==》快速删除全表数据

注:TRUNCATE,DELETE,DROP放在一起比较:
TRUNCATE TABLE:快速删除表数据。
DELETE TABLE: 按条件删除表数据 和where 条件使用
DROP TABLE:删除表结构和表数据

3)改 update 表名 +set 字段= ?
update +表名 set 字段名=值 where 条件
update tab set name = 'xiaoduan' where id = 4;==》把id等于4的数据姓名改为xiaoduan
update tab set name = 'xiaoduan';   ==》把全表的数据姓名字段改为xiaoduan
update tab set score = 100 where id = 1;==》把id等于1的score分数改为100分


4)查   select from 表名
1.查询表中所有数据 select * from +表名 ”*“ 代表所有
2. 查询某个字段的数据 select 字段 from +表名
3. 查询多个字段的数据 select 字段1,字段2 from +表名
4.查询满足某个条件的所有数据 select * from +表名 where 字段=值 where 后面接满足的条件
5.查询不满足某个条件的所有数据 select * from +表名 where 字段!=value “!=” 代表不等于 ,
也可以用符号 ”<>“ 代表不等于

select * from tab;  ==》查询全表的数据
select id from tab; ==》只查询id的数据
select id,score,name from tab;==》同时查询id score name 三个字段的数据
select id from tab where score = 100;  ==》查询score等于100的id的数据
select *from tab where id >3;  ==》查询id大于3的数
select *from tab where id !=3; ==》查询id不等于3的数据
select *from tab where id <>3; ==》查询id不等于3 的数据
4)查   select from 表名
1.查询表中所有数据 select * from +表名 ”*“ 代表所有
2. 查询某个字段的数据 select 字段 from +表名
3. 查询多个字段的数据 select 字段1,字段2 from +表名
4.查询满足某个条件的所有数据 select * from +表名 where 字段=值 where 后面接满足的条件
5.查询不满足某个条件的所有数据 select * from +表名 where 字段!=value “!=” 代表不等于 ,
也可以用符号 ”<>“ 代表不等于

例子:
select * from tab;  ==》查询全表的数据
select id from tab; ==》只查询id的数据
select id,score,name from tab;==》同时查询id score name 三个字段的数据
select id from tab where score = 100;  ==》查询score等于100的id的数据
select *from tab where id >3;  ==》查询id大于3的数
select *from tab where id !=3; ==》查询id不等于3的数据
select *from tab where id <>3; ==》查询id不等于3 的数据


6.查询同时满足多个条件数据 select * from +表名 where 条件1 and 条件2 and
关键字左右的两个条件必须同时满足

7.查询满足至少1个条件的数据 select * from +表名 where 条件1 or 条件2 or
关键字左右的两个条件至少满足1个,否则返回空

8.查询一个条件范围内的数据 select * from +表名 where 字段 between m and n
between...and ... 指定一个范围

9查询字段满足在指定的集合中的数据 select * from +表名 where 字段 in(值1,值2,值3)

10查询字段不满足在指定集合中的数据 select * from +表名 where 字段 not in (值1,值2,值3)

例子:

#查询class为1833并且sex为1的数据
#select * from student where class = 1833 and sex = 1;

#查询class为1833或者class为1835的数据
#select *from student where class = 1833 or class = 1835;
#select * from student where class in (1833,1835);

#查询id 不在1和3里面的数据
#select * from student where id not in (1,3);

#查询年龄大于24和小于31的数据
#select * from student where age > 24 and age <31;

#查询年龄在25和31 之间的数据
#select * from student where age between 25 and 31;




11.查询字段值为空的数据 select * from +表名 where 字段 is null 注意:
字段是空不能写成 字段=null

12.查询字段不为空的数据 select * from +表名 where 字段 is not null

13.查询某个字段模糊匹配成功的数据 select * from +表名 where 字段 like “%值%”
%用于匹配字段开头和结尾

14.查询限定的数量的数据 select * from +表名 where 字段
limit m,n m 指下标,n指限定的数量,下标为m的开始的n条数据
例子:

#查询班级为空的数据
#select * from student where class is null;

#查询class 字段不为空的数据
#select * from student where class is not null;

注:0不等于null,0是一个值,  null 是一个空属性,所以用is
#查询前5行的数据

#select * from student limit 0,5; (select * from student limit 5;)  --》一般数据库从0开始算起的
#查询第3行到第7行的数据

#select * from student limit 2,5; --》(3-1=2,2+5=7 ==》(2,5)帮助理解)
#查询姓名以“ci”开头的数据
#select *from student where name like 'ci%';

#查询姓名以‘qi’结尾的数据
#select * from student where name like '%qi';



15.查询的数据根据某个字段从小到大排序 select * from +表名 order by 字段
asc order by ...asc 从小到大排序

16.查询的数据根据某个字段从大到小排序 select * from +表名 order by 字段
desc order by ... desc 从大到小排序

例子:
#查询表数据按id从小到大排序(升序)
#select * from student order by id asc;  (ascend)
#查询表数据按id从大到小排序(降序)
#select * from student order by  id desc; (descend)



17.查询的数据根据某个字段进行分组 select * from +表名 group by 字段 group by ...
根据条件分组

18.查询的数据根据某个字段进行分组再条件过滤 select * from +表名 group by
字段 having 条件 having跟在group by 后面,作用相当于where

mysql聚合函数 【重要】
19. 统计查询数据的数量 select count(*) from +表名
20. 查询某个字段求和 select sum(字段) from +表名
21. 查询某个字段进行平均值 select avg(字段) from +表名
22. 查询某个字段最大值 select max(字段) from +表名
23. 查询某个字段最小值 select min(字段) from +表名
24 对某个字段进行去重 select distinct(字段) from +表名

例子:
#根据班级class字段分组、然后求出每个班级的人数
#select class,count(*)  from student group by class;
#帮class和count(*)分别取别名a和b
#select class as a,count(*) as b from  student group by class;
#帮class和count(*)分别取别名"班级"和"每个班级总人数"
#select class as 班级,count(*) as 每个班级总人数 from student group by class;
select class,sum(math) from student group by class; 求每个班级的总分
select class,avg(math) from student group by class; --》求每个班的平均分数
select max(math) from student; -->求所有班级的最高分
select min(math) from student; --》求所有班级的最低分
select distinct(age),class from student;---》根据年龄进行去重


题目:
1、求出每隔班数学成绩大于80的人数
select count(*),class from stu where math>80 group by class;

2、求出每个班性别为1的数学总成绩
select sum(math),class from stu where sex = 1 group by class;

3、求出每个班的数学总成绩大于200的班级和成绩信息
select sum(math),class from stu group by class having sum(math) > 200;


单表查询总结
1、分组函数group by 只能和聚合函数一起使用、要接只能接分组字段
2、where 后面可以接group by 分组函数、但是group by 后面不能接where
3、group by 前面夹where 是为了先过滤 后分组
4、having 可以放在group by 后面 筛选满足条件的组==》类似where



12、[数据库的备份]
1)备份表
create table tab1 like tab;  ==》创建一个tab1表,和tab表一样的结构(只是备份结构)
insert into tab1 select * from tab;  ==》往tab1表中插入和tab表一样的数据(全部数据)
insert into tab1(id,name,phone)select id,name,phone from tab;
==》往tab1 表中插入和tab 表中id name phone 一样的数据
注意点
1. 插入的表必须存在
2. 插入的表是新表,没有数据。


2)备份库
mysqldump -uroot -p123456 dcs7 >/dcs7/dcs7_bak  ==》把dcs7这个库备份到根目录下dcs7下名为dcs7_bak

mysql -uroot -p123456 dcs8 </dcs7/dcs7_bak  ==》把根目录下dcs7下名为dcs7_bak 还原到dcs8这个库里面



13、[数据库权限]
进入mysql : mysql -uroot -p123456

use mysql; --》使用mysql 数据库

select host,user from user;  --》查看mysql数据库有那些用户 (localhost和127.0.0.1 代表的是本地用户;%  代表的是具有远程访问权限的用户)

insert into user(host,user,passwrod)values('localhost','dcs7',password("123456"));  --》插入新用户,不具有权限

show grants for 'dcs7'@'localhost'; --》查看dcs7 用户是否具有权限
执行后:ERROR 1141 (42000): There is no such grant defined for user 'dcs7' on host 'localhost'
flush privileges;---》刷新权限表
show grants for 'dcs7'@'localhost';再次查看下
执行后:GRANT USAGE ON *.* TO 'dcs7'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'

grant select,update,delete,drop on *.* to 'dcs7'@'localhost' identified by '123456';  --》 对创建的用户进行授权

flush privileges;---》授权后需要刷新权限

show grants for 'dcs7'@'localhost';---》刷新权限后查看权限










分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册