sql之战

之前有写过mysql的部署,如果要重部署的话只需要
rm -rf $MYSQL_HOME/arch/* binlog日志 恢复 主从同步
rm -rf $MYSQL_HOME/data/* 数据
执行
$MYSQL_HOME/scripts/mysql_install_db
–user=mysqladmin
–basedir=/usr/local/mysql
–datadir=/usr/local/mysql/data

在学习DML语句前,务必掌握
设置用户密码
select host,user,password from user;
update user set password=password(‘123456’) where user=‘root’;
删除空账户
delete from user where user=‘’;
删除用户,%是所有ip都能访问
drop user xxx@‘%’
创建db,user,权限
create database xxx;
grant all privileges on xxx.* to 用户@‘ip‘ identified by ’密码‘;
flush privileges;

use ruozedb;

create table ruozedb.ruozedata(
id int,
name varchar(100),
age int,

create_time timestamp ,
create_user varchar(100),
update_time timestamp ,
update_user varchar(100)
);

insert into ruozedata(id,name,age) values(1,‘jepson’,18);

update ruozedata set age=22 where id=1;
delete from ruozedata where id=1;

select * from ruozedata;


–模板
create table ruozedb.ruozedata_test_prod(
id int auto_increment 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)
)default charset=utf8;

insert into ruozedb.ruozedata_test_prod(name,age) values(‘若泽’,18); – 字段名称和值一一对应
insert into ruozedb.ruozedata_test_prod
values(2,‘jepson’,18,‘上海’,‘2018-09-16 23:00:00’,‘j’,‘2018-09-16 23:00:00’,‘j’); – 字段名称和值一一对应

insert into ruozedb.ruozedata_test_prod(name,age) values(‘huhu’,18);
insert into … values …;

update ruozedata_test_prod set age=22;
update ruozedata_test_prod set age=25,address=‘北京’;
update ruozedata_test_prod set age=19,address=‘上海’ where name =‘jepson’;
update ruozedata_test_prod set address=‘深圳’ where name=‘jepson’ and age=19;
update ruozedata_test_prod set address=‘深圳’ where name=‘jepson’ or age=25;
update …set…where…

delete from ruozedata_test_prod;
delete from ruozedata_test_prod where age=19;
delete from …where…;

select * from ruozedata_test_prod;

–部门表
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);

– 不等于
select * from emp where sal <>5000;
– 模糊查询包含S的
select * from emp where ename like ‘%S%’;
– 占位符,占1位
select * from emp where ename like ‘_o%’;
– 等价于asc,升序
select * from emp order by sal;

select * from emp order by sal desc;
– 两个字段排序
select * from emp order by deptno asc,sal desc;
– 限制显示行
select * from emp limit 2;
select * from emp order by deptno asc,sal desc limit 2;

– 聚合 group by后面的字段必须出现在select字段
– 聚合函数 sum count avg max min
– group by … having …
select
deptno,sum(sal) as sumsal
from emp
group by deptno
having sum(sal)>10000;

– 有多少行
select count(*) from emp;

– 组合
select
deptno,job,sum(sal)
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
drop table a;
create table a(id int,name varchar(100) );
insert into a values(1,19.999);
insert into a values(2,‘xiaoyanj’);
insert into a values(3,‘lanyang’);

drop table b;
create table b(id int,address varchar(300));
insert into b values(1,‘2018-10-10 00:00:00’);
insert into b values(2,2);
insert into b values(3,‘b3’);
insert into b values(4,‘b4’);
insert into b values(5,‘b5’);
insert into b values(3,‘lanyang’);

– 去重复数据
select * from a
union
select * from b

– 不去除重复数据
select id,name from a
union all
select id,address from b

– 名称由a表决定

猜你喜欢

转载自blog.csdn.net/qq_36459386/article/details/83545573
今日推荐