欢迎访问CSDN博客专栏CSDN专栏 Java全栈之路,Github主页

查询语句七大部分

1、 from 子句

1
2
3
4
5
6
7
8
9
10
11
12
select   字段名   from  表名;

sql 中的数学运算
select salary,salary*13 from s_emp;
字符串拼接
select first_name || last_name from s_emp;
NULL 值的处理
select salary*12,salary*12+salary*12*nvl(commission_pct,0)/100 from s_emp;
使用 排重关键字 distinct 就可以完成
select distinct salary from s_emp;
多字段排重 当多个字段的值都相同时 才进行去重
select distinct id,title,salary from s_emp;

2、 where 子句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
select  字段名  from  表名  where  条件;

表达闭区间的运算符 [a,b]
select id,first_name,salary from s_emp where salary between 1400 and 2500;
表达一个字段的值 出现在某个列表内
where 字段名 in 列表;
列表的表达是 (值1,值2,值3)
select id,first_name,dept_id from s_emp where dept_id in(31,41,50);
模糊查询运算符
找出s_emp 表中 first_name 中带 a 字符的 显示 first_name
select first_name from s_emp where first_name like '%a%';
找出s_emp 表中 first_name 中 正数第二个字符是a 字符的 显示 first_name
select first_name from s_emp where first_name like '_a%';
逻辑运算符 (多条件连接)
显示 s_emp 表中 每个员工的 id first_name salary 要求salary 在[1400,2500] 中
select id,first_name,salary from s_emp where salary>=1400 and salary <=2500;
查询s_emp 表的员工 在 31部门 或者在 41部门 或者在 50 最终显示 id first_name dept_id
select id,first_name,dept_id from s_emp where dept_id=31 or dept_id=41 or dept_id=50;
非 ! not
对立面问题
= != <> ^=
> <=
< >=
between a and b not between a and b
in not in ( where id not in (1,3,5) id!=1 and id!=3 and id!=5)
like not like
is null is not null

除了 is null 之外 其它的运算符 都要注意null 值问题

3、 order by 子句 排序

1
select  字段   from  表名  where 条件  order by 排序标准  排序方式;

4、 单行函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
	单行函数:
针对sql语句影响的每一行 都做处理 并且针对每一行都返回一个结果。
sql语句影响多少行就返回多少个结果
upper(par1) 变大写
lower(par1) 变小写
initcap(par1) 每个单词首字母变大写
length(par1) 求长度
concat(par1,par2) 连接两个字符串
nvl(par1,par2) nvl 可以处理任何类型 要求par1 和 par2 类型保持一致

substr(par1,par2,par3)
par1 要处理的字符串或者字段
par2 截取的位置 编号从 1 开始 可以是负数 -1 代表最后一个字符
par3 截取的长度

replace(par1,par2,par3)
par1 要处理的字符串 par2 被替换的内容 par3 替换成什么

组函数:
针对sql语句影响的所有行 只返回一个结果。
无论sql影响多少行 只返回一个结果。
round(par1,par2) 四舍五入函数
par1 要处理的数字 par2 处理的精度 默认是 0 可以省略
trunc(par1,par2) 截取函数
par1 要处理的数字 par2 处理的精度 默认是 0 可以省略
```

## 5、 多表查询
```java
select 表1别名.字段1,表2别名.字段2 from 表11别名,表22别名 where 表1别名.id=表2别名.id;

等值连接:
使用等号作为表的连接条件 叫等值连接
非等值连接:
不使用等号作为表的连接条件 叫非等值连接
grade 工资级别
losal 级别对应的低工资
hisal 级别对应的高工资
显示每个员工的 id salary 和 对应的工资级别
select id,salary,grade
from s_emp , salgrade
where salary between losal and hisal;
自连接
找出所有的领导
select distinct m.id,m.first_name
from s_emp e,s_emp m
where e.manager_id = m.id;
内连接的特点 符合连接条件的数据 被选中 不符合连接条件的数据被过滤掉
select distinct m.id,m.first_name
from s_emp e,s_emp m
where e.manager_id(+) = m.id and e.manager_id is null;

SQL99标准
sql99 的内连接标准
from a表 join b 表 on 表的连接条件 where 过滤条件;
from a表 inner join b 表 on 表的连接条件 where 过滤条件;
select d.name,r.name
from s_dept d,s_region r
where d.region_id = r.id;
select d.name,r.name
from s_dept d join s_region r
on d.region_id = r.id;
select d.name,r.name
from s_dept d inner join s_region r
on d.region_id = r.id;
sql99 外连接标准
a 表 left outer join b 表 on 连接条件 where 过滤条件;
a 表 right outer join b表 on 连接条件 where 过滤条件;
a 表 full outer join b表 on 连接条件 where 过滤条件; 把一个函数的返回值 作为另一个函数参数

