关系型数据库介绍

数据结构模型

数据结构模型主要有:

  • 层次模型
  • 网状结构
  • 关系模型

关系模型:

二维关系:row,column

数据库管理系统:DBMS

关系:Relational,RDBMS

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中的部分数据

关系型数据库的常见组件

关系型数据库的常见组件有:

  • 数据库:database
  • 表:table,由行(row)和列(column)组成
  • 索引:index
  • 视图:view
  • 用户:user
  • 权限:privilege
  • 存储过程:procedure
  • 存储函数:function
  • 触发器:trigger
  • 事件调度器:event scheduler

SQL语句

SQL语句有三种类型:

  • DDL:Data Defination Language,数据定义语言

    • CREATE:创建
    • DROP:删除
    • ALTER:修改
  • DML:Data Manipulation Language,数据操纵语言
    • INSERT:向表中插入数据
    • DELETE:删除表中数据
    • UPDATE:更新表中数据
    • SELECT:查询表中数据
  • DCL:Data Control Language,数据控制语言
    • GRANT:授权
    • REVOKE:移除授权

mysql安装与配置

安装mysql

  1. #配置yum源
  2. [root@YingMo ~]# cd /usr/src/
  3. [root@YingMo src]# ls
  4. debug kernels
  5. [root@YingMo src]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
  6. [root@YingMo src]# yum -y install mysql57-community-release-el7-10.noarch.rpm
  7. [root@YingMo src]# ls /etc/yum.repos.d/
  8. CentOS-Base.repo epel.repo mysql-community.repo mysql-community-source.repo
  9. #可以看到已经有了mysql源了
  10. #接下来安装mysql,安装mysql涉及四个包,分别为mysql-community-server、mysql-community-client、mysql-community-common和mysql-community-devel
  11. [root@YingMo src]yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel

配置mysql

  1. #先启动mysql
  2. [root@YingMo ~]# systemctl start mysqld
  3. [root@YingMo ~]# systemctl status mysqld
  4. mysqld.service - MySQL Server
  5. Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
  6. Active: active (running) since Fri 2019-02-15 15:00:12 CST; 8s ago
  7. Docs: man:mysqld(8)
  8. http://dev.mysql.com/doc/refman/en/using-systemd.html
  9. Process: 26071 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  10. Process: 25997 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
  11. Main PID: 26075 (mysqld)
  12. CGroup: /system.slice/mysqld.service
  13. └─26075 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
  14. Feb 15 15:00:08 YingMo systemd[1]: Starting MySQL Server...
  15. Feb 15 15:00:12 YingMo systemd[1]: Started MySQL Server.
  16. #确保3306端口处于监听状态
  17. [root@YingMo ~]# ss -antl
  18. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  19. LISTEN 0 128 *:22 *:*
  20. LISTEN 0 80 :::3306 :::*
  21. #找到临时密码
  22. [root@YingMo ~]# grep "password" /var/log/mysqld.log
  23. 2019-02-15T07:00:09.467221Z 1 [Note] A temporary password is generated for root@localhost: g(=mj,K_r1gg
  24. #使用临时密码登陆mysql
  25. [root@YingMo ~]# mysql -uroot -p
  26. Enter password:
  27. Welcome to the MySQL monitor. Commands end with ; or \g.
  28. Your MySQL connection id is 2
  29. Server version: 5.7.25
  30. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  31. Oracle is a registered trademark of Oracle Corporation and/or its
  32. affiliates. Other names may be trademarks of their respective
  33. owners.
  34. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  35. mysql>
  36. #看到上面“mysql>”代表登录成功
  37. #修改mysql登陆密码
  38. #将密码安全性检查调到LOW
  39. mysql> set global validate_password_policy=0;
  40. Query OK, 0 rows affected (0.00 sec)
  41. #将密码最大长度设置为1(因为是测试环境,所以改成了1,生产环境最好还是长一些)
  42. mysql> set global validate_password_length=1;
  43. Query OK, 0 rows affected (0.00 sec)
  44. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'lynk';
  45. Query OK, 0 rows affected (0.00 sec)
  46. #如果不想改变validate_password_policy参数,保持默认,那么密码必须包含有数字、小写或大写字母、特殊字符
  47. mysql> quit
  48. Bye
  49. #为了防止更新我们要卸掉最开始安装的yum源。因为更新后可能会存在不稳定的情况,这种问题还是能避免就避免一下的比较好。
  50. [root@YingMo ~]# rpm -qa|grep mysql
  51. mysql-community-common-5.7.25-1.el7.x86_64
  52. mysql-community-libs-5.7.25-1.el7.x86_64
  53. mysql-community-server-5.7.25-1.el7.x86_64
  54. mysql57-community-release-el7-10.noarch
  55. mysql-community-libs-compat-5.7.25-1.el7.x86_64
  56. mysql-community-client-5.7.25-1.el7.x86_64
  57. mysql-community-devel-5.7.25-1.el7.x86_64
  58. [root@YingMo ~]# yum -y remove mysql57-community-release-el7-10.noarch

mysql工具的使用

  1. #语法:mysql [OPTIONS] [database]
  2. #常用OPTIONS:
  3. -uUSERNAME 指定用户名,默认root
  4. -hHOST 指定服务器主机,默认为localhost,推荐使用ip地址
  5. -pPASSWORD 指定用户的密码
  6. -P# 指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
  7. -V 查看当前使用的mysql版本
  8. -e 不登录mysql执行sql语句后退出,常用于脚本
  1. [root@YingMo ~]# mysql -uroot -p -h127.0.0.1
  2. #注意:不建议在命令行中直接使用密码登陆,因为日志系统会将用户命令记录下来,导致密码泄露。建议使用-p选项,用交互式命令输入密码。

服务器监听的两种socket地址

  • ip socket:默认监听在tcp的3306端口,支持远程通讯
  • unix socket:监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock)仅支持本地通讯
    • server地址只能是:localhost,127.0.0.1

