-- 查询 emp 表
select * from emp;
-- where 两种方式
select * from emp where empno = 7369;
select * from emp where empno = '7369';
-- 关系运算符 > < != <> = <= >=
select * from emp where sal > 1000;
-- between and
select * from emp where sal between 1000 and 2000;
-- null 查询消费为null的
select * from emp where comm is null;
-- null 查询消费不为null的 (两种方式)
select * from emp where comm is not null;
select * from emp where not comm is null;
-- 坑
select * from emp where comm not is null ;
-- in not in
-- 查询id是 7369 7499 7521
select * from emp where empno in (999,7369,7499,7521)
-- 查询id在 7369 7499 7521
select * from emp where empno in (999,7369,7499,7521,null)
-- 坑 not in 内只要是导入了空值,查询结果就为null
select * from emp where empno not in (999,7369,7499,7521,null)
-- like % _ 查询 姓名中带有A
select * from emp where ename like '_A%'
-- desc降序 asc升序 order by
-- 根据部门查询30 如果部门相同 根据job排序(asc) 如果job一样 根据empno(desc)
select * from emp where deptno = 30 order by job ,empno desc
-- String字符串函数
update emp set ename = 'zhanmusi' where empno = 7900
-- upper 把小写字母转换为大写字母
select upper(ename) from emp where empno = 7900
-- 临时表 dual
-- 让它看起来就一行,非常的简洁所以我才是用了临时表dual
-- lower 把大写字母转换为小写字母
select lower('LAOwang') from dual
-- 计算String字符串的长度
select length (' lao wang ') from dual;
-- 利用trim去除字符串前后的空格
select length (trim(' lao wnag ')) from dual;
-- 日期函数 SYSDATE SYSTIMESTAMP
select SYSDATE-1 from dual -- -1是当前日期减一天
-- 转换函数 to_date()
select to_date('2020-09-09','yyyy-MM-dd') from dual;
-- to char()
select to_char(11) from dual
-- to_number() 转换失败
select to_number('aaa') from dual ;
-- 查询 员工的全年工资
-- 处理NULL数据
-- 语法:数字 NVL(列,默认值);
select empno , ename , (sal+nvl(comm,0))*12 from emp;
-- DECODE()
select decode (1,2,'xiaowang',3,'laowang',1,'dengdeng','laoqian') from dual;
select decode (5,2,'xiaowang',3,'laowang',1,'dengdeng','laoqian') from dual;
-- COUNT()、AVG()、SUM()、MIN()、MAX();
select SUM(sal) from emp group by deptno ;
-- 伪行 rownum
-- 坑 可以用 < <= 不能用 = > >=
select rownum ,emp.* from emp where rownum < 10 ;
-- 利用伪行 写一个分页查询 temp相当于一个临时表
select * from
(select rownum rm , emp.* from emp where rownum <= 6 ) temp
where temp.rm >= 4;
-- 伪列
-- 处理 数据表中没有主键的相同数据
select * from dept;
-- 复制
create table mydept as select * from dept;
select * from mydept
insert into mydept values(10,'ACCOUNTING','NEW YORK')
-- 保留原始数据
-- 逻辑:1.先查出最小的rowid 2.然后把其余的都删掉(部门为10的)
-- 1.先查出最小的rowid
select min(rowid) from mydept where deptno = 10 ;
-- 2.然后把其余的都删掉(部门为10的)
delete from mydept me
where me.rowid >(
select min(rowid) from mydept where deptno = 10)
and me.deptno = 10
select * from mydept;
--------------这是一条优美得分割线-----------
-- 序列
-- 我们在做项目的时候,数据库的表可以设置为自增 sequence
-- 案例
-- 创建序列s001
create sequence s001;
-- 查询当前数据库中所有的序列
select * from user_sequences;
-- 应用此序列(序列中有两个值 nextval下一个值 currval当前值 )
-- 先写nextval下一个值 然后才可以使用 currval当前值
select s001.nextval from dual;
select s001.currval from dual;
-- 创建表
create table person(
pid number(10),
pname varchar2(255)
)
-- 查询person
select * from person;
insert into person values (s001.nextval,'xiaofang')
-- 删除sequence
drop sequence s001 ;
-- 创建序列 步长为2
create sequence s002
increment by 2;
select s002.nextval from dual ;
-- 创建序列步长为2 最小值为 3 最大值为 18
create sequence s003
increment by 2
minvalue 3
maxvalue 18
select s003.nextval from dual ;
--创建序列 s004 步长为2 最小值为 3 最大值为10 循环 + 缓存
-- cycle 循环 , cache 缓存
create sequence s006
increment by 2
minvalue 3
maxvalue 10
cycle
cache 2;
-- 3,5,7,9 记录在为4个数字
-- cache 缓存设置 最大可以=缓存个数 最小=2
select s006.nextval from dual ;
-- 视图 view
create view v001 as
select * from emp where deptno = 20;
-- 运行上面的创建视图 语句 报错 提升权限不足
-- 在cmd中输入sqlplus /nolog
-- 在进行连接 conn sys/123 as sysdba
-- 运行完 提示 已连接
-- 进行授权 grant create view toscott
-- 最后 提示 授权成功
-- 再最后运行上面的 创建视图语句
-- 查询视图v001
select * from v001
-- 视图的原则! 不能对原表进行修改
-- 修改 7369 的deptno 为 30
update v001 set deptno = 30 where empno = 7369
-- 进过查询emp原表发现,通过视图可以对原表进行修改
select * from emp where empno = 7369
-- 所以创建视图的时候,需要加上条件
create view v002 as
select * from emp where deptno = 20
with check option;
select * from v002;
-- 修改 7369 的deptno 为 30
update v002 set deptno = 30 where empno = 7566;
-- 修改 7369 的ename 为 laowang
update v002 set ename = 'laowang' where empno = 7566;
-- 经过查询emp原表发现,通过视图可以对原表进行修改(只是限制了where条件后面的数据)
select * from emp where empno = 7566
-- with read only (只读)
create view v003 as
select * from emp where deptno = 20
with read only;
select * from v003;
-- 修改7566 的deptno 为 30
update v003 set deptno = 30 where empno = 7566;
-- 修改7566 的ename 为 laownag
update v003 set ename = 'laowang' where empno = 7566;
-- 总结:推荐使用with read only ;
--------------------------分割线---------------------------
-- 同义词 如果涉及到表数据非常大的表 迁移 (他的速度最快)
-- CREATE [PUBLIC] SYNONYM 同义词名称 FOR 用户名.表名称 ;
select * from de;
create synonym em for scott.emp;
create synonym de for scott.dept;
-- 索引 提高速度
select * from emp where sal > 1500;
-- 语法
--CREATE INDEX emp_sal_ind ON emp(sal) ;
create index emp_sal_ind on emp(sal);
--什么情况下推荐使用索引
--(1)表经常进行 INSERT/UPDATE/DELETE 操作 增删改
--(2)表很小(记录超少)
--(3)列名不经常作为连接条件或出现在 where 子句中
--(4)对于那些定义为text, image和bit, blob数据类型的列不应该增加索引
-- 权限
-- 创建用户 用户名dog 密码123
create user dog identified by 123;
-- 创建dog用户的登录权限 grant to (进入管理员进行操作)
grant create session to dog ;
-- 创建dog用户的建表权限
grant create table to dog;
-- 提供了两个角色 connect , resource
GRANT CONNECT,RESOURCE to dog ;
-- 创建dog的视图权限
grant create view to dog
-- 创建dog的 序列权限
grant create sequence to dog;
-- 所以 如果我们嫌麻烦,我们可以给数据库一个超级管理员权限
grant dba to dog;
----------------------分割线--------------------------
-- 存储过程(了解)
create or replace procedure p1
is
begin
dbms_output.put_line(' 执行了 ');
end p1;
-- 调用 存储过程
call p1() ;
create or replace procedure p3(newcount out number)
is
begin
-- into 关键字 将查询的结果赋值给变量 newcount
dbms_output.put_line(' 执行了 ');
end p3;
declare
newname varchar2(32);
begin
newname := ' 小红 ';
p3(newname);
end;
----------------------分割线--------------------------
-- 触发器(重点)
-- 语法如下: trigger 触发器 的意思
create or replace trigger t2
before | after
delete | update | save
on ta -- ta 是某个表
for each row
begin
sql...
end;
-- 创建表 p1
create table p1 (
pid number(10),
pname varchar2(255)
);
-- 创建表log_p1
create table log_p1(
pid number(10),
pname varchar2(255)
);
select * from p1 ;
select * from log_p1 ;
-- 创建触发器: 当对p1表进行添加的时候,触发器触动了>>>>>将添加的内容存到log_p1中,当一个备份
create or replace trigger t1
before
insert
on p1
for each row
begin
insert into log_p1 values(:new.pid,:new.pname);
end;
-- 开始测试
-- 向p1表中添加数据
insert into p1 values(1003,'laowang');
-- 创建触发器: 当对p1表进行删除的时候,触发器触动了>>>>>将删除的内容存到log_p1中,当一个备份
create or replace trigger t2
before
delete
on p1
for each row
begin
insert into log_p1 values(:old.pid,:old.pname);
end;
-- 删除 p1中的1002
delete from p1 where pid = 1002;
-- 创建触发器: 当对p1表进行修改的时候,触发器触动了>>>>>将修改 之前 内容存到log_p1中,当一个备份
create or replace trigger t3
before
update
on p1
for each row
begin
insert into log_p1 values(:old.pid,:old.pname);
end;
-- 修改 1001 改名为 wangfang
update p1 set pname = 'wangfang' where pid = 1001;
-- 创建触发器: 当对p1表进行修改的时候,触发器触动了>>>>>将修改 之后 内容存到log_p1中,当一个备份
create or replace trigger t4
before
update
on p1
for each row
begin
insert into log_p1 values(:new.pid,:new.pname);
end;
-- 修改 1001 改名为 wangfang22
update p1 set pname = 'wangfang22' where pid = 1001;
-- 总结: 当 insert 我们使用 :new
-- 当 delete 我们使用 :old
-- 当 update 我们使用 :new 还可以使用 :old