MariaDB练习

时间:2020-10-18 16:55:25   收藏:0   阅读:29
1、导入hellodb.sql生成数据库
(1)在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄

[root@centos7 ~]$systemctl start mariadb
[root@centos7 ~]$ss -tnl
State      Recv-Q Send-Q           Local Address:Port                          Peer Address:Port              
LISTEN     0      100                  127.0.0.1:25                                       *:*                  
LISTEN     0      50                           *:3306                                     *:*                  
LISTEN     0      128                          *:22                                       *:*                  
LISTEN     0      100                      [::1]:25                                    [::]:*                  
LISTEN     0      128                       [::]:22                                    [::]:*

[root@centos7 ~]$mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> source hellodb_innodb.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)
...

MariaDB [hellodb]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [hellodb]> select name,age from students where age > 25 and gender=‘M‘;
+--------------+-----+
| name         | age |
+--------------+-----+
| Xie Yanke    |  53 |
| Ding Dian    |  32 |
| Yu Yutong    |  26 |
| Shi Qing     |  46 |
| Tian Boguang |  33 |
| Xu Xian      |  27 |
| Sun Dasheng  | 100 |
+--------------+-----+

(2)以ClassID为分组依据,显示每组的平均年龄

MariaDB [hellodb]> select classid, avg(age) from students group by ClassID;
+---------+----------+
| classid | avg(age) |
+---------+----------+
|    NULL |  63.5000 |
|       1 |  20.5000 |
|       2 |  36.0000 |
|       3 |  20.2500 |
|       4 |  24.7500 |
|       5 |  46.0000 |
|       6 |  20.7500 |
|       7 |  19.6667 |
+---------+----------+
8 rows in set (0.00 sec)

MariaDB [hellodb]> select classid, avg(age) from students group by ClassID having classid is not null;
+---------+----------+
| classid | avg(age) |
+---------+----------+
|       1 |  20.5000 |
|       2 |  36.0000 |
|       3 |  20.2500 |
|       4 |  24.7500 |
|       5 |  46.0000 |
|       6 |  20.7500 |
|       7 |  19.6667 |
+---------+----------+
7 rows in set (0.00 sec)

(3)显示第二题中平均年龄大于30的分组及平均年龄

MariaDB [hellodb]> select classid, avg(age) from students group by ClassID having avg(age)> 30 and classid is not null;
+---------+----------+
| classid | avg(age) |
+---------+----------+
|       2 |  36.0000 |
|       5 |  46.0000 |
+---------+----------+

(4)显示以L开头对的名字的同学的信息

MariaDB [hellodb]> select * from students where name like ‘L%‘;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name        | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
|     8 | Lin Daiyu   |  17 | F      |       7 |      NULL |
|    14 | Lu Wushuang |  17 | F      |       3 |      NULL |
|    17 | Lin Chong   |  25 | M      |       4 |      NULL |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)

2、数据库授权magedu用户,允许192.168.1.0/24网段可以连接mysql

MariaDB [hellodb]> grant all on *.* to magedu@‘192.168.1.%‘ identified by ‘centos‘;
Query OK, 0 rows affected (0.00 sec)

MariaDB [hellodb]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [hellodb]> select user,password,host from mysql.user;
+--------+-------------------------------------------+-------------------+
| user   | password                                  | host              |
+--------+-------------------------------------------+-------------------+
| root   |                                           | localhost         |
| root   |                                           | centos7.localhost |
| root   |                                           | 127.0.0.1         |
| root   |                                           | ::1               |
|        |                                           | localhost         |
|        |                                           | centos7.localhost |
| magedu | *128977E278358FF80A246B5046F51043A2B1FCED | 192.168.1.%

3、总结mysql常见的存储引起以及特点
技术图片
常用的MyISAM存储引擎和InnoDB存储引擎的特点
MyISAM存储引擎:

InnoDB存储引擎:

其它存储引擎

MariaDB支持的其它存储引擎

* OQGraph
* SphinxSE
* TokuDB
* Cassandra
* CONNECT
* SQUENCE

管理存储引擎
查看mysql支持的存储引擎