应用实例

创建一个以lynk为名的数据库,创建一张表student,该表包含三个字段(id,name,age),表结构如下

  1. mysql> desc student;
  2. +-------+--------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +-------+--------------+------+-----+---------+-------+
  5. | id | int(11) | NO | | NULL | |
  6. | name | varchar(100) | NO | | NULL | |
  7. | age | tinyint(4) | YES | | NULL | |
  8. +-------+--------------+------+-----+---------+-------+
  9. 3 rows in set (0.00 sec)
  1. mysql> use lynk;
  2. Database changed
  3. mysql> CREATE TABLE student (id int(11),name varchar(100),age tinyint(4) NULL);
  4. Query OK, 0 rows affected (0.20 sec)
  5. mysql> desc student;
  6. +-------+--------------+------+-----+---------+-------+
  7. | Field | Type | Null | Key | Default | Extra |
  8. +-------+--------------+------+-----+---------+-------+
  9. | id | int(11) | YES | | NULL | |
  10. | name | varchar(100) | YES | | NULL | |
  11. | age | tinyint(4) | YES | | NULL | |
  12. +-------+--------------+------+-----+---------+-------+
  13. 3 rows in set (0.15 sec)

查看下该新建的表有无内容(用select语句)

  1. mysql> select * from student;
  2. Empty set (0.00 sec)

往新建的student表中插入数据(用insert语句)

  1. mysql> insert student 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);
  2. Query OK, 11 rows affected (0.04 sec)
  3. Records: 11 Duplicates: 0 Warnings: 0
  4. mysql> select * from student;
  5. +------+-------------+------+
  6. | id | name | age |
  7. +------+-------------+------+
  8. | 1 | tom | 20 |
  9. | 2 | jerry | 23 |
  10. | 3 | wangqing | 25 |
  11. | 4 | sean | 28 |
  12. | 5 | zhangshan | 26 |
  13. | 6 | zhangshan | 20 |
  14. | 7 | lisi | NULL |
  15. | 8 | chenshuo | 10 |
  16. | 9 | wangwu | 3 |
  17. | 10 | qiuyi | 15 |
  18. | 11 | qiuxiaotian | 20 |
  19. +------+-------------+------+
  20. 11 rows in set (0.00 sec)

修改lisi的年龄为50

  1. mysql> update student set age = 50 where name = 'lisi';
  2. Query OK, 1 row affected (0.80 sec)
  3. Rows matched: 1 Changed: 1 Warnings: 0
  4. mysql> select * from student;
  5. +------+-------------+------+
  6. | id | name | age |
  7. +------+-------------+------+
  8. | 1 | tom | 20 |
  9. | 2 | jerry | 23 |
  10. | 3 | wangqing | 25 |
  11. | 4 | sean | 28 |
  12. | 5 | zhangshan | 26 |
  13. | 6 | zhangshan | 20 |
  14. | 7 | lisi | 50 |
  15. | 8 | chenshuo | 10 |
  16. | 9 | wangwu | 3 |
  17. | 10 | qiuyi | 15 |
  18. | 11 | qiuxiaotian | 20 |
  19. +------+-------------+------+
  20. 11 rows in set (0.00 sec)

