搭建MySQL服务

MySQL服务

关系型数据库介绍
数据结构模型
数据结构模型主要有:

层次模型
网状结构
关系模型
关系模型:
二维关系: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:移除授权

1. 配置mysql的yum源

[root@2 ~]# wget wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@2 ~]# ls
anaconda-ks.cfg  mysql57-community-release-el7-10.noarch.rpm
[root@2 ~]# rpm -ivh mysql57-community-release-el7-10.noarch.rpm 
warning: mysql57-community-release-el7-10.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql57-community-release-el7-10 ################################# [100%]
[root@2 ~]# cd /etc/yum.repos.d/
[root@2 yum.repos.d]# ls
mysql-community.repo  mysql-community-source.repo  redhat.repo  xx.repo

2. 安装mysql5.7

[root@2 yum.repos.d]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel
[root@2 ~]# systemctl start mysqld
[root@2 ~]# systemctl enable mysqld

3. 创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age)

[root@2 ~]# grep "password" /var/log/mysqld.log 
2019-01-17T11:02:44.387565Z 1 [Note] A temporary password is generated for root@localhost: wwqS;jonS09f
[root@2 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24

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> 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.00 sec)
mysql> create database zml;
Query OK, 1 row affected (0.00 sec)

mysql> use zml;
Database changed
mysql> CREATE TABLE student (id int(11),name varchar(100),age tinyint(4) NULL);
Query OK, 0 rows affected (0.22 sec)
mysql> desc student;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | YES  |     | NULL    |       |
| name  | varchar(100) | YES  |     | NULL    |       |
| age   | tinyint(4)   | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.16 sec)

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

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

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

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);
Query OK, 11 rows affected (0.04 sec)
Records: 11  Duplicates: 0  Warnings: 0

mysql> select * from student;                                            
+------+-------------+------+
| id   | name        | age  |
+------+-------------+------+
|    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 |
+------+-------------+------+
11 rows in set (0.00 sec)

6. 修改lisi的年龄为50

mysql> update student set age = 50 where name = 'lisi';
Query OK, 1 row affected (0.80 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student;                                            
+------+-------------+------+
| 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      |    3 |
|   10 | qiuyi       |   15 |
|   11 | qiuxiaotian |   20 |
+------+-------------+------+
11 rows in set (0.00 sec)

7.以age字段降序排序

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

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

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

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

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

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

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

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

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

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

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

13. 修改wangwu的年龄为100

mysql> update student set age = 100 where name = 'wangwu';
Query OK, 1 row affected (1.34 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student;
+------+-------------+------+
| 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)

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

mysql> delete from student where name = 'zhangshan' and age <= 20;
Query OK, 1 row affected (0.06 sec)

mysql> select * from student;
+------+-------------+------+
| id   | name        | age  |
+------+-------------+------+
|    1 | tom         |   20 |
|    2 | jerry       |   23 |
|    3 | wangqing    |   25 |
|    4 | sean        |   28 |
|    5 | zhangshan   |   26 |
|    7 | lisi        |   50 |
|    8 | chenshuo    |   10 |
|    9 | wangwu      |  100 |
|   10 | qiuyi       |   15 |
|   11 | qiuxiaotian |   20 |
+------+-------------+------+
10 rows in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值