Linux下MySQL的简单使用(二)

上一篇我们介绍到一些Linux的简单命令的使用,今天我们更深入了解一下MySQL!

一.多表查询

这篇通过实例来讲述多表查询,目的是穷尽所有的场景和所有的方法,并且对每个方法的使用做实例。 首先先列举本篇用到的分类(内连接,外连接,交叉连接)和连接方法:

A)内连接:join,inner join

B)外连接:left join,left outer join,right join,right outer join,union

C)交叉连接:cross join

首先导入hellodb.sql,以下的操作都是基于此:

1.内连接:仅查看有老师的学员和有学员的老师的信息

mysql> select s.name student_name,t.name teacher_name from students s inner join teachers t on s.teacherid=t.tid;+----------------+---------------+
| student_name   | teacher_name  |
+----------------+---------------+
| Sun Shangxiang | Liu Bang      |
| Hou Yi         | Wu Zetian     |
| Da Ji          | Cheng Jisihan |
+----------------+---------------+
3 rows in set (0.67 sec)

2.左内连接:显示所有学员信息,包含有老师的学员和没老师的学员

mysql> select s.name student_name,t.name teacher_name from students s left join teachers t on s.teacherid=t.tid;
+----------------+---------------+
| student_name   | teacher_name  |
+----------------+---------------+
| Hou Yi         | Wu Zetian     |
| Ya Se          | NULL          |
| An Qila        | NULL          |
| Da Ji          | Cheng Jisihan |
| Sun Shangxiang | Liu Bang      |
| Huang Zhong    | NULL          |
| Liu Bei        | NULL          |
| Guan Yu        | NULL          |
| Zhang Fei      | NULL          |
| Di Renjie      | NULL          |
| Li Yuanfang    | NULL          |
| Lan Lingwang   | NULL          |
| Wang Zhaojun   | NULL          |
| Bai Qi         | NULL          |
| A Ke           | NULL          |
| Cai Wenji      | NULL          |
| Lv Bu          | NULL          |
| Diao Chan      | NULL          |
| Gong Sunli     | NULL          |
| Ming Shiyin    | NULL          |
| Dun Shan       | NULL          |
| Zhou Yu        | NULL          |
| Mi Yue         | NULL          |
| Kai            | NULL          |
| Sun Wukong     | NULL          |
+----------------+---------------+
25 rows in set (0.05 sec)

3.右内连接:显示所有的老师信息,包含有学员的老师,和没学员的老师

mysql> select s.name student_name,t.name teacher_name from students s right join teachers t on s.teacherid=t.tid;

+----------------+---------------+
| student_name   | teacher_name  |
+----------------+---------------+
| Sun Shangxiang | Liu Bang      |
| NULL           | Ying Zheng    |
| Hou Yi         | Wu Zetian     |
| Da Ji          | Cheng Jisihan |
+----------------+---------------+
4 rows in set (0.00 sec)

4.左外连接:仅显示没老师的学员

select s.name student_name,t.name teacher_name from students s left join teachers t on s.teacherid=t.tid where t.name is null;
+--------------+--------------+
| student_name | teacher_name |
+--------------+--------------+
| Ya Se        | NULL         |
| An Qila      | NULL         |
| Huang Zhong  | NULL         |
| Liu Bei      | NULL         |
| Guan Yu      | NULL         |
| Zhang Fei    | NULL         |
| Di Renjie    | NULL         |
| Li Yuanfang  | NULL         |
| Lan Lingwang | NULL         |
| Wang Zhaojun | NULL         |
| Bai Qi       | NULL         |
| A Ke         | NULL         |
| Cai Wenji    | NULL         |
| Lv Bu        | NULL         |
| Diao Chan    | NULL         |
| Gong Sunli   | NULL         |
| Ming Shiyin  | NULL         |
| Dun Shan     | NULL         |
| Zhou Yu      | NULL         |
| Mi Yue       | NULL         |
| Kai          | NULL         |
| Sun Wukong   | NULL         |
+--------------+--------------+
22 rows in set (0.00 sec)

5.右外连接:仅显示没学员的老师