以age字段降序排序

  1. mysql> select * from student order by age desc;
  2. +------+-------------+------+
  3. | id | name | age |
  4. +------+-------------+------+
  5. | 7 | lisi | 50 |
  6. | 4 | sean | 28 |
  7. | 5 | zhangshan | 26 |
  8. | 3 | wangqing | 25 |
  9. | 2 | jerry | 23 |
  10. | 1 | tom | 20 |
  11. | 6 | zhangshan | 20 |
  12. | 11 | qiuxiaotian | 20 |
  13. | 10 | qiuyi | 15 |
  14. | 8 | chenshuo | 10 |
  15. | 9 | wangwu | 3 |
  16. +------+-------------+------+
  17. 11 rows in set (0.03 sec)

查询student表中年龄最小的3位同学

  1. mysql> select * from student order by age limit 3;
  2. +------+----------+------+
  3. | id | name | age |
  4. +------+----------+------+
  5. | 9 | wangwu | 3 |
  6. | 8 | chenshuo | 10 |
  7. | 10 | qiuyi | 15 |
  8. +------+----------+------+
  9. 3 rows in set (0.00 sec)

查询student表中年龄最大的4位同学

  1. mysql> select * from student order by age desc limit 4;
  2. +------+-----------+------+
  3. | id | name | age |
  4. +------+-----------+------+
  5. | 7 | lisi | 50 |
  6. | 4 | sean | 28 |
  7. | 5 | zhangshan | 26 |
  8. | 3 | wangqing | 25 |
  9. +------+-----------+------+
  10. 4 rows in set (0.00 sec)

查询student表中名字叫zhangshan的记录

  1. mysql> select * from student where name = 'zhangshan';
  2. +------+-----------+------+
  3. | id | name | age |
  4. +------+-----------+------+
  5. | 5 | zhangshan | 26 |
  6. | 6 | zhangshan | 20 |
  7. +------+-----------+------+
  8. 2 rows in set (0.01 sec)

查询student表中名字叫zhangshan且年龄大于20岁的记录

  1. mysql> select * from student where name = 'zhangshan' and age > 20;
  2. +------+-----------+------+
  3. | id | name | age |
  4. +------+-----------+------+
  5. | 5 | zhangshan | 26 |
  6. +------+-----------+------+
  7. 1 row in set (0.02 sec)

查询student表中年龄在23到30之间的记录

  1. mysql> select * from student where age between 23 and 30;
  2. +------+-----------+------+
  3. | id | name | age |
  4. +------+-----------+------+
  5. | 2 | jerry | 23 |
  6. | 3 | wangqing | 25 |
  7. | 4 | sean | 28 |
  8. | 5 | zhangshan | 26 |
  9. +------+-----------+------+
  10. 4 rows in set (0.00 sec)

修改wangwu的年龄为100

  1. mysql> update student set age = 100 where name = 'wangwu';
  2. Query OK, 1 row affected (1.35 sec)
  3. Rows matched: 1 Changed: 1 Warnings: 0
  4. mysql> select * from student;
  5. +------+-------------+------+
  6. | id | name | age |
  7. +------+-------------+------+
  8. | 1 | tom | 20 |
  9. | 2 | jerry | 23 |
  10. | 3 | wangqing | 25 |
  11. | 4 | sean | 28 |
  12. | 5 | zhangshan | 26 |
  13. | 6 | zhangshan | 20 |
  14. | 7 | lisi | 50 |
  15. | 8 | chenshuo | 10 |
  16. | 9 | wangwu | 100 |
  17. | 10 | qiuyi | 15 |
  18. | 11 | qiuxiaotian | 20 |
  19. +------+-------------+------+
  20. 11 rows in set (0.00 sec)

删除student中名字叫zhangshan且年龄小于等于20的记录

  1. mysql> delete from student where name = 'zhangshan' and age <= 20;
  2. Query OK, 1 row affected (0.06 sec)
  3. mysql> select * from student;
  4. +------+-------------+------+
  5. | id | name | age |
  6. +------+-------------+------+
  7. | 1 | tom | 20 |
  8. | 2 | jerry | 23 |
  9. | 3 | wangqing | 25 |
  10. | 4 | sean | 28 |
  11. | 5 | zhangshan | 26 |
  12. | 7 | lisi | 50 |
  13. | 8 | chenshuo | 10 |
  14. | 9 | wangwu | 100 |
  15. | 10 | qiuyi | 15 |
  16. | 11 | qiuxiaotian | 20 |
  17. +------+-------------+------+
  18. 10 rows in set (0.00 sec)

