1. 程式人生 > >sql之戰

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 [email protected]‘%’
建立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表決定