mysql数据库总结
2023-12-22 07:06:57 0 举报
AI智能生成
登录查看完整内容
MySQL是一种开源的关系型数据库管理系统,支持跨平台操作,广泛应用于Web应用程序、企业系统和嵌入式系统。MySQL数据库的核心特性包括ACID(原子性、一致性、隔离性和持久性)特性,支持多种数据类型,如整数、浮点数、日期/时间、文本和二进制数据等。它提供了许多功能和工具,如连接池、事务、锁、缓存和备份与恢复机制,以保障数据的安全性和高可用性。此外,MySQL还支持集群和复制功能,以实现数据的分布式和高可用性。它的查询语言为SQL,提供多种接口,如JDBC、ODBC和Python等,与其他编程语言的集成易于实现。随着MySQL的版本更新,越来越多的新功能被添加,如JSON支持、全文检索和空间数据存储等,使得MySQL在现代应用程序中具有更广泛的应用价值。
作者其他创作
大纲/内容
主键数在一个表中,只能有一个。
字段名 字段类型 primary key
alter table 数据库表名 add primary key(字段名);
alter table 数据库表名 drop primary key;
自增长只能用在主键上
将表su id字段设置为主键自增长
alter table su modify column id int primary key auto_increment;
字段名 字段类型 primary key auto_increment
alter table 数据库表名 auto_increment=起始值;
**主键** primary key #用于id
删除记录后,对自增长字段没有影响
delete
删除记录后,自增长字段重新从1开始
truncate
记录删除后对自增的影响
字段名 字段类型 unique
**唯一** unique #姓名唯一
非空与唯一约束可以设置在N个字段上
字段名 字段类型 not null
**非空** not null
给表su 中sname设置默认字段
alter table su alter column sname set default 'zhang';
删除表su 中sname设置默认字段
alter table su alter column sname drop default;
字段名 字段类型 default 默认值
**默认值** default
[constraint][外键约束名称] foreign key(外键字段名) references 主表名称(主键字段名); #在create table时设置
alter table 从表名称 add [constraint][外键约束名称] foreign key(外键字段名) references 主表名称(主键字段名); #建好表后修改
删除外键约束
alter table 从表 drop foreign key 外键约束名称;
**子表:**定义了外键的表,外键的取值要么取父表中字段对应的值,要么取NULL值,严重受限于父表
**父表:**被引用的字段要具有唯一性(绝大多数都是用的父表的主键)
一张表的一个字段受限于另外一张表的一个字段对应的值。这里涉及到两张表:被引用的表叫主表(父表),另外一张叫从表(子表)。
**外键** foreign key
表达式中可以包含该字段名: 字段名 > 0表达式注意事项1.允许使用文字,内置函数和运算符2.不允许在使用了auto_increment的列上使用3.不允许存储函数和用户定义的函数4.不允许子查询等如果省略或指定为enforced,则创建并强制执行约束。如果指定为not enforced,则创建约束但不强制执行。
行级添加
表级添加
alter table 数据库表名 add constraint 检查约束名 check(检查约束);
表后添加
alter table 数据库表名 drop check 检查约束名;
约束删除
**检查约束** check
约束
mysqldump -u用户名 -p密码 数据库名 > sql文件位置
备份整个数据库
mysqldump -u用户名 -p密码 数据库名 数据库表名1 数据库表名2> sql文件位置
备份数据库中的某个表
mysqldump -u用户名 -p密码 --databases 数据库名1 数据库名2> sql文件位置
备份多个数据库
备份
登录mysql数据库后
进入所要导入的数据库
source 导入文件的路径;
mysql - u root -p < .sql文件
还原
mysqldump备份
show create database 数据库名;
show databases;
show character set;
show create table student;
desc 数据库表名;
show tables;
show
use 数据库名;
select database();
use
刷新数据库
flush privileges;
指令
create table 新数据库表名 like 旧数据库表名;
create database 数据库名 character set 字符集;
create database if not exists 数据库名;
create database 数据库名;
create
alter table 数据库表名 character set 字符集;
rename table 数据库表名 to 新数据库表;
alter table 数据库表名 drop 字段名;
alter table 数据库表名 change 旧字段名 新字段名 字段类型;
alter table 数据库表名 modify 字段名 新字段类型; #5.7版本可以改int类型 8.0版本改后不显示
alter table 数据库表名 add 字段名 字段类型;
alter
drop table if exists 数据库表名;
drop table 数据库表名;
drop database 数据库名;
drop
DDL
insert [into] 数据库表名 [字段名列表] values(字段值列表)
insert
update
truncate table 数据库表名; #删除表中所有的记录
delete from 数据库表名;
delete from 数据库表名 [where 条件表达式];
DML
select distinct sex from student;
select distinct 字段名 from 数据库表名;
select 字段名+固定值 from 数据库表名;
select 字段名+字段名 from 数据库表名;
select * from 数据库表名;
记录查询
select * from students where english>80;
select * from students where english<80;
select * from students where age<=19;
select * from students where math=88;
select * from students where age<>18;
select * from students where age>=19;
>、<、<=、>=、=、<>
基础运算符
select * from students where english between 80 and 90;
between...and...
select * from students where sname like '张%';
% :匹配任意多个字符
select * from students where sname like '王_';
_ :匹配一个字符
指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
select */字段列表 from 数据库表名 where 字段名 regexp [字符串]字符串;
[ ]:表示括号内所列字符中的一个(类似正则表达式)。
其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符。
select */字段列表 from 数据库表名 where 字段名 regexp [^字符串]字符串;
[^ ] :表示不在括号所列之内的单个字符。
mysql通配符
可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示。
like '%值%'
select * from students where age is null;
is null #表示某字段值为null
select * from students where english is not null;
is not null #表示某字段值不为null
提高运算符
条件查询
select * from students where english>85 and sex='男';
and #与,二边条件同时成立,成立 &&
select * from students where age>=20 or math>85;
or #或,二边条件有一边成立,成立 ||
select * from students where not (age>=20 or math>85);
not #非,一边条件成立,取反;一边条件不成立,取反 !
逻辑运算符
select */字段列表 from 数据库表名 [where 条件表达式];
select * from students where english is not null order by english;
select */字段列表 from 数据库表名 [where 条件表达式] [order by 字段名 [asc/desc]];
排序
select sex from students group by sex;
where #where后面不能用聚合函数
select count(*) from students where age>19;
select sum(english) as 英语总分 from students;
select avg(english) as 英语平均分 from students;
select max(english) as 英语最高分 from students;
select min(english) as 英语最低分 from students;
select 聚合函数(字段名) from 数据库表名;
count(字段名):统计总行数sum(字段名):计算列总和avg(字段名):求某一列平均值min(字段名):求某一列的最小值max(字段名):求某一列的最大值
聚合函数基础
聚合函数分组
group_concat #放置每一组的某字段的值的集合。
with rollup #在最后新增一行,group by分组字段的基础之上在进行统计数据
聚合函数应用扩展
having #having是先分组,在将分组后不符合条件的去掉。
select */字段列表 from 数据库表名 [group by 分组字段名 [having 条件表达式]];
分组
offset:起始行数,从 0 开始计数,如果省略,默认就是 0
length: 返回的行数
limit分页
select
DQL
select * from user;
查看用户
**注意:****用户名:** 新用户的名字**主机名:** 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%**密码:** 密码可以为空,如果为空则该用户可以不需要密码登陆服务器
create user '用户名'@'主机名' identified by '密码';
创建用户
drop user '用户名'@'主机名';
删除用户
用户管理
alter user '用户名'@'主机名' identified by '密码';
修改用户密码
use mysql;
update user set authentication_string='' where user='root';
清空密码
alter user 'root'@'%' identified by 'Root12345';
alter user 'root'@'localhost' identified by 'Root12345';
设置密码
设置管理员(root)密码
密码管理
show grants for '用户名'@'主机名';
查看用户权限
**grant… on …to :**授权关键字**权限:** 如select\\insert\\update\\delete等,如果是所有用all**数据库名.表名:** 该用户可以操作哪个数据库的哪些表。如果要授予该用户对所有数据库和表的相应操作权限则可用*表示,如*.***’用户名’@‘主机名’ :** 注意单引号不能省略
授权
**revoke… on …from :** 撤消授权关键字**权限:** 如select\\insert\\update\\delete等,如果是所有用all**数据库名.表名:** 该用户可以操作哪个数据库的哪些表。如果要撤消授予该用户对所有数据库和表的相应操作权限则可用*表示,如*.***’用户名’@‘主机名’ :** 注意单引号不能省略
撤销授权
权限管理
DCL
mysl数据库
收藏
0 条评论
回复 删除
下一页