SQL语法一

1.数据类型
char 字符 属性:定长
varchar 字符串 属性:变长
int 整数
float 单精度
double 双精度
date 日期(年月日)
timestamp 日期(年月日时分秒)

2.DML语句 :insert(增) update(改) delete(删) select(查)
DDL语句:create database drop database…(创建DB,Table…)

3.创建Table
use ruozedata;

create table ruozedata.ruozet1(
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);
(insert into …values…)

update ruozedata set age=22 where id=1;
(update …set…where…)

delete from ruozedata where id=1;
(delete from…where)

select * from ruozedata; *表示所有选项
(select * from…)

4.模版(生产上)
create table ruozedata.ruozedata_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 ruozedata.ruozedata_prod(name,age) values(‘若泽’,18); – 字段名称和值一一对应
insert into ruozedata.ruozedata_prod
values(2,‘jepson’,18,‘上海’,‘2018-09-16 23:00:00’,‘j’,‘2018-09-16 23:00:00’,‘j’); – 字段名称和值一一对应 (prod后面不跟选项就代表默认,都要有且一一对应)

insert into ruozedata.ruozedata_prod(name,age) values(‘huhu’,18);
insert into … values …;

update ruozedata_prod set age=22;
update ruozedata_prod set age=25,address=‘北京’; --多个字段修改 逗号分隔
update ruozedata_prod set age=19,address=‘上海’ where name =‘jepson’;

update ruozedata_prod set address=‘深圳’ where name =‘jepson’ and age=19; --且
update ruozedata_prod set address=‘深圳’ where name =‘jepson’ or age=25; --或
update … set … where …;

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

select * from ruozedata_prod;
select id,name,age from ruozedata_prod;
select id,name,age from ruozedata_prod where id=3;

–部门表
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部门编号)
工资 = 薪金 + 佣金

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);

–1.> < = >= <= <>
select * from emp where sal >3000;
select * from emp where sal <>5000;
select * from emp where sal =5000;

–2.模糊查询 like
select * from emp where ename like ‘%S%’; S不知道什么位置
select * from emp where ename like ‘S%’; S开头
select * from emp where ename like ‘%S’; S结尾
select * from emp where ename like '_o%’; 第3个字符为o的数据; 占位符

–3.排序
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 ;

–4.限制多少行
select * from emp limit 2;
select * from emp order by deptno asc,sal desc limit 2;

–5.聚合 group by … having …
–6.聚合函数 sum count avg max min

select
deptno,sum(sal) as sumsal
from emp
group by deptno;
– group by 字段 必须 出现在 select 字段
– having
select
deptno,sum(sal) as sumsal
from emp
group by deptno
having sum(sal)>10000;

select
deptno,job,sum(sal) as sumsal
from emp
group by deptno,job;

–7.数量 最大值等
select count(*) from emp ;
select max(sal) from emp ;

–8.组合
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

select
deptno,job,sal
from emp
where job=‘SALESMAN’

select
deptno,job,sum(sal)
from emp
where job=‘SALESMAN’
group by deptno,job

9.as 别名
sum(sal) as sumsal
xxxxxxxx as xx

10.union
drop table a;
create table a(id int,name double );
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 timestamp);
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 * from a
union all
select * from b

select id,name from a
union all
select id,address from b

–a.名称是第一张表决定 b.不用* 指定字段 c.对应字段类型保持一致

猜你喜欢

转载自blog.csdn.net/Jaserok/article/details/82875083