左外连接
a 表 left outer join b 表 的意思就是 a 表发起连接,
a表发起连接就代表a表全部被匹配出来。
显示每个部门的名字 和 对应的地区的名字 没有地区编号的部门也要显示
select d.name,r.name
from s_dept d,s_region r
where d.region_id = r.id(+);

select d.name,r.name
from s_dept d left outer join s_region r
on d.region_id = r.id;

组函数
count(par1) 统计数据的个数
max(par1) 统计最大值
min(par1) 统计最小值
avg(par1) 统计平均值
sum(par1) 统计和

分组
按照一定的标准 把数据分成若干部分
select 字段
from 表名
where 条件
group by 分组标准 having 组函数的过滤条件
order by 排序标准 排序方式;
按照部门编号分组 统计每个部门的工资和部门的名字 显示工资和大于3000的部门
select dept_id,sum(salary),max(name)
from s_emp,s_dept where dept_id = s_dept.id
group by dept_id having sum(salary) > 3000;

DDL (data define language 数据定义语言)

1、建表

1
2
3
4
5
6
7
8
9
10
11
create table 表名(
字段名1 类型,
字段名2 类型,
字段名3 类型
);

类型:
number 数字类型
varchar2(n) 变长字符串类型
char(n) 定长字符串
data 日期类型

2、删表

1
drop table 表名;

3、改表(Oracle不加column,)

删除某个字段

1
alter table 表名 drop column 字段名;

增加一个字段

1
alter table 表名 add column 字段名 类型;

1、DML(data manipulation language 数据库操作语言)

1.1 插入语句 insert into

1.1.1 全字段插入 插入数据的顺序和 desc表名之后的顺序相同

1
2
3
4
insert into 表名 values(值1,值2,值3);
注意: 数字类型可以直接写 字符串类型的值应该使用单引号 值如果可以为null 则可以使用null
insert into sstudent100 values(1,'zhangsan','201700011',null);
commit;

1.1.2 选择部分字段插入

1
2
3
没有选择的字段 默认为null 这就要求大家必须包含所有的非空字段
insert into 表名(字段名1,字段名3) values(值1,值3);
commit;

1.2 删除语法

1
2
delete from 表名 where 条件;
commit;

1.3 修改数据

1
2
3
update 表名 set 字段名=值 where 条件;
update 表名 set 字段名=值,字段名2=值2 where 条件;
commit;

2、TCL(transaction control language 事务控制语句)

2.1 事务的概念

事务又叫交易 开发中希望把多个sql操作 看成一个逻辑整体 这些sql要求同时成功或者同时失败.

2.2 举例

银行转账

2.3 可以完成一个事务的语句

1
2
commit; 提交 确认
rollback; 回滚 撤销

2.4 事务四大特性

原子性 事务中的语句是一个逻辑整体不可分割
一致性 同时成功 同时失败 状态要保持一致
隔离性 一个事务中的操作 在没有提交之前 数据的变化 对另外一个事务而言不可见。
持久性 状态的持久

2.5 保存点

引入它就是为了打破事务的原子性
1
2
3
4
5
6
7
insert into student100 values(1,'test1','111','sysdate');
savepoint a;
insert into student100 values(2,'test2','222','sysdate');
savepoint b;
insert into student100 values(3,'test3','333','sysdate');
rollback to b;
commit;

3、日期类型 date

3.1 默认的日期类型

 dd-MON-yy
表现默认是两位年 实际上存储了 四位

3.2 显示 s_emp表中的数据 按照入职日期排序 显示id first_name start_date

1
select id,first_name,start_date from s_emp order by start_date;

3.3 如何按照指定格式 显示日期

1
2
3
4
5
6
par2 日期格式 日期格式
yyyy 四位年 mm 月 dd日
hh 12小时制 hh24 24小时制 mi 分钟 ss 秒
day 星期几 MON 英文月的缩写 month 英文月的全写

select id,first_name,to_char(start_date,'yyyy-mm-dd hh24:mi:ss') start_date from s_emp order by start_date;

3.4 如何插入一个日期

1
2
3
4
5
6
7
8
9
之前学过 null 和 sysdate sysdate 代表系统时间
如何插入未来的时间 或者 过去的时间点
2008-08-08 20:08:10
insert into student100(id,sdate) values(3,'08_AUG-08');
默认只能放入 年 月 日,时分秒信息是 0
如果需要时分秒信息 则使用 to_date就可以了
to_date(par1,par2) par1是一个日期字符串 par2 是日期的格式
insert into student100(id,sdate) values(4,to_date('2008-08-08 20:08:08','yyyy=mm-dd hh24:mi:ss'));
2012-12-22 23:59:59

