Oracle
一.关键字
sysdate:系统时间
primary key:主键
check:检查
unique:唯一
references:外键
二.代码
2.1修改表结构
括号里语法与创建表一致
2.1.1添加字段
alter table 表名 add(字段名1 类型1,字段名2 类型2)
alter table 表名 add classid number references t_class(classid)
2.1.2修改字段
alter table 表名 modify (字段名 varchar(100)...)
alter table 表名 rename column 原列名 to 新列名
2.1.3删除列drop
alter table 表名 drop column 列名;
2.2删除表结构
drop table 表名;
2.3查询
1. where:条件
where
>group by
>having
>order by
distinct:放在select后,字段前,对指定字段去重 and--并且 or--或者 优先级 and > or
between和in(前面都可以加not) --100到200之间 between(100,200) --100和200 in(100,200) --例如: --2500到3500之间 select * from emp where sal between 2500 and 3500; --2500到3500之外 select * from emp where sal not between 2500 and 3500; -- in()同理
is null 和 is not null 值为空和值非空
order by排序 格式:order by 字段名 asc/desc 根据指定字段名asc(升序)/desc(降序)排序,默认升序,则asc可以省略 多字段格式:order by 字段名1 asc,字段名2 desc...
2.4单表复杂查询
2.4.1统计函数
只能获取一个结果,但可以分组实现每一组一个结果
select 字段 from 表 where 条件 group by 字段--可以写多个
count()
只能放在select后
--统计该字段非空行数
count(字段)
--统计该字段空行数
count(*)-count(字段)
sum()
avg()
max()
min()
2.4.2分组统计
group by
1.题目直接告诉你需要对什么进行分组,然后分别统计各组数据的时候使用分组查询
2.如果在select后面同时出现了字段名和分组函数就要用分组
3.group by后面写的就是select后面的所有字段名(不能有函数)
2.5新增insert into
2.5.1插入记录
--提交
insert into t value(1,1,'a');
commit;
--回滚
insert into t value(1,1,'a');
rollback;
--commit和rollback可以在多条语句后一次性执行
2.5.2插入时间-使用to_date()函数
1.会自动校验日期正确性(包括闰月)
2.可以加入未来时间
--1.字符串格式
insert into t value(to_date('2001-01-01','YYYY-MM-DD'));
commit;
--2.数字格式
insert into t value(to_date(20010101,'YYYY-MM-DD'));
commit;
2.6修改update
update emp
set sal=666
where deptno = 20;
commit;
2.7删除delete/truncate
delete(dml)
truncate(ddl)
事务:通俗理解就是做一件事情的过程,这个过程有两种结果:成功\失败,在数据库中发出一条dml语句即开启一个事务,开启事务就一定要结束事务,
结束事务的两种方式:
a1)、commit,提交事务,所有的操作都生效,操作的结果直会永久保存在数据库中
a2)、rollback,回滚事务,所有的操作都不会生效,操作的结果直接扔掉,不会永久保存到数据库中
删除表的另一种方式:
truncate table 表名
截断表,即删除表中所有的数据,和delete的区别:
delete:会发起事务,可以有where过滤条件(即删除指定的数据),属于dml语句,删除效率比truncate低
truncate:不会发起事务,不能有where过滤条件(即只能删除表中所有的数据),属于ddl语句,删除效率比delete高,
但有危险性
若表中有列是其他表的外键,则不能在该表中使用truncate(即truncate会影响外键列)
drop table 表名:即删除表的数据又删除表的结构。
2.5多表查询
能够放在一起查询的表一定有关系
主外键
主从表
2.5.1等值连接
- 先确认从哪些表查询
- 再确定select后面的内容都属于哪个表并添加
- 必须要有连接条件(两个表的主外键连接)
- 如果查询的时候还有别的条件,用and写在连接条件的后面
select 表1别名.字段, 表2别名.字段
from 表1 表1别名, 表2 表2别名,....
where 表1.字段 = 表2.字段
2.5.2分析题目的思路:
- 如果有多个表,先找到这些表的相同字段;
- 确定主从表。
2.5.3查询的总思路:
- 分析题目,确定查询的数据涉及哪些表;
- 如果查询的数据必须涉及多张表,就要用到多表查询:
找到两张表中相同意思的字段,在where语句后面用等于号连接。 - 如果需要对单条数据做过滤,就把过滤条件追加到where语句后面,并用and连接;
- 根据题目要求,填写select语句后面的内容;
- 如果需要对多组数据进行统计,就要用到group by语句;
- 如果需要对分组后的数据进行过滤,就要用到having语句;
- 如果需要对数据进行排序,就要用到order by语句。
2.6子查询
必须用圆括号括起来
2.6.1单行值子查询
只返回一个值(一行/一条记录)
--1
select ename,job,sal
from emp
where sal > (select sal from emp where ename = 'SCOTT')
--2
select e.ename,e.job,e.sal,d.loc
from emp e,dept d
where e.deptno = d.deptno
and e.sal > (select sal from emp where ename = 'SCOTT')
and d.loc = 'NEW YORK'
--3
select ename,job,sal
from emp
where sal = (select max(sal) from emp)
--4
select ename,job,sal
from emp
where deptno = (select deptno from dept where loc = 'CHICAGO')
2.6.2多行值子查询
返回多个值,用in(),not in()函数筛选
--1
select *
from emp
where deptno
in (select distinct deptno from emp where sal >=1000 and sal <=3500)
--2
select ename,job,sal
from emp
where job
not in(select job from emp where deptno = 10)
--3
select ename,job,deptno
from emp
where job
in(select job from emp where deptno = 20)
注意:
1.数据库的增删改都不能使用多表联查,如果删除,修改的数据涉及多张表,必须使用子查询
2.删除的数据存在于多张表,必须先删除从表的数据,再删除主表的数据