mySQL安装与基础配置的更多相关文章

  1. MySQL数据库(一)—— 数据库介绍、MySQL安装、基础SQL语句

    数据库介绍.MySQL安装.基础SQL语句 一.数据库介绍 1.什么是数据库 数据库即存储数据的仓库 2.为什么要用数据库 (1)用文件存储是和硬盘打交道,是IO操作,所以有效率问题 (2)管理不方便 ...

  2. SVN CentOS7 下配置svn的安装及基础配置介绍

    CentOS7 下配置svn的安装及基础配置介绍 by:授客 QQ:1033553122 目录 一. 二. 三. 四. 五. 六. 七. 一.      实践环境 CentOS 7操作系统(CentO ...

  3. MySQL数据库(一)-- 数据库介绍、MySQL安装、基础SQL语句

    一.数据库介绍 1.什么是数据库 数据库即存储数据的仓库 2.为什么要用数据库 (1)用文件存储是和硬盘打交道,是IO操作,所以有效率问题 (2)管理不方便 (3)一个程序不太可能仅运行在同一台电脑上 ...

  4. MySQL安装及主从配置

    系统环境:CentOS release 6.5 (Final)(最小化安装) MySQL版本:mysql-5.6.12 Cmake版本:cmake-2.8.4 说明:安装mysql先安装cmake(原 ...

  5. MySQL安装及初步配置.md

    MySQL 安装脚本 #!/bin/bash MYSQL_BASEDIR=/usr/local/mysql MySQL_DATADIR=/data/mysql SERVER_ID=`hostname ...

  6. mysql安装后的配置

    mysql的安装其实就是一步一步按提示就可以: 1.开始安装 2.选择mysql安装位置 3.选择数据库存放位置,最好和mysql安装在一起,好查找 4.选择详细设置 5.选开发者机器 6.选多功能型 ...

  7. python mysql安装&&简单基础sql

    ##############总结############## 1.mysql 介绍 Mysql是开源的,所以你不需要支付额外的费用. Mysql支持大型的数据库.可以处理拥有上千万条记录的大型数据库. ...

  8. MySQL安装与基本配置

    一.简介 SQL语言 DDL:表.视图.索引.触发器操作等.CREATE/ALTER/DROP语句 DML:数据操作.SELECT/INSERT/UPDATE/DELETE DCL:权限设置.GRAN ...

  9. Mysql安装及主从复制配置

    1.下载 mysql数据库 wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.9-linux-glibc2.5-x86_64.ta ...

随机推荐

  1. python开发计算器

    1 业务需求 1.1 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16- ...

  2. [UE4]Spacer

    一.Spacer:留白占位控件 二.如下图所示,如果想要2个按钮都在容器右对齐: 三.可以放一个Spacer到最左边,设置成Fill,Spacer控件就是起到占位的作用.  

  3. 杂谈3.py

    bin() --------十转二 hex()------- 十转十六 oct()-------十转八 import math math.floor(数值)返回小于等于数值的整数 math.trunc ...

  4. RabbitMQ--windows10环境下的RabbitMQ安装步骤(转)

    https://blog.csdn.net/weixin_39735923/article/details/79288578

  5. 有哪些你不知道的python小工具

    python作为越来越流行的一种编程语言,不仅仅是因为它语言简单,有许多现成的包可以直接调用. python中还有大量的小工具,让你的python工作更有效率. 1.- 快速共享 - HTTP服务器 ...

  6. 实战ELK(4)Metricbeat 轻量型指标采集器

    一.介绍 用于从系统和服务收集指标.从 CPU 到内存,从 Redis 到 Nginx,Metricbeat 能够以一种轻量型的方式,输送各种系统和服务统计数据. 1.系统级监控,更简洁(轻量型指标采 ...

  7. leetcode238

    public class Solution { public int[] ProductExceptSelf(int[] nums) { int[] result = new int[nums.Len ...

  8. python 数据分析库介绍

    1 引言 高效处理数据的python工具: 与外界进行交互: 读写各种文件格式和数据库 准备: 对数据进行清理.修整.整合.规范化.重塑.切片切换.变形等处理以便进行分析 转换: 对数据集做一些数学和 ...

  9. 【391】栈与队列,Python实现

    参考:python实现stack(栈)和队列(queue) - hjhmpl123的博客 - CSDN博客 参考:Python3 数据结构 | 菜鸟教程 栈和队列是两种基本的数据结构,同为容器类型.两 ...

  10. CentOS6.5搭建OpenVas完全搭建手册(搭建过程总结及小记)

    一.OpenVAS 介绍 1.关于OpenVAS OpenVAS(Open Vulnerability Assessment System)是一套开源的漏洞扫描系统,早期Nessus 是其中一个最流行 ...