title: MySql基础总结
date: 2021-04-20 18:07:08
categories:
- 数据库
tags: - Mysql
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9BFNWdMZ-1626262228961)(/Users/tianzhiwei/MyHexo/source/_posts/2021-04-20-MySql基础总结/MySQL基本操作.png)]
基础
数据库:是一个以某种有组织的方式存储的数据集合。
表:是一种结构化的文件,可用来存储某种特定类型的数据。
模式:
- 模式可以用来描述数据库中特定的表以及整个数据库(和其中表的关系)
- 关于数据库和表的布局及特性的信息
列:表中的一个字段。数据库中每个列都有相应的数据类型,数据类型定义列可以存储的数据种类。
行:表中的数据是按行存储的,表中的一个记录。
**主键:**一列(或一组列),其值能够唯一区分表中每个行。主键用来表示一个特定的行。
满足主键的条件
- 任意两行都不具有相同的主键值;
- 每个行都必须具有一个主键值(主键列不允许NULL值)
可以一起使用多个列作为主键。
SQL structured query language 结构化查询语言。
数据库的发展史
第一代数据库:层次模型、网状模型
层次模型
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tPSJomy1-1626262228963)(/Users/tianzhiwei/MyHexo/source/_posts/2021-04-20-MySql基础总结/1.png)]
缺点:
1、 查找不同类的数据效率低了(导航的结构的缺点)
2、 数据不完整(不能区分到底是一个李白还是两个李白)
网状模型
网状模型解决了层次数据的数据不完整的问题,但是没有解决层次模型的导航问题。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lVUgRikS-1626262228964)(/Users/tianzhiwei/MyHexo/source/_posts/2021-04-20-MySql基础总结/2.png)]
关系型数据库
特点:
每个表都是独立的
表与表之间通过公共字段来建立关系
优点:解决了导航问题,并且数据完整性得到解决
缺点:多表查询效率低了
提示:我们现在用的主流的数据库都是关系模型的。
MySql安装
在
Ubuntu
中,默认情况下,只有最新版本的MySQL
包含在APT
软件包存储库中,要安装它,只需更新服务器上的包索引并安装默认包apt-get
。#命令1 sudo apt-get update #命令2 sudo apt-get install mysql-server
初始化配置
sudo mysql_secure_installation
配置项较多,如下所示:
#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的选项)
#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)
#3
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)
#4
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y (我的选项)
#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)
#6
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (我的选项)
检查mysql服务状态
systemctl status mysql.service
显示如下结果说明mysql服务是正常的:
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
Active: active (running) since Mon 2021-06-28 20:03:54 CST; 2min 21s ago
Main PID: 62268 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 2307)
Memory: 326.2M
CGroup: /system.slice/mysql.service
└─62268 /usr/sbin/mysqld
6月 28 20:03:53 ubuntu systemd[1]: Starting MySQL Community Server...
6月 28 20:03:54 ubuntu systemd[1]: Started MySQL Community Server.
lines 1-12/12 (END)
通过命令行启动\关闭
启动
使用 service 启动:service mysql start
使用 mysqld 脚本启动:/etc/inint.d/mysql start
使用 safe_mysqld 启动:safe_mysql&
停止
使用 service 启动:service mysql stop
使用 mysqld 脚本启动:/etc/inint.d/mysql stop
mysqladmin shutdown
重启
使用 service 启动:service mysql restart
使用 mysqld 脚本启动:/etc/inint.d/mysql restart
连接服务器
通过命令行面板连接
host:主机 -h
username:用户名 -u
password:密码 -p
port:端口 -P
mysql -h127.0.0.1 -P3306 -u root -ptian3281916
或者
mysql -u root -p
如果MySQL服务器在本地,IP地址可以省略;如果MySQL服务器用的是3306端口,-P也是可以省略
关闭连接
方法一:exit
方法二:quit
方法三:\q
数据库操作
显示数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.57 sec)
安装MySQL后,MySQL自带了4个数据库
- information_schema:存储了MySQL服务器管理数据库的信息。
- performance_schema:MySQL5.5新增的表,用来保存数据库服务器性能的参数
- mysql:MySQL系统数据库,保存的登录用户名,密码,以及每个用户的权限等等
- sys:通过这个库可以快速的了解系统的元数据信息。
创建数据库
语法:create database [if not exists] `数据名` [字符编码]
创建数据库:
mysql> create database stu;
Query OK, 1 row affected (0.09 sec)
创建数据库的时候判断一下数据库是否存在,如果不存在再创建
mysql> create database if not exists stu;
Query OK, 1 row affected, 1 warning (0.00 sec)
如果数据库名是关键字和特殊字符要报错
解决:在特殊字符、关键字行加上反引号
mysql> create database `create`;
Query OK, 1 row affected (0.05 sec)
多学一招:为了创建数据库时万无一失,我们可以在所有的数据库名上加上反引号
创建数据库的时候可以指定字符编码
mysql> create database teacher charset=gbk;
Query OK, 1 row affected (0.01 sec)
gbk 简体中文
gb2312: 简体中文
utf8: 通用字符编码
脚下留心:创建数据库如果不指定字符编码,默认和MySQL服务器的字符编码是一致的。