1关系型数据库介绍
1.1 数据结构模型
数据结构模型主要有:
- 层次模型
- 网状结构
- 关系模型
关系模型:
二维关系:row,column
数据库管理系统:DBMS
关系:Relational,RDBMS
1.2 RDBMS专业名词
常见的关系型数据库管理系统:
-
MySQL:MySQL,MariaDB,Percona-Server
-
PostgreSQL:简称为pgsql
-
Oracle
-
MSSQL
事务:多个操作被当作一个整体对待就称为一个事务
要看一个关系型数据库是否支持事务,需要看其是否支持并满足ACID测试
ACID:ACID是事务的一个基本标准 -
A:Automicity,原子性
-
C:Consistency,一致性
-
I:Isolation,隔离性
-
D:Durability,持久性
如果你对ACID感兴趣,可以查看这里了解详细说明,ACID将不作为我们讲解的重点。
SQL:Structure Query Language,结构化查询语言
约束: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安装与配置
2.1 mysql安装
mysql安装方式有三种:
- 源代码:编译安装
- 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
- 程序包管理器管理的程序包:
- rpm:有两种
- OS Vendor:操作系统发行商提供的
- 项目官方提供的
- deb
- rpm:有两种
//启动mysql
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2018-08-12 23:39:33 CST; 6s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 1325 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 1249 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 1327 (mysqld)
CGroup: /system.slice/mysqld.service
└─1327 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/my...
Aug 12 23:39:26 localhost.localdomain systemd[1]: Starting MySQL Server...
Aug 12 23:39:33 localhost.localdomain systemd[1]: Started MySQL Server.
//确保3306端口已经监听起来
[root@localhost ~]# ss -antl
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@localhost ~]# grep "password" /var/log/mysqld.log
2018-08-12T15:39:28.710830Z 1 [Note] A temporary password is generated for root@localhost: &vsD!YuKT7&/
//此处的临时密码为&vsD!YuKT7&/
//注意,你的密码跟这是不一样的,一定要看清楚,禁止直接复制我这里的密码
//使用获取到的临时密码登录mysql
[root@localhost ~]# mysql -uroot -p
Enter password: //此处输入密码,可以直接复制你的密码粘贴至此处,也可手动输入
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
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.01 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'wangqing123!';
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
//为避免mysql自动升级,这里需要卸载最开始安装的yum源
[root@localhost ~]# 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@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-
: manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
....
Removed:
mysql57-community-release.noarch 0:el7-10
Complete!
3.mysql的程序组成
- 客户端
- mysql:CLI交互式客户端程序
- mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
- mysqldump:mysql备份工具
- mysqladmin
- 服务器端
- mysqld
3.1 mysql工具使用
//语法:mysql [OPTIONS] [database]
//常用的OPTIONS:
-uUSERNAME //指定用户名,默认为root
-hHOST //指定服务器主机,默认为localhost,推荐使用ip地址
-pPASSWORD //指定用户的密码
-P# //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
-V //查看当前使用的mysql版本
-e //不登录mysql执行sql语句后退出,常用于脚本
[root@localhost ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using EditLine wrapper
[root@localhost ~]# mysql -uroot -pwangqing123! -h127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
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>
//注意,不推荐直接在命令行里直接用-pPASSWORD的方式登录,而是使用-p选项,然后交互式输入密码
[root@localhost ~]# mysql -uroot -p -h127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
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>
[root@localhost ~]# mysql -uroot -p -h 127.0.0.1 -e 'SHOW DATABASES;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wangqingge |
+--------------------+
3.2 服务器监听的两种socket地址
socket类型 | 说明 |
---|---|
ip socket | 默认监听在tcp的3306端口,支持远程通信 |
unix sock | 监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock 仅支持本地通信 server地址只能是:localhost,127.0.0.1 |
实验:
//搭建mysql
[root@wbk ~]#wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@wbk ~]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel
[root@wbk ~]# systemctl start mysqld----启动mysql
[root@wbk ~]# ss -anlt
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 :::*
//创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age)
[root@wbk ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@wbk ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@wbk ~]# grep "password" /var/log/mysqld.log
2019-01-17T10:20:13.001062Z 1 [Note] A temporary password is generated for root@localhost: aywf.!GRg8x-
[root@wbk ~]# mysql -uroot -p
Enter password:
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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'wangqing123!';
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE DATABASE IF NOT EXISTS xiaojian;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wangbokang |
+--------------------+
5 rows in set (0.00 sec)
mysql> CREATE TABLE xiaojian(id int NOT NULL,name VARCHAR(50) NOT NULL,age tinyint);
Query OK, 0 rows affected (0.02 sec)
mysql> SHOW TABLES;
+--------------------+
| Tables_in_xiaojian |
+--------------------+
| wangbokang |
+--------------------+
1 row in set (0.01 sec)
mysql> DESC xiaojian.xiaojian;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(50) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.29 sec)
---------------------
//查看该新建的表有无内容
mysql> select * from wangbokang;
Empty set (0.00 sec)
//往新建的student表中插入数据
mysql> INSERT wangbokang (id,name,age) VALUES (1,'tom',20),(2,'jerry',23),(3,'wangqing',25),(4,'sean',28),(5,'zhangshan',26),(6,'zhangshan',20),(7,'lisi',null),(8,'chenshuo',10),(9,'wangwu',3),(10,'qiuyi',15),(11,'qiuxiaotian',20);
Query OK, 11 rows affected (0.01 sec)
Records: 11 Duplicates: 0 Warnings: 0
mysql> select * from wangbokang;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 24 |
| 3 | wangqing | 35 |
| 4 | sean | 26 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 40 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 31 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.00 sec)
//修改lisi的年龄为50
mysql> UPDATE wangbokang set age = 50 where name = 'lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from xiaojian;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 24 |
| 3 | wangqing | 35 |
| 4 | sean | 26 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 40 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 31 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.00 sec)
//以age字段降序排序
mysql> select * from wangbokang ORDER BY age DESC;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 7 | lisi | 50 |
| 6 | zhangshan | 40 |
| 3 | wangqing | 35 |
| 9 | wangwu | 31 |
| 4 | sean | 26 |
| 5 | zhangshan | 26 |
| 2 | jerry | 24 |
| 1 | tom | 20 |
| 11 | qiuxiaotian | 20 |
| 10 | qiuyi | 15 |
| 8 | chenshuo | 10 |
+----+-------------+------+
11 rows in set (0.00 sec)
//查询student表中年龄最小的3位同学
mysql> select * from wangbokang ORDER BY age limit 2;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 10 | qiuyi | 15 |
| 8 | chenshuo | 10 |
+----+----------+------+
2 rows in set (0.00 sec)
mysql>
//查询student表中年龄最大的4位同学
mysql> select * from wangbokang ORDER BY age DESC limit 4;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 7 | lisi | 50 |
| 6 | zhangshan | 40 |
| 3 | wangqing | 35 |
| 9 | wangwu | 31 |
+----+-----------+------+
4 rows in set (0.00 sec)
mysql>
//查询student表中名字叫zhangshan的记录
mysql> select * from wangbokang WHERE name = 'zhangshan';
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 6 | zhangshan | 40 |
| 5 | zhangshan | 26 |
+----+-----------+------+
2 rows in set (0.00 sec)
mysql>
//查询student表中名字叫zhangshan且年龄大于20岁的记录
mysql> select * from wangbokang WHERE name = 'zhangshan' AND age > 20;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 5 | zhangshan | 40 |
| 5 | zhangshan | 26 |
+----+-----------+------+
1 row in set (0.00 sec)
mysql>
//查询student表中年龄在23到30之间的记录
mysql> select * from wangbokang WHERE age BETWEEN 23 and 30;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 4 | sean | 26 |
| 5 | zhangshan | 26 |
| 2 | jerry | 24 |
+----+-----------+------+
4 rows in set (0.00 sec)
mysql>
//修改wangwu的年龄为100
mysql> UPDATE wangbokang set age = 100 where name = 'wangwu';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from wangbokang;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.00 sec)
mysql>
//删除student中名字叫zhangshan且年龄小于等于20的记录
mysql> delete from wangbokang where name = 'zhangshan' and age <= 20;
Query OK, 1 row affected (0.00 sec)
mysql> select * from wangbokang;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 24 |
| 3 | wangqing | 35 |
| 4 | sean | 26 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 40 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 31 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
10 rows in set (0.00 sec)