当前位置:网站首页>Mysql库的操作
Mysql库的操作
2022-07-24 14:40:00 【GSX_MI】
目录
1.创建数据库
语法
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [, create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name- 大写的表示关键字
- [] 是可选项
- CHARACTER SET: 指定数据库采用的字符集
- COLLATE: 指定数据库字符集的校验规则
2.创建数据库案例
- create database db1;
- 当我们创建数据库没有指定字符集和校验规则时,系统使用默认字符集:utf8,校验规则是:utf8_general_ ci
(2) 创建一个使用utf8字符集的 db2 数据库
- create database db2 charset=utf8;
- create database db3 charset=utf8 collate utf8_general_ci;
3.字符集和校验规则
(1)查看系统默认字符集以及校验规则
show variables like 'character_set_database';
show variables like 'collation_database';
(2)查看数据库支持的字符集
show charset;- 字符集主要是控制用什么语言。比如utf8就可以使用中文

(3)查看数据库支持的字符集校验规则
show collation;- 所谓的校验规则就是我们在对数据进行存就正常存,取的时候我们应该以什么样的校验方式(编码格式)进行对比我们的数据

(4)校验规则对数据库的影响
①不区分大小写 : 创建一个数据库,校验规则使用utf8_ general_ ci[不区分大小写]

②区分大小写 : 创建一个数据库,校验规则使用utf8_ bin[区分大小写]

③进行查询
- 不区分大小写的查询以及结果: test1

- 区分大小写的查询以及结果: test2

④结果排序
- 不区分大小写排序以及结果:test1

- 区分大小写排序以及结果:
4.操纵数据库
(1) 查看数据库
show databases;
(2)显示创建语句
show create database 数据库名;
说明:
- MySQL 建议我们关键字使用大写,但是不是必须的。
- 数据库名字的反引号 ' ' ,是为了防止使用的数据库名刚好是关键字
- /*!40100 default.... */ 这个不是注释,表示当前mysql版本大于4.01版本,就执行这句话
(3)修改数据库
ALTER DATABASE db_name [alter_spacification [,alter_spacification]...]
alter_spacification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name- 对数据库的修改主要指的是修改数据库的字符集,校验规则
实例: 将 test1 数据库字符集改成 gbk

(4)数据库的删除
DROP DATABASE [IF EXISTS] db_ name;- 数据库内部看不到对应的数据库
- 对应的数据库文件夹被删除,级联删除,里面的数据表全部被删
- 注意:不要随意删除数据库
5.备份和恢复
(1)备份
# mysqldump -P3306 -u root -p 密码 -B 数据库名 > 数据库备份存储的文件路径①示例:将test2库备份到文件(退出连接)

②查看 test.sql 文件里的内容,其实把我们整个创建数据库,建表,导入数据的语句都装载这个文 件中。

- 所谓的备份,本质是将历史上对mysql指定的数据库的所有历史操作,都做一下备份。
- mysql怎么知道我历史上都输入了那些sql命令和数据呢? mysql应该知道,binlog, 几乎全量记录对指定数据库的所有的操作!
(2)还原
mysql> source 备份数据路径

(3)注意事项
- # mysqldump -u root -p 数据库名 表名1 表名2 > D:/mytest.sql
②同时备份多个数据库
- # mysqldump -u root -p -B 数据库名1 数据库名2 ... > 数据库存放路径
6.查看连接情况
- 可以告诉我们当前有哪些用户连接到我们的MySQL,如果查出某个用户不是你正常登陆的,很有可能你的数据库被 人入侵了。以后大家发现自己数据库比较慢时,可以用这个指令来查看数据库连接情况
show processlist- id #ID标识,要kill一个语句的时候很有用
- use #当前连接用户
- host #显示这个连接从哪个ip的哪个端口上发出
- db #数据库名
- command #连接状态,一般是休眠(sleep),查询(query),连接(connect)
- time #连接持续时间,单位是秒
- state #显示当前sql语句的状态
- info #显示这个sql语句
边栏推荐
- "After 00" is coming! Digital data ushers in a new generation of "codeless" forces
- spark:获取日志中每个时间段的访问量(入门级-简单实现)
- The solution to the error of [installation detects that the primary IP address of the system is the address assigned by DHCP] when installing Oracle10g under win7
- Detailed explanation of IO model (easy to understand)
- Conversion of timestamp and time in Excel
- Native asynchronous network communication executes faster than synchronous instructions
- Atcoder beginer contest 261e / / bitwise thinking + DP
- mysql
- JS judge whether the data is empty
- The fourth edition of Zhejiang University probability proves that the uncorrelation of normal distribution random variables is equivalent to independence
猜你喜欢

Don't lose heart. The famous research on the explosive influence of Yolo and PageRank has been rejected by the CS summit

Source code analysis of ArrayList

Su Chunyuan, founder of science and technology · CEO of Guanyuan data: making business use is the key to the Bi industry to push down the wall of penetration

Similarities and differences between nor flash and NAND flash

C language -- program environment and preprocessing

2022年IAA行业品类发展洞察系列报告·第二期

正则表达和绕过案例

LeetCode·每日一题·1184.公交站间的距离·模拟

电赛设计报告模板及历年资源

Introduction to Xiaoxiong school
随机推荐
Class loading mechanism and parental delegation mechanism
2.4. properties of special profile
Time series of machine learning
LeetCode·每日一题·1184.公交站间的距离·模拟
电赛设计报告模板及
老虎口瀑布:铜梁版小壶口瀑布
Problem handling of repeated restart during Siemens botu installation
threw exception [Circular view path [index]: would dispatch back to the current handler URL [/index]
Isprs2018/ cloud detection: cloud/shadow detection based on spectral indexes for multi/hyp multi / hyperspectral optical remote sensing imager cloud / shadow detection
“00后”来了!数睿数据迎来新生代「无代码」生力军
exchange
正则表达和绕过案例
Maotai ice cream "bucked the trend" and became popular, but its cross-border meaning was not "selling ice cream"
[oauth2] II. Known changes in oauth2.1
Data analysis and mining 1
DDD based on ABP -- Entity creation and update
Stack and queue - 20. Valid parentheses
"After 00" is coming! Digital data ushers in a new generation of "codeless" forces
Summary of Baimian machine learning
Comparison of traversal speed between map and list
