数据库的练习和总结

1、 导入hellodb.sql生成数据库

(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
(2) 以ClassID为分组依据,显示每组的平均年龄
(3) 显示第2题中平均年龄大于30的分组及平均年龄
(4) 显示以L开头的名字的同学的信息

# 导入hellodb.sql
mysql -uroot -p < hellodb_innodb.sql

# 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
use hellodb;
select name as 姓名,age as 年龄 from students where age > 25 and gender='M';

# 以ClassID为分组依据,显示每组的平均年龄
select ClassID as 班级,avg(Age) as 平均年龄 from students group by Classid;

# 显示第2题中平均年龄大于30的分组及平均年龄
select ClassID as 班级,avg(Age) as 平均年龄 from students group by Classid having avg(Age) > 30;

# 显示以L开头的名字的同学的信息
select *  from students where name like 'L%';

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

# 创建magedu用户并且设定密码
create user magedu@'192.168.1.%' identified by '123456';

# 在另一台CentOS虚拟机登录数据库进行验证
mysql -umagedu -p123456 -h192.168.1.12

# 使用hellodb数据库
use hellodb;

3、mysql常见的存储引擎以及特点

MySQL最常见的存储引擎包括InnoDB、MyISAM

InnoDB存储引擎的特点:

  • 支持事务,适合处理大量短期事务
  • 支持行级锁,性能比表级锁更好
  • 可缓存数据和索引
  • 具备崩溃修复能力和并发控制
  • 支持外键
  • 支持聚簇索引
  • 插入数据的速度低

MyISAM存储引擎的特点:

  • 不支持事务
  • 只支持表级锁,不支持行级锁
  • 读写相互阻塞,写入不能读,读时不能写
  • 只缓存索引
  • 不支持外键约束
  • 不支持聚簇索引
  • 读取数据较快
  • 不支持MVCC(多版本并发控制机制)高并发
  • 崩溃恢复性较差

猜你喜欢

转载自blog.51cto.com/14920534/2605439