MariaDB [hellodb]> show engines;
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                                          | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys       | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                                            | NO           | NO   | NO         |
| MyISAM             | YES     | Non-transactional engine with good performance and small data footprint          | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears)                   | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                                               | NO           | NO   | NO         |
| CSV                | YES     | Stores tables as CSV files                                                       | NO           | NO   | NO         |
| ARCHIVE            | YES     | gzip-compresses tables for a low storage footprint                               | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables                        | NO           | NO   | NO         |
| FEDERATED          | YES     | Allows to access tables on other MariaDB servers, supports transactions and more | YES          | NO   | YES        |
| Aria               | YES     | Crash-safe tables with MyISAM heritage                                           | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+

查看当前默认的存储引擎

MariaDB [hellodb]> show variables like ‘%storage_engine%‘;
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| default_storage_engine | InnoDB |
| storage_engine         | InnoDB |
+------------------------+--------+

设置默认的存储引擎

[root@centos7 ~]$vim /etc/my.cnf
[root@centos7 ~]$head -2 /etc/my.cnf
[mysqld]
default_storage_engine=MyISAM
...
[root@centos7 ~]$systemctl restart mariadb
[root@centos7 ~]$mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

MariaDB [(none)]> SHOW VARIABLES LIKE ‘%storage_engine%‘;
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| default_storage_engine | MyISAM |
| storage_engine         | MyISAM |
+------------------------+--------+

查看数据库中所有表使用的存储引擎

MariaDB [hellodb]> show table status from hellodb;
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| Name     | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation       | Checksum | Create_options | Comment |
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| classes  | InnoDB |      10 | Compact    |    8 |           2048 |       16384 |               0 |            0 |   9437184 |              9 | 2020-10-17 14:52:11 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| coc      | InnoDB |      10 | Compact    |   14 |           1170 |       16384 |               0 |            0 |   9437184 |             15 | 2020-10-17 14:52:12 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| courses  | InnoDB |      10 | Compact    |    7 |           2340 |       16384 |               0 |            0 |   9437184 |              8 | 2020-10-17 14:52:12 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| scores   | InnoDB |      10 | Compact    |   15 |           1092 |       16384 |               0 |            0 |   9437184 |             16 | 2020-10-17 14:52:12 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| students | InnoDB |      10 | Compact    |   25 |            655 |       16384 |               0 |            0 |   9437184 |             26 | 2020-10-17 14:52:12 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| teachers | InnoDB |      10 | Compact    |    4 |           4096 |       16384 |               0 |            0 |   9437184 |              5 | 2020-10-17 14:52:12 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
| toc      | InnoDB |      10 | Compact    |    0 |              0 |       16384 |               0 |            0 |   9437184 |              1 | 2020-10-17 14:52:12 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
7 rows in set (0.00 sec)

查看库中指定表的存储引擎

MariaDB [hellodb]> show table status like ‘students‘;
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| Name     | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation       | Checksum | Create_options | Comment |
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| students | InnoDB |      10 | Compact    |   25 |            655 |       16384 |               0 |            0 |   9437184 |             26 | 2020-10-17 14:52:12 | NULL        | NULL       | utf8_general_ci |     NULL |                |         |
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
1 row in set (0.00 sec)

MariaDB [hellodb]> show create table students\G;
*************************** 1. row ***************************
       Table: students
Create Table: CREATE TABLE `students` (
  `StuID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Name` varchar(50) NOT NULL,
  `Age` tinyint(3) unsigned NOT NULL,
  `Gender` enum(‘F‘,‘M‘) NOT NULL,
  `ClassID` tinyint(3) unsigned DEFAULT NULL,
  `TeacherID` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`StuID`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

设置表的存储引擎

MariaDB [hellodb]> CREATE TABLE test (id int) ENGINE=MyISAM;
MariaDB [hellodb]> SHOW CREATE TABLE test;
+-------+----------------------------------------------------------------------------------------+
| Table | Create Table                                                                           |
+-------+----------------------------------------------------------------------------------------+
| test  | CREATE TABLE `test` (
  `id` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------

MariaDB [hellodb]> ALTER TABLE test ENGINE=InnoDB;
MariaDB [hellodb]> SHOW CREATE TABLE test;
+-------+----------------------------------------------------------------------------------------+
| Table | Create Table                                                                           |
+-------+----------------------------------------------------------------------------------------+
| test  | CREATE TABLE `test` (
  `id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------+
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!