1.关系型数据介绍:
1.1数据结构模型
数据结构模型主要有:
层次模型
网状模型
关系模型
关系模型:
二维关系:row,column
数据库管理系统:DBMS
关系:Relational,RDBMS
1.2RDBMS专业名词
常见的关系型数据库管理系统:
MySQL :MySQL,MariaDB,Percona-Server
Oracle
MSSQL
事务:多个操作被当作一个整体对待就称为一个事务
要看一个关系型数据库是否支持事务,需要看其是否支持并满足ACID测试
ACID:ACID是事务的一个基本标准
A:Automicity 原子性
C:Consistency 一致性
I:Isolation 隔离性
D:Durability 持久性
约束:constraint,向数据表提供的数据要遵守的限制
主键约束:一个或多个字段的组合。填写的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL) /一个表只能存在一个
唯一键约束:一个或多个字段的阻隔,填入的数据必须能在本表中唯一标识本行,允许为空(NULL) //一个表可以存在多个
外键约束:一个表中的某个字段可以填入数据取决于另一个表的主键已有的数据
检查性约束
索引:
将表中的一个或多个字段中的数据复制一份另存,并且这些数据按特定次序存储。
关系运算
选择:挑选出符合条件的行(部分行)
投影:挑选出需要的字段
链接
数据抽象方式:
物理层:决定数据的存储格式,即RDBMS在磁盘上如何组织文件
逻辑层:描述DB存储什么数据,以及数据间存在什么样的关系
视图层:描述DB中的部分数据
1.3关系型数据的常见组件:
关系型数据常见组件有:
数据库:database
表:table,由行(row)和列(column)组成
索引:index
视图:view
用户:user
权限:privilege
存储过程:procedure
存储函数:function
触发器:trigger
事件调度器:event scheduler
1.4 SQL语句
SQL语句有三种类型:
DDL:Data Defination Language 数据定义语言
DML:Data Manipulation Language 数据操纵语言
DCL:Data Control Language 数据控制语言
SQL语句类型
对应操作
DDL
create 创建 drop 删除 alter 编辑
DML
insert 向表中插入数据 delete 删除表中数据 update 更新表中数据select 查询表中数据
DCL
grant 授权 revoke 移除授权
2 .MySQL的安装与配置
//关闭防火墙和SELINUX
[root@linfan ~]# systemctl stop firewalld
[root@linfan ~]# systemctl disable firewalld
[root@linfan ~]# sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@linfan ~]# setenforce 0
2.1 MySQL的安装
//配置mysql的yum源
[root@linfan ~]# cd /usr/src
[root@linfan src]# ls
debug kernels
[root@linfan src]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@linfan src]# ls
debug kernels mysql57-community-release-el7-10.noarch.rpm
[root@linfan src]# yum -y install mysql57-community-release-el7-10.noarch.rpm
//安装mysql5.7
[root@linfan src]# yum -y install mysql-community-server mysql-community-c lient mysql-community-common mysql-community-devel
Loaded plugins: fastestmirror
mysql-connectors-community | 2.5 kB 00:00:00
mysql-tools-community | 2.5 kB 00:00:00
mysql57-community | 2.5 kB 00:00:00
(1/3): mysql-connectors-community/x86_64/primary_db | 25 kB 00:00:01
(2/3): mysql-tools-community/x86_64/primary_db | 45 kB 00:00:02
(3/3): mysql57-community/x86_64/primary_db | 152 kB 00:00:02
Loading mirror speeds from cached hostfile
No package mysql-community-c available.
No package lient available.
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-common.x86_64 0:5.7.23-1.el7 will be installed
---> Package mysql-community-devel.x86_64 0:5.7.23-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-devel-5.7.23-1.el7.x86_64
--> Processing Dependency: libmysqlclient.so.20()(64bit) for package: mysql-community-devel-5.7.23-1.el7.x86_64
---> Package mysql-community-server.x86_64 0:5.7.23-1.el7 will be installed
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.23-1.el7.x86_64
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.56-2.el7 will be obsoleted
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
---> Package mysql-community-client.x86_64 0:5.7.23-1.el7 will be installed
---> Package mysql-community-libs.x86_64 0:5.7.23-1.el7 will be obsoleting
--> Running transaction check
---> Package mysql-community-libs-compat.x86_64 0:5.7.23-1.el7 will be obsoleting
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================================================================
Package Arch Version Repository Size
==========================================================================================================================
Installing:
mysql-community-common x86_64 5.7.23-1.el7 mysql57-community 274 k
mysql-community-devel x86_64 5.7.23-1.el7 mysql57-community 3.6 M
mysql-community-libs x86_64 5.7.23-1.el7 mysql57-community 2.2 M
replacing mariadb-libs.x86_64 1:5.5.56-2.el7
mysql-community-libs-compat x86_64 5.7.23-1.el7 mysql57-community 2.0 M
replacing mariadb-libs.x86_64 1:5.5.56-2.el7
mysql-community-server x86_64 5.7.23-1.el7 mysql57-community 165 M
Installing for dependencies:
mysql-community-client x86_64 5.7.23-1.el7 mysql57-community 24 M
Transaction Summary
==========================================================================================================================
Install 5 Packages (+1 Dependent package)
Total download size: 197 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.23-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-common-5.7.23-1.el7.x86_64.rpm is not installed
(1/6): mysql-community-common-5.7.23-1.el7.x86_64.rpm | 274 kB 00:00:00
(2/6): mysql-community-devel-5.7.23-1.el7.x86_64.rpm | 3.6 MB 00:00:01
(3/6): mysql-community-libs-5.7.23-1.el7.x86_64.rpm | 2.2 MB 00:00:01
(4/6): mysql-community-libs-compat-5.7.23-1.el7.x86_64.rpm | 2.0 MB 00:00:01
(5/6): mysql-community-client-5.7.23-1.el7.x86_64.rpm | 24 MB 00:00:05
(6/6): mysql-community-server-5.7.23-1.el7.x86_64.rpm | 165 MB 00:00:21
--------------------------------------------------------------------------------------------------------------------------
Total 7.3 MB/s | 197 MB 00:00:27
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
Userid : "MySQL Release Engineering "
Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
Package : mysql57-community-release-el7-10.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql-community-common-5.7.23-1.el7.x86_64 1/7
Installing : mysql-community-libs-5.7.23-1.el7.x86_64 2/7
Installing : mysql-community-client-5.7.23-1.el7.x86_64 3/7
Installing : mysql-community-server-5.7.23-1.el7.x86_64 4/7
Installing : mysql-community-libs-compat-5.7.23-1.el7.x86_64 5/7
Installing : mysql-community-devel-5.7.23-1.el7.x86_64 6/7
Erasing : 1:mariadb-libs-5.5.56-2.el7.x86_64 7/7
Verifying : mysql-community-server-5.7.23-1.el7.x86_64 1/7
Verifying : mysql-community-libs-5.7.23-1.el7.x86_64 2/7
Verifying : mysql-community-client-5.7.23-1.el7.x86_64 3/7
Verifying : mysql-community-libs-compat-5.7.23-1.el7.x86_64 4/7
Verifying : mysql-community-common-5.7.23-1.el7.x86_64 5/7
Verifying : mysql-community-devel-5.7.23-1.el7.x86_64 6/7
Verifying : 1:mariadb-libs-5.5.56-2.el7.x86_64 7/7
Installed:
mysql-community-common.x86_64 0:5.7.23-1.el7 mysql-community-devel.x86_64 0:5.7.23-1.el7
mysql-community-libs.x86_64 0:5.7.23-1.el7 mysql-community-libs-compat.x86_64 0:5.7.23-1.el7
mysql-community-server.x86_64 0:5.7.23-1.el7
Dependency Installed:
mysql-community-client.x86_64 0:5.7.23-1.el7
Replaced:
mariadb-libs.x86_64 1:5.5.56-2.el7
Complete!
2.2配置mysql
//启动mysql
[root@linfan ~]# systemctl start mysqld
[root@linfan ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2018-08-16 10:36:42 HKT; 14s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 2244 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 2171 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 2248 (mysqld)
CGroup: /system.slice/mysqld.service
└─2248 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Aug 16 10:36:39 linfan systemd[1]: Starting MySQL Server...
Aug 16 10:36:42 linfan systemd[1]: Started MySQL Server.
//确保3306端口已经开启
[root@linfan ~]# ss -natl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
//在日志中找出临时密码
[root@linfan ~]# grep 'password' /var/log/mysqld.log
2018-08-16T02:36:40.079131Z 1 [Note] A temporary password is generated for root@localhost: a007Jilj-rtr
//使用临时密码登录:
[root@linfan ~]# mysql -root -p
Enter password: //此处输入临时密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.23
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> //此处表示登录成功
//修改mysql的登录密码:
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> set password=password('linfan123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
或者 alter user root@localhost identified by ‘linfan123’
//为了避免mysql自动升级,这里需要卸载最开始安装的yum源
[root@linfan ~]# rpm -qa |grep mysql
mysql-community-server-5.7.23-1.el7.x86_64
mysql57-community-release-el7-10.noarch
mysql-community-common-5.7.23-1.el7.x86_64
mysql-community-client-5.7.23-1.el7.x86_64
mysql-community-libs-compat-5.7.23-1.el7.x86_64
mysql-community-libs-5.7.23-1.el7.x86_64
mysql-community-devel-5.7.23-1.el7.x86_64
[root@linfan ~]# yum -y remove mysql57-community-release-el7-10.noarch
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package mysql57-community-release.noarch 0:el7-10 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================================================================
Package Arch Version Repository Size
==========================================================================================================================
Removing:
mysql57-community-release noarch el7-10 installed 30 k
Transaction Summary
==========================================================================================================================
Remove 1 Package
Installed size: 30 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing : mysql57-community-release-el7-10.noarch 1/1
Verifying : mysql57-community-release-el7-10.noarch 1/1
Removed:
mysql57-community-release.noarch 0:el7-10
Complete!
4 .mysql数据库
4.1.1 数据库创建
//创建数据库
//语法: CREATE DATABASE [IF NOT EXISTS] 'DB_NAME';
/创建数据库linfan
mysql> create database if not exists linfan;
Query OK, 1 row affected (0.00 sec)
//查看当前有哪些数据库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linfan |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
//删除数据库
//语法:DROP DATABASE [IF EXISTS] 'DB_NAME';
//删除数据库linfan
mysql> drop database if exists linfan;
Query OK, 0 rows affected (0.00 sec)
4.1.2表操作
//创建表
//语法:CREATE TABLE table_name (col1 datatype 修饰符,col2 datatype修饰符) ENGINE='存储引擎类型';
//在数据库linfan中创建表linfan
mysql> use linfan;
Database changed
mysql> create table linfan (id int NOT NULL,name VARCHAR(100) NOT NULL,age tinyint);
Query OK, 0 rows affected (0.02 sec)
mysql>
//查看当前有哪些表
mysql> show tables;
+------------------+
| Tables_in_linfan |
+------------------+
| linfan |
+------------------+
1 row in set (0.00 sec)
mysql>
//删除表
//语法: DROP TABLE [ IF EXISTS ] 'table_name';
4.1.3 用户操作
mysql用户账户由两部分组成,如‘USERNAME’@‘HOST’,表示ciUSERNAME只能从此HOST上远程登录
这里(‘USERNAME’@‘HOST’)的HOST的HOST用于限制此用户可通过哪些主机远程连接mysql程序
其值可为
IP地址:127.0.0.1
通配符
%:匹配任意长度的任意字符,常用于设置允许从任何主机登录
_:匹配任意单个字符
//数据库用户创建
//语法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
//创建数据库用户linfan
mysql> create user 'linfan'@'127.0.0.1' identified by 'linfan123';
Query OK, 0 rows affected (0.01 sec)
mysql>
//使用新建的用户linfan登录mysql
[root@linfan ~]# mysql -ulinfan -p -h127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.23 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
//删除数据库用户
//语法:DROP USER 'username'@'host';
//删除数据库用户linfan
mysql> drop user 'linfan'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)
###4.1.5 获取帮助
//获取命令使用帮助
//语法:HELP keyword;
//获取创建表的帮助
mysql> help create table;
##DML操作
DML操作包括增(INSERT)、删(DELETE)、改(UPDATE)、查(SELECT)
INSERT语句
//增操作insert
//语法:INSERT [INTO] table_name [(column_name,...)] {VALUES | VALUE} (value 1,...),(...),...
mysql> use linfan;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> insert into linfan (id,name,age) VALUE (1,'tom',20);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO linfan (id,name,age) VALUES (2,'jerry',23),(3,'wangqin g',25),(4,'sean',28),(5,'zhangshan',26),(6,'zhangshan',20),(7,'lisi',NULL);
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
4.2.2 SELECT语句
字段column表示法:
*:所有字段
as:字段别名,如col1 as alist1,当表名很长时用别名代替
判断语句 WHERE
操作类型
常用操作符
操作符
>,=,<=,=,!= BETWEEN column# ANDcolumn#LIKE:模糊匹配RLIKE:基于正则表达式进行模式匹配IS NOT NULL 非空IS NULL 空
条件逻辑操作
AND RO NOT
ORDER BY 语句,排序,默认为升序(ASC)
ORDER BY 语句
意义
ORDER BY ‘column_name’
进行升序排序
ORDER BY ‘column_name’ DESC
进行降序排序
ORDER BY ‘column_name’LIMIT2
进行升序排序,只取前面2个结果
ORDER BY ‘column_name’LIMIT1,2
行升序排序,并且略过第1个结果取后面2个结果
//查操作
//语法;SELECT column1,column2,... FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
mysql> use linfan ;
Database changed
mysql> select * from linfan;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
+----+-------------+------+
7 rows in set (0.00 sec)
mysql> select * from linfan order by age;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 7 | lisi | NULL |
| 1 | tom | 20 |
| 6 | zhangshan | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 5 | zhangshan | 26 |
| 4 | sean | 28 |
+----+-------------+------+
7 rows in set (0.01 sec)
4.2.3. update语句
//改操作
//语法:UPDATE table_name SET column1 = new_value1[,column2 = new_value2,... ] [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
mysql> select * from linfan;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
+----+-------------+------+
7 rows in set (0.00 sec)
mysql> update linfan set age = 30 where name= 'lisi';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from linfan;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | 30 |
+----+-------------+------+
7 rows in set (0.00 sec)
mysql>
4.2.4 delete语句
//删操作
//语法:DELETE FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC] ] [LIMIT [m,]n];
mysql> delete from linfan where id = 3 ;//删除某条记录
Query OK, 1 row affected (0.00 sec)
mysql> select * from linfan;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | 30 |
+----+-----------+------+
6 rows in set (0.00 sec)
mysql> delete from linfan;
Query OK, 6 rows affected (0.00 sec)
mysql> select * from linfan; //删除整张表
Empty set (0.00 sec)
mysql> desc linfan;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
4.3 授权
4.3.1创建授权grant
权限类型
意义
ALL
所有权限
DELETE
删除内容的权限
SELECT
读取内容权限
INSERT
插入内容的权限
UPDATE
更新内容权限
指定要操作的对象db_name.table_name
表示方式
意义
.
所有库的所有表
db_name
指定库的所有表
db_name.table_name
指定库的指定表
//授权
//语法:
GRANT priv_type,... ON [object_type] db_name.table_name TO ‘username'@'host
' [IDENTIFIED BY 'password'] [WITH GRANT OPTION];
//授权linfan用户在数据库本机上登录访问所有数据库
mysql> GRANT ALL ON *.* TO 'linfan'@'127.0.0.1' IDENTIFIED BY 'linfan123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
//授权linfan用户在远程主机192.168.24.188上登录访问linfan数据库
mysql> GRANT ALL ON linfan.* TO 'wangqing'@'192.168.24.188' IDENTIFIED BY 'linfan123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
//授权linfan用户在所有位置上都可以远程访问linfan数据库
mysql> GRANT ALL ON linfan.* TO 'wangqing'@'%' IDENTIFIED BY 'linfan123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
4.3.2查看授权
//查看当前登录用户的授权信息
mysql> SHOW GRANTS;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
//查看指定用户的授权信息:
mysql> SHOW GRANTS for 'linfan'@'127.0.0.1';
+-----------------------------------------------------+
| Grants for linfan@127.0.0.1 |
+-----------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'linfan'@'127.0.0.1' |
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql>
4.3.取消授权
//语法:REVOKE priv_type,... ON db_name.table_name FROM 'username'@'host';
mysql> REVOKE ALL ON *.* FROM 'wangqing'@'192.168.24.188';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES; //刷新权限
Query OK, 0 rows affected (0.00 sec)
注意:mysql服务进程启动时会读取mysql的所有授权表至内存中;
grant或revoke等执行权限操作会保存于表中,mysql的进程会自动读取授权表并更新在内存中
对于不能够或不能及时重读授权表的命令,可手动让mysql的服务进程重读授权表