标题: 数据库之MySQL的介绍与使用20180703 [打印本页] 作者: 觋牧 时间: 2018-7-4 22:56 标题: 数据库之MySQL的介绍与使用20180703 [attach]3586[/attach]
/*******************************************************************************************/
一、mysql的安装
0.下载
MySQL数据库版本相对比较繁杂。常见的有:Community社区版、Enterprise企业版。
Community版是开源免费的,这也是我们通常用的MySQL的版本。可以满足绝大多数用户需求。
Enterprise版,官方指出提供30天免费试用期。可进一步划分为MySQL标准版、MySQL企业版、MySQL集群版。官方提供付费服务。
其中Community Server 可以直接从mysql 的官网下载。但Enterprice Edition只能从Oracle edelivery上下载,而Edelivery有时会屏蔽中国IP。
MySQL各版本区别参见:
http://www.admin10000.com/Document/62.html
下载mysql时注意区分版本细节及所应用的系统平台:linux(32/64) 、win(32/64)
举例:MySQL Community Server 5.6.20 win版本
GA 是指软件的通用版本,一般指正式发布的版本 (Generally Available (GA) Release)
mysql-essential-5.1.60-win32.msi 是精简版,如果只需要mysql服务,就选择此版本。
mysql-5.1.60-win32.msi 是完整版,包含安装程序和配置向导,有MySQL文档。
mysql-noinstall-5.1.60-win32.zip 是非安装的zip压缩包,没有自动安装程序和配置向导,无安装向导
mysql-5.1.60.zip 是用于windows的Mysql源码压缩包
linux版本
在http://www.mysql.com/downloads/网站上下载不了
在 www.oracle.com/downloads 找mysql 注册用户, 选择操作系统平台和mysql版本 进行下载
官方文档上有关MySQL安装,介绍了3种类型及其对应安装方式来安装MySQL数据库:
Linux supports anumber of different solutions for installing MySQL. The recommended method isto use one of the distributions from Oracle. If you choose this method, thereare three options available:
(1) Installingfrom a generic binary package in .tar.gz format. See Section 2.2,“Installing MySQL from Generic Binaries on Unix/Linux” for moreinformation.
(2) Extractingand compiling MySQL from a source distribution. For detailed instructions,see Section 2.9, “InstallingMySQL from Source”.
(3) Installingusing a pre-compiled RPM package. For more information on using the RPMsolution, see Section 2.5.1,“Installing MySQL from RPM Packages on Linux”
我们选用较简单的RPM 包来安装
1).日期转字符串:
转换函数 yyyy-mm-dd 语法支持,功能没有
mysql> select date_format('2013-5-11', 'yyyy-mm-dd') from dual;
mysql> select date_format(now(), '%Y-%m-%d') from dual;
y(没有20只有17)和Y(2017)不一样。
mysql> select date_format(now(), '%Y-%c-%d %h:%i:%s') from dual;
c(02变成2,会去掉0)和m(02)、M(用英语书写的月份)不一样
2).字符串转日期:
mysql> select str_to_date('2013-6-04 05:14:15' , '%Y-%c-%d %h:%i:%s') from dual;
//最好字符串要和格式串一致,即对应起来,当然不一致也能转换
mysql> select str_to_date('2013-06-04 05:14:15' , '%Y-%m-%d %h:%i:%s') from dual;
3).格式很像Linux下的date命令,date命令后可以加格式串来得到想要的日期格式:
[itcast@localhost ~]$ date +'%y-%m-%d'
17-02-16
[itcast@localhost ~]$ date +'%Y-%m-%d'
2017-02-16
4.数学相关函数
具体可见图5:
[attach]3592[/attach]
5.group by语法
-- 求各个班英语的平均分
select avg(english),class_id,id from student group by class_id;
//这样在oracle中是不允许的(select后面出现的非主函数的列在group by后面必须出现),但是在mysql中是可以,
这个id是每个分组第一条记录对应的id,但是这样没有意义.
mysql group by语法检查不严格,我们仍然用oracle的要求.
/*******************************************************************************************/
七、多表数据
create database if not exists mydb1 character set utf8;
//if not exists 如果不存在才去执行,这样反复执行这个命令就不会报错
1).求员工号,姓名,月薪,部门名称
--oracle写法
select e.empno,e.ename,e.sal,d.dname
from emp e,dept d
where e.deptno=d.deptno;
--sql99(mysql)写法
select e.empno,e.ename,e.sal,d.dname
from emp e inner join dept d
on e.deptno=d.deptno;
口诀: 1. , --> inner join 2. where ---> on
两种写法在mysql中都能使用
2.外连接
1).求员工总人数,显示部门编号,名称,人数
--oracle写法
select d.deptno,d.dname,count(e.empno)
from emp e , dept d
where e.deptno=d.deptno;
查询结果有问题,这是由于条件不是完全相等,需要用到左外 右外才行,(也有可能是没有group by的原因)
>>>>>>oracle的外连接在mysql中不能使用,如下是oracle的外连接在mysql中不能使用
select d.deptno,d.dname,count(e.empno)
from emp e , dept d
where e.deptno(+)=d.deptno;
--sql99的写法
select d.deptno,d.dname,count(e.empno)
from emp e right outer join dept d
on e.deptno=d.deptno
group by d.deptno,d.dname;
右外连接口诀: 1 , --- > right outer join(即保留join右边的部分,也就是dept d) 2 where --> on
select d.deptno,d.dname,count(e.empno)
from dept d left outer join emp e
on e.deptno=d.deptno
group by d.deptno,d.dname;
左外连接口诀: 1 , --- > left outer join 2 where --> on
--oracle写法(不带外连接)
select concat(e.ename,'''s boss is ',b.ename)
from emp e,emp b
where e.mgr = b.empno;
--sql99的写法
select concat(e.ename,'''s boss is ',b.ename)
from emp e left outer join emp b
on e.mgr = b.empno;
返回的结果中有一个记录是null,这时因为有一个 b.ename是null,这样字符串连接后也是null
滤空函数:
--nvl(滤空)函数mysql不支持
select concat(e.ename,'''s boss is ',nvl(b.ename,'himself'))
from emp e left outer join emp b
on e.mgr = b.empno;
--nvl(滤空)函数用ifnull替换
select concat(e.ename,'''s boss is ',ifnull(b.ename,'himself'))
from emp e left outer join emp b
on e.mgr = b.empno;
4.满外联接
任一边有值就会显示。
select e.*, d.*
from emp e full outer join dept d
on e.deptno=d.deptno
也可以省略outer关键字
5.交叉连接:
叉集,即笛卡尔集
select e.*, d.*
from emp e cross join dept d
无连接条件
6.inner 和 outer 可以省.
7.
后续实际项目中用的是redhat系统上装数据库
select 'create synonym'||TNAME||'for scott.'||TNAME from tab where tabtype='TABLE';//这样可以拿到建所有同义词的语句
--显示数据
select * from emp \G; 这种方式显出出来的结果是一行一行的展示数据,就和没有\G的效果完全不同了
--top-N问题解决(mysql解决top-N问题的方法与oracle不同,主要就是使用了limit)
-- limit 取前3名,
select * from emp order by sal desc limit 3;
-- limit取5-8名,limit 后的两个数字,代表跳过的记录数,和然后取的记录数.
select * from emp order by sal desc limit 4,4;
/*******************************************************************************************/
十一、mysql api编程
mysql API详细见《MySQL 中文完全参考手册5.1.chm》,主要研究的是c库,也就是mysql提高给c的api
(下面摘出的是文档中需要特别注意的地方,并作了相应的注释)