3.5 日期的调整

1
2
3
4
5
6
7
8
9
10
11
select to_char(sysdate,'yyyy=mm-dd hh24:mi:ss') from dual;
调整一天 默认按照天为单位
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(sysdate+1,'yyyy-mm-dd hh24:mi:ss') from dual;

调整一个小时的一半(半个小时) 默认按照天为单位
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(sysdate+1/(24*2),'yyyy-mm-dd hh24:mi:ss') from dual;
将时间向前调半个小时
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')
to_char(sysdate-1/(24*2),'yyyy-mm-dd hh24:mi:ss') from dual;

3.6 特殊的单位调整

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
按照月为单位调整
add_months(par1,par2) par1 要调整的日期 par2 调整的月数 可以是负数

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(add_months(sysdate,1),'yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(add_months(sysdate,-1),'yyyy-mm-dd hh24:mi:ss') from dual;

对日期进行 截取
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(trunc(sysdate),'yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(round(sysdate),'yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(trunc(sysdate,'hh'),'yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(trunc(sysdate,'mm'),'yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(trunc(sysdate,'yy'),'yyyy-mm-dd hh24:mi:ss') from dual;

3.7 给你一个时间 获取到这个时间对应的月的最后一天的最后一秒对应的时间点

1
select to_char(add_months(trunc(sysdate,'mm'),1)-1/(24*60*60),'yyyy-mm-dd hh24:mi:ss') from dual;

4、约束

4.1 概念

约束是 对数据库表中字段加的限制,有了约束之后 可以让我们的数据更加准确 和 完整。

4.2 约束种类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
主键约束 primary key pk
如果给表的某个字段加了约束 则这个字段的值 不能重复,并且字段的值不能位null.一张表只能有一个主键.

唯一性约束 unique uk
如果给表的字段加了唯一性约束 则 这个字段的值不能重复
注意 唯一性约束 无法区分两个 null

非空约束 not null nn
如果给表的字段增加了 非空约束 则这个字段的值不能是 null

检查约束 check ck
如果给表的字段增加了 检查约束 则这个字段必须符合检查条件 符合检查条件的数据 才能进入数据库.

外键约束 references foreign key

4.3 约束的实现

列级约束: 在定义表的某一列时 直接在这一列的后面加约束限制叫列级约束.
表级约束: 在定义完美的所有列之后 再选择某些列加约束限制.

4.4 主键的列级约束实现

1
2
3
4
5
6
id number pk
oname varchar2(30)
create table myorder(
id number primary key,
name varchar2(30)
);
如果不给约束起名字 则系统会自动给约束进行一个命名 保证约束的名字不重复
但是这个名字 不方便记忆 并且 不方便排错 以及以后的约束管理。

4.5 如何给约束命名

1
2
3
4
5
6
7
8
9
10
11
12
13
constraint 约束的名字 约束对应的英文
约束的名字构成: 表名_字段名_约束简写

drop table myorder;
create table myorder(
id number constraint myorder_id_pk primary key,
name varchar2(30)
);
insert into myorder values(1,'test1');
insert into myorder values(1,'test1');
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.MYORDER_ID_PK) violated

4.6 练习

1
2
3
4
5
6
7
8
9
10
11
建立一张 订单表 叫order100
id number pk
ono varchar2(30) uk
odate date nn
create table order100(
id number constraint order100_id_pk primary key,
ono varchar2(30) constraint order100_ono_uk unique,
odate date constraint order100_odate_nn not null
);
insert into order100 values(1,null,sysdate);
insert into order100 values(2,null,sysdate);

检查约束

1
2
3
4
5
6
7
8
在上面的基础上 加一个字段 omoney number 要求omoney 大于35
drop table order100;
create table order100(
id number constraint order100_id_pk primary key,
ono varchar2(30) constraint order100_ono_uk unique,
odate date constraint order100_odate_nn not null,
omoney number constraint order100_omoney_ck check(omoney>35)
);

4.8 主键的表级约约束实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
create table order200(
id number,
ono varchar2(30),
odate date,
omoney number,constraint order200_id_pk primary key(id)
);

联合主键,表级约束最大的优势就是可以做联合约束(主键)
create table order300(
id number,
ono varchar2(30),
odate date,
omoney number,constraint order300_idono_pk primary key(id,pno)
);

4.9 练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
建立一张表 订单表 叫order1001
id number pk
ono varchar2(30) uk
odate date nn 列级约束(因为不支持表级)
除了odate 之外 其他的字段需要使用 表级约束

create odate order1001(
id number,
ono varchar2(30),
odate date not null,
omoney number,
constraint order1001_id_pk primary key(id),
constraint order1001_ono_uk unique(ono),
constraint order1001_omoney_ck check(omoney>35)
);
SQL
SQL