0
点赞
收藏
分享

微信扫一扫

sql注入原理与实战(二)数据库原理

sql注入原理与实战(二)数据库原理_内模式

数据库架构

数据库结构是指数据库系统中数据的组织方式和存储方式。常见的数据库结构有单用户结构、主从式结构、分布式结构、客户-服务器结构、浏览器-应用服务器/数据库服务器结构等。数据库结构包括三个层次:外模式、概念模式和内模式。其中,外模式是用户看到的视图,概念模式是数据库的逻辑结构,内模式是数据库在存储介质上的物理结构。保证数据的物理独立性是数据库设计的一个重要目标,即当数据库的存储结构改变时,只需要修改模式/内模式映像,而不需要修改应用程序,从而保证了数据与程序的物理独立性。

sql注入原理与实战(二)数据库原理_数据库_02

数据库的存储特点

  1. 数据存放到表中,然后表再放到库中
  2. 一个库中可以有多张表,每张表具有唯一的表名用来标识自己
  3. 表中有一个或多个列,列又称为“字段”,相当于Java中“属性”
  4. 表中的每一行数据,相当于Java中“对象”

完整语法

一、语法

select [TOP|DISTINCT] [选择列表]|[*]

from 数据源

[where 查询条件]

[group by 分组条件]

[having 过滤条件]

[order by 排序条件 asc|desc nulls first|last];

二、执行顺序

(5)select [(5-3)TOP|(5-2)DISTINCT] (5-1)[选择列表]|[*]
(1)from 数据源
(2)[where 查询条件]
(3)[group by 分组条件]
(4)[having 过滤条件]
(6)[order by asc|desc nulls first|last];

简单查询

--查询所有员工的信息

select * from emp;

别名查询

--查询所有员工的姓名

select e.ename from emp e;

去重查询

--查询所有部门的编号


select distinct e.deptno from emp e;

条件查询

一、运算符


条件运算符:>、>=、<、<=、=、<=>、!=、<>

逻辑运算符:and、or、not

模糊运算符:

like:%任意多个字符、_任意单个字符、如果有特殊字符,需要使用escape转义

between and

not between and

in

is null

is not null

二、演示

--查询工资>3000的员工信息

select * from emp where sal > 3000;

3.6、分组查询

--统计每个部门有多少个人

select deptno as "部门",count(*) as "人数" from emp group by deptno;

3.7、分组过滤

--统计部门人数>5人的部门的编号

select deptno as "部门", count(*) as "人数"from emp
group by deptno
having count(*) > 5;

3.8、排序查询

--按照员工主管编号由高到低进行排序,NULL值放到最后边

select * from emp order by mgr desc nulls last;

3.9、分页查询

--查询前10条员工的信息
--注意:Oracle中不支持limit,需要在原始表加上一列:行号,然后使用子查询来实现分页
select * 
from (select rownum hanghao,e.* from emp e) t
where t.hanghao >=1 and t.hanghao <= 10;

3.10、多表查询

内连接

隐式内连接:select * from emp e1, dept d1 where e1.deptno = d1.deptno;

显示内连接:select * from emp e1 inner join dept d1 on e1.deptno = d1.deptno;

外连接

左外连接

隐式左外连接:select * from emp e1,dept d1 where e1.deptno = d1.deptno(+);

显示左外连接:select * from emp e1 left outer join dept d1 on e1.deptno = d1.deptno;

右外连接

隐式右外连接:select * from emp e1,dept d1 where e1.deptno(+) = d1.deptno;

显示右外连接:select * from emp e1 right outer join dept d1 on e1.deptno = d1.deptno;

全外连接:select * from emp e1 full outer join dept d1 on e1.deptno = d1.deptno;

交叉连接

隐式交叉连接:select * from emp, dept;

显示交叉连接:select * from emp e1 cross join dept d1;

3.11、联合查询

并集运算:将两个查询结果进行合并

/*
	union  	: 它会去除重复的,并且排序
	union all  : 不会去除重复的,不会排序
*/

--工资大于1500或者20号部门下的员工
select * from emp where sal > 1500
union
select * from emp where deptno = 20;

--工资大于1500或者20号部门下的员工
select * from emp where sal > 1500
union all
select * from emp where deptno = 20;


交集运算:找两个查询结果的交集

--工资大于1500并且20号部门下的员工
select * from emp where sal > 1500
intersect
select * from emp where deptno = 20;


差集运算:找两个查询结果的差集

--1981年入职员工(不包括总裁和经理)
select * from emp where to_char(hiredate,'yyyy') = '1981'
minus
select * from emp where job = 'PRESIDENT' or job = 'MANAGER';

注意事项:

列的类型要一致

列的顺序要一致

列的数量要一致,如果不够,可以使用null填充

举报

相关推荐

0 条评论