mysql数据库 期末总结

目录

  • 数据库基础知识
  • MySQL命令
    • 字符集
    • 库操作
    • 表操作
        • 创建、查看和删除
        • 修改表
        • 复制表
        • 约束控制
    • 数据操作
      • 插入数据(增)
      • 删除数据(删)
      • 修改数据(改)
      • 查询数据(查)
        • 单表查询
            • 分组查询
            • 排序
            • 限制查询结果数量
            • 集合函数
        • 多表查询
            • 1. 内连接
            • 2. 外连接
            • 3. 子查询
    • 视图
    • 存储过程
        • 创建、使用存储过程
        • 变量
        • 流程控制
    • 触发器
    • MySQL权限管理
        • 用户管理
        • 账户权限管理
    • 数据库备份
          • 数据库、表备份
          • 数据备份

数据库基础知识

mysql数据库 期末总结_第1张图片
关系中的基本术语:
(1)元组,也称记录,相当于 关系相当于
(2)属性,相当于
(3)候选码 = 码 = 关键字
(4)主码,有多个主码时称作复选码
(5)外码:一个码在本表中不是主码,在另一个表中是主码
(6)强制关键字:所有属性都不能作为码时,引入的强制关键字
(7)全码:关系模式的所有属性是这个关系模式的候选码,称为全码
(8)候选码中的属性叫主属性,不包含在候选码中的属性叫非主属性

MySQL命令

sql命令的9个核心动词:insert、delete、 update、select,create、alter、drop、grant、revoke

字符集

show character set查看所有可以使用的字符集

mysql字符集包括字符集和校对规则两个概念,字符集和校对规则时一对多的关系,两个不同的字符集不能有相同的校对规则,每个字符集有一个默认校对规则。校对规则的结尾处_ci表示大小写不敏感,_cs大小写敏感,_bin按二进制排序。

库操作

create database stu; 创建数据库 
create database if not exists stu;
create database stu default character set gbk collate gbk_chinese_ci;
show create database stu; 查看数据库详细信息

use stu; 使用数据库

alter database stu character set gbk; 修改数据库字符集
drop database stu; 删除数据库
drop database if exists stu;

表操作

创建、查看和删除

create table student; 创建表

