1、写一个一键安装 mariadb 数据库脚本。(不准用王老师笔记上的,自己写。)
2、简述Event 事件介绍以及它的优缺点
3、在 students 表中,查询年龄大于25岁,且为男性的同学的名字和年龄:
4、在 students 表中,以 ClassID 为分组依据,查询显示每组的平均年龄
5、显示第2题中平均年龄大于30的分组及平均年龄
第一题目 安装 mariadb-10.2.31的脚本
1、官网下载mariadb-10.2.31-linux-x86_64.tar.gz
2、编写脚本
vim mariadb.sh
#/bin/bash
mkdir /data/mysal
yum -y install libaio
tar xf mariadb-10.2.31-linux-x86_64.tar.gz -C /usr/local
ln -s /usr/local/mariadb-10.2.31-linux-x86_64/ /usr/local/mysql
chown -R root.root /usr/local/mysql/
useradd -s /sbin/nologin -r mysql
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
bash /etc/profile.d/mysql.sh
ln -s /usr/local/mysql/bin/* /usr/bin/
cat > /etc/my.cnf << EOF
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
/usr/local/mysql/scripts/mysql_install_db --datadir=/data/mysql --user=mysql --basedir=/usr/local/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
systemctl start mysqld
3、执行脚本
bash mariadb.sh
第二题、简述event事件介绍以及优缺点?
事件(event)是MYSQL在响应的时刻调用的过程式数据库对象。一个事件可调用一次,也可以周期性的启动,它由一个特定的线程来管理的,也就是所谓的“事件调度器”。
事件和触发器类似,都是在某些事情发送的时候启动。当数据库上启动一条语句的时候,触发器就启动了,而事件是根据调度事件来启动的。由于他们彼此相似,所以事件也称为临时性触发器。
事件取代了原先只能由操作系统的计划任务来执行的工作,而且MYSQL的事件调度器可以精确到每秒钟执行的一个任务,而操作系统的计划任务(如:linux下的CRON或windows下的任务计划),只能精确到每分钟执行一次。
事件的优缺点
优点:一些对数据定时性操作不再依赖外部程序,而直接使用数据本身提供的功能,可以实现每秒钟执行一个任务,这在一些对实时性要求较高的环境下就非常实用。
缺点:定时触发,不可以直接调用。
实验前准备
创建数据库 db_student 和 student表
MariaDB [(none)]> creat database db_student 创建数据库 db_student
MariaDB [(none)]> use db_student; 进入数据库
MariaDB [db_student]> desc student; 查看数据库中的student结构
+---------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| age | int(10) | YES | | NULL | |
| gender | enum('F','M') | YES | | NULL | |
| classid | int(10) unsigned | YES | | NULL | |
+---------+------------------+------+-----+---------+-------+
MariaDB [db_student]> select * from student; 查看student表的内容
+------+-------+------+--------+---------+
| id | name | age | gender | classid |
+------+-------+------+--------+---------+
| 1 | fang | 18 | M | 2 |
| 2 | zhang | 28 | M | 2 |
| 3 | zhang | 23 | M | 3 |
| 4 | lin | 23 | M | 4 |
| 5 | wu | 23 | M | 4 |
| 6 | li | 88 | M | 9 |
| 7 | xiao | 28 | F | 2 |
+------+-------+------+--------+---------+
第三题目 :挑选出年龄大于25的男同学的姓名,年龄性别
MariaDB [db_student]> select * from (select name,age from student where gender='M' group by id) s where age > 25;
+-------+------+
| name | age |
+-------+------+
| zhang | 28 |
| li | 88 |
+-------+------+
2 rows in set (0.00 sec)
第四题目:在 students 表中,以 ClassID 为分组依据,查询显示每组的平均年龄
MariaDB [db_student]> select classid,avg(age) from student group by classid;
+---------+----------+
| classid | avg(age) |
+---------+----------+
| 2 | 24.6667 |
| 3 | 23.0000 |
| 4 | 23.0000 |
| 9 | 88.0000 |
+---------+----------+
4 rows in set (0.00 sec)
第五题目:显示上述题中平均年龄大于30的班级和平均年龄
MariaDB [db_student]> select classid 班级,avg(age) 平均年龄 from student group by classid having 平均年龄 >30;
+--------+--------------+
| 班级 | 平均年龄 |
+--------+--------------+
| 9 | 88.0000 |
+--------+--------------+
1 row in set (0.00 sec)
浙公网安备 33010602011771号