【完全分布式Hadoop】(六)MySQL5.7服务端安装,DBA权限设置以及客户端配置
1、数据类型
char 字符 定长,char(10)
varchar 字符串
int 整数
float 单精度
double 双精度
date 日期 年月日
timestamp 年月日 时分秒 时间戳
decimal 通常和费用金钱相关的使用该类型
2、设置用户权限以及客户端的IP
grant all privileges on *.* to 'root'@'%' identified by '123456';
flush priviliges;
3、查看databases以及创建database、切换database
show databases;
create database ruozedata;
use ruozedata;
4、SQL操作类型介绍
DML:insert update delete select
增删改查
DDL:create database/drop database
5、创建表
每张表基本都需要:创建时间、创建者、修改时间、修改者
create table ruozet1(
id int auto_incremen primary key,
name varchar(100),
age int,
address varchar(100),
create_time timestamp default current_timestamp,
create_user varchar(100),
update_time timestamp default current_timestamp on update current_timestamp,
update_user varchar(100)
)ENGING=InnoDB default CHARSET=utf-8;
– id主键自增长,唯一
– default current_timestamp创建时默认时间
– default current_timestamp on update current_timestamp 新增时以及更新时都会写入当前时间
insert into ruozedata (name,age) values ('张三',18);
6、增删改查数据
新增数据
insert into ruozedata(id, name, age) values(1,‘zhangsan’,18);
修改数据指定id的信息(高危,请检查好where)
update ruozedata set age=20,address=‘北京’ where id = 1;
删除指定id的数据
delete from ruozedata where id = 1;
清空表(高危,请再三确认where条件,必要时可以先做备份)
delete from ruozedata;
查询数据
select id,name,age from ruozedata;
7、例子
以下表全部来自于Oracle官方例子
--部门表
dept部门表(deptno部门编号/dname部门名称/loc地点)
create table dept (
deptno numeric(2),
dname varchar(14),
loc varchar(13)
);
insert into dept values (10, 'ACCOUNTING', 'NEW YORK');
insert into dept values (20, 'RESEARCH', 'DALLAS');
insert into dept values (30, 'SALES', 'CHICAGO');
insert into dept values (40, 'OPERATIONS', 'BOSTON');
--工资等级表
salgrade工资等级表(grade 等级/losal此等级的最低/hisal此等级的最高)
create table salgrade (
grade numeric,
losal numeric,
hisal numeric
);
insert into salgrade values (1, 700, 1200);
insert into salgrade values (2, 1201, 1400);
insert into salgrade values (3, 1401, 2000);
insert into salgrade values (4, 2001, 3000);
insert into salgrade values (5, 3001, 9999);
--员工表
emp员工表(empno员工号/ename员工姓名/job工作/mgr上级编号/hiredate受雇日期/sal薪金/comm佣金/deptno部门编号)
工资 = 薪金 + 佣金
1.表自己跟自己连接
create table emp (
empno numeric(4) not null,
ename varchar(10),
job varchar(9),
mgr numeric(4),
hiredate datetime,
sal numeric(7, 2),
comm numeric(7, 2),
deptno numeric(2)
);
insert into emp values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20);
insert into emp values (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);
insert into emp values (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);
insert into emp values (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20);
insert into emp values (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);
insert into emp values (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30);
insert into emp values (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10);
insert into emp values (7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000, null, 20);
insert into emp values (7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10);
insert into emp values (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30);
insert into emp values (7876, 'ADAMS', 'CLERK', 7788, '1983-01-12', 1100, null, 20);
insert into emp values (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, null, 30);
insert into emp values (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, null, 20);
insert into emp values (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, null, 10);
-
条件 where > < = >= <=
select * from emp where sal > 3000; --大于3000
select * from emp where sal = 5000; --等于5000
select * from emp where sal <> 5000; --不等于5000
-
模糊查询 like
select * from emp where ename like '%S%';
--查出ename中包含S的数据
select * from emp where ename like 'S%';
--查出ename中S开头的数据
select * from emp where ename like '%S';
--查出ename中S结尾的数据
select * from emp where ename like '_o%';
--第二个字符为o的字符,占位符: _ -
排序
select * from emp order by sal;
--默认升序
select * from emp order by sal asc;
--asc升序
select * from emp order by sal desc;
--desc降序
select * from emp order by deptno asc,sal desc;
--按照deptno升序,按照sal降序 -
限制多少行
select * from emp limit 10;
select * from emp order by deptno asc,sal desc limit 10;
-
聚合 (group by) 聚合函数 (sum count avg max min)
select deptno,sum(sal) sa sumsal from emp group by deptno having sum(sal) > 10000;
–查看各个部门所有的人工资总和 -
group by字段必须出现在select语句中
select deptno,job,sum(sal) sa sumsal from emp group by deptno,job;
– group by 字段必须出现在select字段 -
数量 最大值等
select count(*) from emp;
select max(sal) from emp;
-
组合 顺序
select deptno,job,sum(sal) as sumsal from emp where job = 'SALESMAN' group by deptno,job having sum(sal) >3000 order by sum(sal) desc limit 1;
-
as别名
sum(sal) as sumsal -
union
a表 id,name,address 1,zhangsan,zhangsan18 2,lisi,lisi20 3,wangwu,wangwu24 b表 id,name,address 1,zhangsan,zhangsan18 2,lilei,lilei22 3,wangwu,wangwu24
去重复数据
select * from a union select * from b; 1,zhangsan,zhangsan18 2,lisi,lisi20 3,wangwu,wangwu24 2,lilei,lilei22
不去重复数据
select * from a union all select * from b; 1,zhangsan,zhangsan18 2,lisi,lisi20 3,wangwu,wangwu24 1,zhangsan,zhangsan18 2,lilei,lilei22 3,wangwu,wangwu24
select id,name from a union all select id,address from b; 1,zhangsan 2,lisi 3,wangwu 1,zhangsan18 2,lilei22 3,wangwu24