select s.name student_name,t.name teacher_name from students s right join teachers t on s.teacherid=t.tid where s.name is null;
+--------------+--------------+
| student_name | teacher_name |
+--------------+--------------+
| NULL         | Ying Zheng   |
+--------------+--------------+
1 row in set (0.00 sec)

6.并集:显示所有的老师和学员

mysql>  select s.name student_name,t.name teacher_name from students s left join teachers t on s.teacherid=t.tid
    -> union
    -> select s.name student_name,t.name teacher_name from students s right join teachers t on s.teacherid=t.tid;+----------------+---------------+
| student_name   | teacher_name  |
+----------------+---------------+
| Hou Yi         | Wu Zetian     |
| Ya Se          | NULL          |
| An Qila        | NULL          |
| Da Ji          | Cheng Jisihan |
| Sun Shangxiang | Liu Bang      |
| Huang Zhong    | NULL          |
| Liu Bei        | NULL          |
| Guan Yu        | NULL          |
| Zhang Fei      | NULL          |
| Di Renjie      | NULL          |
| Li Yuanfang    | NULL          |
| Lan Lingwang   | NULL          |
| Wang Zhaojun   | NULL          |
| Bai Qi         | NULL          |
| A Ke           | NULL          |
| Cai Wenji      | NULL          |
| Lv Bu          | NULL          |
| Diao Chan      | NULL          |
| Gong Sunli     | NULL          |
| Ming Shiyin    | NULL          |
| Dun Shan       | NULL          |
| Zhou Yu        | NULL          |
| Mi Yue         | NULL          |
| Kai            | NULL          |
| Sun Wukong     | NULL          |
| NULL           | Ying Zheng    |
+----------------+---------------+
26 rows in set (0.00 sec)

二.视图

视图是一种虚拟存在的表,视图可以理解为是一个容器,表通过条件查询之后,将查询后的结果放入这个容器内,然后给容器命名后即为视图。

视图相对于表的优势:

1.简单:使用视图的用户不必关系后面的表,只需要使用过滤好的内容就行了;

2.安全:因为对表的全新不能限制到表的行或者是列,所以可以通过视图来限制用户对表的访问权限;

3.数据独立:确定了视图的结构之后,如果给原来的表增加了列,并不会影响视图,增加行,视图的相对于的行也会增加,如果源表的列名称发生了改变,可以通过修改视图来解决。

 例:如下图在单表上创建视图,选择表的name,age列作为视图v1_student的列。

mysql> desc students;
+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| StuID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Name | varchar(50) | NO | | NULL | |
| Age | tinyint(3) unsigned | NO | | NULL | |
| Gender | enum(‘F‘,‘M‘) | NO | | NULL | |
| ClassID | tinyint(3) unsigned | YES | | NULL | |
| TeacherID | int(10) unsigned | YES | | NULL | |
+-----------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

mysql> create view v1_students as select name,age from students;
Query OK, 0 rows affected (0.00 sec)

查看已创建的视图