create table student(
sno varchar(10) not null [primary key],
sname varchar(20) not null,
ssex enum('男','女') not null default '男',
sbirth date not null default '2001-01-01',
primary key (sno)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

show student; 查看表
decribe student; 显示表
desc student; 显示表

drop table student; 删除表
drop table if exists stu,student; 删除两个表

修改表

alter table student 
add sphone char(20) not null default '10' [after ssex]; 给表添加字段

alter table student 
drop sphone; 删除字段

alter table student
modify sname char(40); 修改字段数据类型

alter table student
modify sbirth date default '2002-02-02'; 修改默认值

alter table student 
change sname sname char(40); 重命名字段并修改数据类型或修改默认值

alter table student rename to stu; 修改表名

复制表

create table stu like student; 复制student表到stu表中

create table SnoNameTable 
as select sno,sname 
from student; 复制student表中的sno,sname到新表SnoNameTable中

约束控制

主键约束

create table stu2(
sno char(4) primary key
);

create table sc(
sno char(10) not null,
cno char(10) not null,
grade int not null default 0,
primary key(sno,cno)
);

修改表sc的主键,删除原来的主键,增加sno、cno为主键

alter table sc drop primary key,add primary key(sno,cno);

外键约束

alter table score
add foreign key(sno)
references student(sno);

如果表还没建立,可以在建立表时指定

create table score(
sno varchar(10) not null,
sname varchar(20) not null,
grade int not null default 0,
primary key(sno,cno),
foreign key(sno) references student(sno)
);

或者

create table score(
sno varchar(10) not null references student(sno),
sname varchar(20) not null,
grade int not null default 0,
primary key(sno,cno)
);

非空约束

not null

唯一约束

unique

alter table student add unique(sname); 添加唯一约束

默认约束

default 60
default '2001-02-03'

自增约束

auto_increment

删除约束

alter table score 
drop foreign key sno;

数据操作

插入数据(增)

向表中所有字段插入数据:
insert into student 
values('20190901','张三','男','1888888888','2002-09-09');

向表中指定字段插入数据:
insert into student(sno,sname,ssex)
values('20190901','张三','男');

同时插入多条数据:
insert into student(sno,sname,ssex)
values
('20190901','张三','男')
('20190902','路瑶琨','男')
('20190903','路筱簜','女');

补充:把一个表中数据复制到另一个表中

insert into student_copy(sno,sname,sbirthday)
select (sno,sname,sbirthday) from student;

删除数据(删)

delete from student
where sno='20190901';

补充:使用truncate清空表数据

truncate student;

truncate、delete、drop的区别:
delete table:删除内容,不删除定义,不释放空间
truncate table:删除内容,不删除定义,释放空间
drop table:删除内容,删除定义,释放空间
truncate 数据定义语言 (DDL语句)
delete 数据操作语言(DML语句)

修改数据(改)

update student
set sname='钟无艳',ssex='女'
where sno='20190901';

查询数据(查)

单表查询

关键词:select from where group by having order by limit

select * from student;

select sname from student 
where sno='202000';

distinct去除重复的查询记录

select distinct cno from student;

为表和字段取别名 AS

select sno '学号', grade s'成绩' from score; 或:
select sno as '学号',grade as '成绩' from score; 

条件查询
比较:= < <= != <> !>
匹配字符:like not like
指定范围:between and not between and in not in
是否为空值:is null is not null

“%” 可以代表任意长度的字符串,"_" 只能代表单个字符
eg:“李%” “李__”

分组查询

group by 跟集合函数配合使用

select ssex,count(ssex)
from student
group by ssex
having count(ssex)>=10;

group by 后面必须用having,不能用where

排序

order by ASC升序 DESC降序

select * from student
order by zno asc,sno desc;
限制查询结果数量
limit a,b;
从第a+1位开始查看b条信息
limit b;
只有一个数字时默认a=0,从第一条开始查询b条信息
集合函数

集合函数不能嵌套
count() sum() avg() max() min()

多表查询

1. 内连接
  • 等值连接/不等值连接 inner join:有一个字段相同 (自身连接)

  • 自然连接 natural join :自然连接会去掉重复字段

2. 外连接
  • 左连接 left join:包含左表所有记录即使右表中没有匹配字段

  • 右连接 right join: 包含右表所有记录即使右表中没有匹配字段

3. 子查询

能用多表连接的都可以用子查询实现,如:

查询没有选修课程的学生学号姓名
select sno,sname
from student
where sno not in(select sno from score);

select sno,sname
from student left join score
on student.sno=score.sno
where cno is null;
  1. in关键字的子查询
select sno,grade
from score
where grade in(65,75,85,95);

select *
from student
where sno not in (select sno from score);
  1. exists关键字的子查询
select *
from course
where exists (select * from specialty where zname='网络空间安全');
  1. any关键字的子查询
查询比其他班级某一个同学年龄小的学生姓名
select sname,age
from student
where sbirth > any(
               select sbirth
               from student
               where class='渗透测试'
               );
  1. all关键字的子查询
查询比其他班级所有同学年龄都小的学生姓名
select sname,age
from student
where sbirth > all(
               select sbirth
               from student
               where class='渗透测试'
               );

=
>ALL = >MAX()
>ANY = >MIN()
=
!=ALL = not in
any = in

视图

创建视图

create view stu_view
as
select * from student; 

删除视图

drop view [if exists] stu_view;

查看视图的定义

1.describe stu_view; 或 desc stu_view;
2.show table status like 'stu_view';
3.show create view stu_view; 

视图的增删改查与表的增删改查相同,更新视图中的数据其实就是更新表中的数据

查询 添加 修改 删除
行列子集视图
连接视图 × ×
集合视图 × × ×

       在视图的使用中,需要注意的是:要保证视图中的每一列都有列名,并且要给聚合函数的那一列起名

create view stu_score
as
select sno,avg(grade) as '平均成绩'
from score
group by sno;

存储过程

创建、使用存储过程

delimiter $$
create procedure stu_pro()
begin
end $$
delimiter ;

带输出参数的存储过程在存储过程内部有给输出参数赋值的语句

查看存储过程
1.show procedure status like 'stu_proc';
2.show create procedure stu_proc; 

删除存储过程
drop procedure [if exists] stu_proc;

调用存储过程:call stu_pro;
			 call stu_pro(@s);
			 call stu_pro('18888','26666',@s);

用call调用存储函数时,结果放在用户变量中@i,最后输出@i的值。

在mysql中," / "运算符并非取整,而是:select 5%3 , 5/3;
                                                                 => 2 , 1.67777

变量

变量包括局部变量、用户会话变量和系统变量,系统变量前通常两有个@符号,但某些系统变量会省略@符号

  • 会话变量名以@开头,局部变量名前没有@
  • 会话变量的作用范围与生存周期大于局部变量
  • 局部变量必须定义数据类型,会话变量不需要定义

1. declare语句声明局部变量,仅使用与begin…end程序段中

declare str1,str2 varchar(6);

defalut value将默认值设为value,没有使用default默认时null

2. 使用set语句给变量赋值

set str1='abc',str2='123';

比较:1.set可以直接声明用户变量,不需要声明类型,declare必须指定类型; 2.set位置可以任意,declare必须在复合语句的开头,在其他语句之前; 3.declare定义的变量的作用范围时begin…end块内,只能在块中使用; 4.set定义的用户变量在变量定义时,变量名称前使用@符修饰,如set @var=12。

3. 使用select语句给变量赋值
mysql数据库 期末总结_第2张图片

流程控制

  1. if语句
if search_condition then statement_list
	[elseif search_condition then statement_list]
	...
	[else search_condition then statement_list]
end if
(一个if对应一个end if)
  1. case语句
case case_value
	when when_value then statement_list
	[when when_value then statement_list]
	...
	else statement_list
end case

3.loop语句:可以使某些特定的语句重复执行,实现简单的循环。loop没有停止循环的语句。要结合leave语句退出循环或是iterate继续迭代,例如:

add_num:loop
set @count = @count + 1;
end loop add_num
  1. leave语句:用于跳出循环
add_num:loop
set @count = @count + 1;
if @count = 10 then leave add_num;
end loop add_num
  1. itebate语句:用于跳出本次循环,然后进入下一轮循环
add_num:loop
set @count = @count + 1;
if @count = 10 then leave add_num;
elseif mod(@count,2) = 0 then iterate add_num; 
end loop add_num
  1. repeat语句:有条件控制的循环语句,当满足特定条件时,就会跳出循环语句。例如:
repeat
set @count = @count + 1;
until @count = 10;
end repeat
  1. while语句:有条件控制的循环语句,当满足特定条件时,执行循环内的语句。例如:
while @count<10 do
set @count = @count + 1;
end while

存储过程习题:https://blog.csdn.net/weixin_43376075/article/details/103752147

触发器

基本格式:

delimiter $$
create trigger tri_name
before [insert/update/delete] on student
for each row
begin
end $$
delimiter ;

触发器执行顺序:before触发器、表操作、after触发器
一般情况下,触发操作与触发器内部操作的数据相同时,用before

删除触发器:
drop trigger tri_name;
查看触发器:
show trigger;

触发器习题:https://blog.csdn.net/weixin_43376075/article/details/103752147

MySQL权限管理

用户管理

创建用户

create user 'u1'@'localhost' identified by '123';

修改密码

set password for 'u1'@'localhost' = password('1234');

删除用户

drop user 'u1'@'localhost';

账户权限管理

权限授予

grant [select/update/all/..] on student 
to 'u1'@'localhost';

用户不存在授权

grant select on student 
to 'u1'@'localhost' identified by '123';

允许传播权限

grant select on student 
to 'u1'@'localhost' identified by '123'
with grant option;

收回权限

revoke select on student
from 'u1'@'localhost';

可以只回收部分字段的权限

revoke select(sno,sname) on student
from 'u1'@'localhost';

数据库备份

数据库、表备份
备份数据库中的表到指定路径:
mysqldump -hlocalhost -uroot -p123456 mysql_test table_name > C:\back\file.sql;
备份数据库到指定路径:
mysqldump -hlocalhost -uroot -p123456 -database mysql_test > C:\back\file.sql;
备份所有数据库到指定路径:
mysqldump -uroot -p123456 -all -datebases > C:\back\file.sql;

还原:mysql -uroot -p123456 mysql_test < mysql_test.sql
数据备份
备份:
select * from student 
into outfile 'D:\1.txt';

还原:
load data infile 'D:\1.txt'
into table student;

你可能感兴趣的:(数据库)