大数据学习之SQL语句一

本文深入探讨了SQL中的各种数据类型,包括字符、整数、浮点数、日期等,并通过实例展示了如何使用这些数据类型创建表、插入、更新、删除及查询数据。此外,还介绍了SQL的高级特性,如模糊查询、排序、限制行数、聚合函数及分组等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 数据类型

数据类型详情链接

char 字符 varchar 字符串 int 整数 float 单精度 double 双精度
date日期 年月日 timestamp 年月日 时分秒

  • charvarchar后面可以接一个括号,里面填入一个数字表示字符个数,char是定长的,一旦确定在数据库中存储时就占这么多个字符,varchar是动态的当你的输入没有满足最大字符数时就按实际的字符数来存储。

2. 实例code

use louiedb;
create table louiedb.louiedata(
id int,
name varchar(100),
age int,


create_time timestamp ,
create_user varchar(100),
update_time timestamp ,
update_user  varchar(100)
);
insert into louiedata(id,name,age) values(1,'louie',26);
update louiedata set age=20 where id=1;
delete from louiedata where id=1;

select * from louiedata;

--------------------------------------------------------------
--模板
create table louiedb.louiedata_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 louiedb.louiedata_prod(name,age) values('mr.zhang',18); -- 字段名称和值一一对应
insert into louiedb.louiedata_prod 
values(2,'louie',18,'杭州','2018-09-29 23:00:00','l','2018-09-29 23:00:00','l'); -- 字段名称和值一一对应

insert into louiedb.louiedata_prod(name,age) values('huhu',18);
---------insert into ..... values .......;

update louiedata_prod set age=22;
update louiedata_prod set age=25,address='北京';  --多个字段修改 逗号分隔
update louiedata_prod set age=19,address='上海' where name ='louie';

update louiedata_prod set address='深圳' where name ='louie' and age=19; --且 (多个字段同时满足再做改正)
update louiedata_prod set address='深圳' where name ='louie' or age=25; --或 (只要有符合一个字段就可以做修改)
----update ... set .... where ....;

delete from louiedata_prod ;  ---将会删除所有的数据,此操作需谨慎
delete from louiedata_prod where  age =19;
#delete from ..... where ....;


select * from  louiedata_prod;
select id,name,age from  louiedata_prod;
select id,name,age from  louiedata_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不知道什么位置 (查找ename中包含有字符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;
---sumsal为别名

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 别名

---10.union
drop table a;
create table a(id int,name double );

---给表a加入数据
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);

--给表b加入数据
insert into b values(1,'2018-10-10 00:00:00');
insert into b values(2,'b2');
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.对应字段类型保持一致

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值