mysql> show create view v1_students\G;
*************************** 1. row ***************************
                View: v1_students
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1_students` AS select `students`.`Name` AS `name`,`students`.`Age` AS `age` from `students`
character_set_client: latin1
collation_connection: latin1_swedish_ci
1 row in set (0.00 sec)

删除已创建的视图

mysql> drop view v1_students;
Query OK, 0 rows affected (0.00 sec)

mysql>  select * from information_schema.views\G;
Empty set (0.06 sec)

三.函数

函数分为系统函数和自定义函数

系统函数:https://dev.mysql.com/doc/refman/5.7/en/func-op-summary ref.html,在这个官方的MySQL网站中,可以查看所有的系统函数。

自定义函数:(user-defined function UDF),保存在mysql.proc表中

1.创建一个自定义函数:

mysql> CREATE FUNCTION simpleFun() RETURNS VARCHAR(20) RETURN "Hello World!";
Query OK, 0 rows affected (0.05 sec)

mysql> select simplefun();
+--------------+
| simplefun()  |
+--------------+
| Hello World! |
+--------------+
1 row in set (0.05 sec)

2.查看所有的函数列表

mysql> show function status;
+---------+-----------+----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| Db      | Name      | Type     | Definer        | Modified            | Created             | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+---------+-----------+----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| hellodb | simpleFun | FUNCTION | [email protected] | 2018-09-26 23:33:33 | 2018-09-26 23:33:33 | DEFINER       |         | latin1               | latin1_swedish_ci    | utf8_general_ci    |
+---------+-----------+----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)

3.查看自定义函数的定义

mysql> show create function simplefun\G;
*************************** 1. row ***************************
            Function: simplefun
            sql_mode:
     Create Function: CREATE DEFINER=`root`@`localhost` FUNCTION `simplefun`() RETURNS varchar(20) CHARSET utf8
RETURN "Hello World!"
character_set_client: latin1
collation_connection: latin1_swedish_ci
  Database Collation: utf8_general_ci
1 row in set (0.00 sec)

4.删除自定义函数

mysql> drop function simplefun;
Query OK, 0 rows affected (0.00 sec)

mysql> show function status\G;
Empty set (0.00 sec)

四.用户管理和授权

 1.创建用户

语法:create user 用户名@主机 identified by 密码;

mysql> create user [email protected] identified by ‘centos‘;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,password from user;
+--------+--------------------+-------------------------------------------+
| user   | host               | password                                  |
+--------+--------------------+-------------------------------------------+
| root   | localhost          | *128977E278358FF80A246B5046F51043A2B1FCED |
| root   | centos6.magedu.com |                                           |
| root   | 127.0.0.1          |                                           |
|        | localhost          |                                           |
|        | centos6.magedu.com |                                           |
| momoda | localhost          | *128977E278358FF80A246B5046F51043A2B1FCED |
+--------+--------------------+-------------------------------------------+
6 rows in set (0.01 sec)

2.删除用户

mysql> drop user [email protected];
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from user;
+------+--------------------+-------------------------------------------+
| user | host               | password                                  |
+------+--------------------+-------------------------------------------+
| root | localhost          | *128977E278358FF80A246B5046F51043A2B1FCED |
| root | centos6.magedu.com |                                           |
| root | 127.0.0.1          |                                           |
|      | localhost          |                                           |
|      | centos6.magedu.com |                                           |
+------+--------------------+-------------------------------------------+
5 rows in set (0.00 sec)

 3.授权

grant不仅可以用来授权,还可以用来创建用户;

授权的语法:grant 权限列表 on 库名.表名 to 用户@主机 identified by ‘密码‘;

刷新权限:flush privileges;

mysql>  grant all on *.* to [email protected]‘192.168.%.%‘ identified by ‘123‘;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from user;
+------+--------------------+-------------------------------------------+
| user | host               | password                                  |
+------+--------------------+-------------------------------------------+
| root | localhost          | *128977E278358FF80A246B5046F51043A2B1FCED |
| root | centos6.magedu.com |                                           |
| root | 127.0.0.1          |                                           |
|      | localhost          |                                           |
|      | centos6.magedu.com |                                           |
| haha | 192.168.%.%        | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+------+--------------------+-------------------------------------------+
6 rows in set (0.00 sec)

4.查看授权

mysql> show grants for [email protected]‘192.168.%.%‘;
+------------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]192.168.%.%                                                                                            |
+------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘haha‘@‘192.168.%.%‘ IDENTIFIED BY PASSWORD ‘*23AE809DDACAF96AF0FD78ED04B6A265E05AA257‘ |
+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

5.回收授权

回收权限有两种方法:删除用户和revoke

回收语法:  revoke 权限 on 库名.表名 from 用户@主机;

mysql> revoke all on *.* from [email protected]‘192.168.%.%‘;
Query OK, 0 rows affected (0.00 sec)

原文地址:https://www.cnblogs.com/liuwentaolaji/p/9698079.html

时间: 2024-11-09 22:33:33

Linux下MySQL的简单使用(二)的相关文章

Linux下MySQL的简单使用(一)

MySQL是一个关系型数据库管理系统,由MySQL AB 公司开发,目前属于 Oracle .MySQL 是最流行的关系型数据库管理系统之一.关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性.MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言.拥有体积小.速度快.总体成本低,尤其是开放源码这一特点. 一.登陆MySQL 在没有设置密码的前提下,输入命令mysql可直接进入MySQL界面: [[email protected] ~]

linux下mysql的简单配置

1.设置字符编码 输入命令: [root@localhost ~]# vi /etc/my.cnf 最后一行加入:default-character-set=utf8,保存! 2.启动mysql服务: 输入命令: [root@localhost ~]# service mysqld start 返回一下信息则表示已经启动成功 Initializing MySQL database: Installing MySQL system tables- OK Filling help tables- O

Linux下MySQL 5.5的修改字符集编码为UTF8(彻底解决中文乱码问题)

一.登录MySQL查看用SHOW VARIABLES LIKE ‘character%’;下字符集,显示如下:+--------------------------+----------------------------+| Variable_name | Value |+--------------------------+----------------------------+| character_set_client | utf8 || character_set_connectio

linux下MySQL安装登录及操作

linux下MySQL安装登录及操作 二.安装Mysql 1.下载MySQL的安装文件 安装MySQL需要下面两个文件: MySQL-server-4.0.16-0.i386.rpm MySQL-client-4.0.16-0.i386.rpm 下载地址为:http://www.mysql.com/downloads/mysql-4.0.html, 打开此网页,下拉网页找到“Linux x86 RPM downloads”项,找到“Server”和“Client programs”项,下载需要的

【MySQL】Linux下MySQL 5.5、5.6和5.7的RPM、二进制和源码安装

  [MySQL]Linux下MySQL 5.5.5.6和5.7的RPM.二进制和源码安装 1.1  BLOG文档结构图 1.2  前言部分 1.2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① MySQL的二进制安装过程(重点) ② MySQL多实例管理(mysqld_multi) ③ MySQL的源码编译安装过程 ④ Linux的逻辑卷的使用 ⑤ 文件的MD5值 ⑥ 访问MySQL的几种客户端工具(Nav

linux下mysql基于mycat做主从复制和读写分离之基础篇

Linux下mysql基于mycat实现主从复制和读写分离1.基础设施 两台虚拟机:172.20.79.232(主) 172.20.79.233(从) 1.1软件设施 mysql5.6.39 , mycat1.6-RELEASE jdk1.7及其以上版本2.实现步骤一(mycat实现读写分离) 1.首先在两台服务器安装mysql 1.下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rp

Linux下Mysql 操作命令

Linux下Mysql 操作命令 一.连接MySQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysqlbin,再键入命令mysql -uroot -p,回车后提示你输密码,如果刚安装好MYSQL,超级用户root是没有密码的,故直接回车即可进入到MYSQL中了,MYSQL的提示符是: mysql>. 2.例2:连接到远程主机上的MYSQL.假设远程主机的IP为:110.110.110.110,用户名为

linux下mysql 安装

小菜鸟接触linux太晚, 装个mysql(免安装 mysql-5.6.22-linux-glibc2.5-x86_64版本,最简单的安装方法) 竞折腾了两个晚上… 网上到处有linux下mysql的安装,但我自己安装过程中总出现这样那样的问题,现将此次安装过程及错误记录,以供自己日后参考,也希望可以给后来人一些帮助… 1. 去Oracle下载mysql-5.6.22-linux-glibc*.tar.gz 2.解压 tar -zxvf mysql-5.6.22-linux-glibc*.tar

linux下mysql数据库主从同步配置

说明: 操作系统:CentOS 5.x 64位 MySQL数据库版本:mysql-5.5.35 MySQL主服务器:192.168.21.128 MySQL从服务器:192.168.21.129 准备篇: 说明:在两台MySQL服务器192.168.21.128和192.168.21.129上分别进行如下操作 备注: 作为主从服务器的MySQL版本建议使用同一版本! 或者必须保证主服务器的MySQL版本要高于从服务器的MySQL版本! 一.配置好IP.DNS .网关,确保使用远程连接工具能够连接