--设置oracle隔离级别
set transaction isolation level serializable
--oracle查询闪回
(1)用户必须拥有dbms_flashback 对这个包有execute权限
利用时间查询闪回:
一、开启闪存:execute dbms_flashback.enable_at_time(sysdate-10/1440)--查询10分钟以前的数据库闪存。
二、执行想过查询:select * from 。。。
三禁用闪存:execute dbms_flashback_disable();
(2)利用scn号查询闪回:
一、查询并记录数据库scn号
variable current_scn number;
exectue :current_scn:=dbms_flashback.get_system_change_number();
print current;
二、根据scn号闪回查询
execute dbms_flash_back_at_system_change_number(:current_scn);
三、执行查询
select * from ...
四、禁用闪存
execute dbms_flashback.disable();
(3)闪回查询相关sql:
获取数据删除前的一个时间点或scn,如下:
1.SQL>select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') time, to_char(dbms_flashback.get_system_change_number) scn from dual;
2.查询该时间点(或scn)的数据,如下:
SQL> select * from t as of timestamp to_timestamp('2010-06-29 22:57:47', 'yyyy-mm-dd hh24:mi:ss');
SQL> select * from t as of scn 1060174;
3.将查询到的数据,新增到表中。也可用更直接的方法,如:
SQL>create table tab_test as select * from t of timestamp to_timestamp('2010-06-29 22:57:47', 'yyyy-mm-dd hh24:mi:ss');
SQL>insert into tab_test select * from1060174;
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---oracle 用户创建及赋权
1.创建用户
create user_name identified by password;
default tablespace default_tablesipace->设置存放这个用户的表空间,可省略使用默认值。
temporary tablespace temporary_tablespace;->设置此用户临时表使用的表空间,可以省略使用默认值。
2.查看所有表空间:
select tablespace_name from dba_tablespaces;
3.给用户授权
grant 权限 to user_name(具体权限需要参考oracle权限列表)
4.修改用户密码
alter user user_name identified new_password;或者直接执行password命令
5.删除用户
drop user user_name;(在此用户下创建的表将全部被删除,要确保这些表已经不被其他用户使用)
6.给用户赋系统特权
grant execute any procedure to user_a
grant execute any procedure to user_a with admin option(加with admin option 表示user_a用户可以给其他用户赋此系统权限,可以不加)。
--查看用户拥有的系统权限
select * from user_sys_privs
--撤销用户系统权限
revoke execute any procedure from user_a
7.给用户赋对象特权
grant select,delete,update,insert,alter store.products to user_a(加with admin option 表示user_a用户可以给其他用户赋予同样权限)
--查询用户对象拥有的对象特权
select * from user_tab_privs_made;
--撤销用户对象权限
revoke select,update,insert,alter store.products from user_a;
user_tab_privs_recd:可查询用户被授予了哪些表上的哪些对象特权。
user_cd_privs_recd:查询用户被授予表的列特权。
8.创建同义词:
create synonym customers for store.customers;
--创建公共同义词
create public synonym customers for store.customers;
9.角色(角色是一组特权)
--创建一个角色
create role product_manager;
--给角色赋予权限
grant select,update,delete on product_types to product_manager;
--将角色赋予用户
grant product_manager to stere;_
--撤销角色
revoke overall_man;
--撤销角色的特权:
revoke all on products from product_manager;
---角色查询相关表
user_role_privs:检查赋予一个用户哪些角色;
role_sys_privs:检查意见赋予一个角色哪些系统特权;
role_tab_privs:检查已经赋予一个角色哪些对象特权;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------merge合并行(替代update可,起到优化update作用)
merge into product p-->要合并的目标表
using product_changes pc on(--->要合并的表
p.product_id=pc.produc_id-->关联条件
)
when matched then --->当有满足关联条件的数据时
update
set p.product_type_id=pc.product_type_id,
p.name=pc.name,
p.description=pc.description,
p.price=pc.descripotion
when not matched then--->当不满足关联条件的数据时
insert (p.product_id,p.prodct_type_id,p.name,p.description,p.price)
values(pc.product_id,pc.prodct_type_id,pc.name,pc.description,pc.price);
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------
----表
--创建表(略)
--查询表信息:通过all_tables 是视图查询。
--查询列信息:通过user_tab_columns视图可以获得有的有关表中各列的信息。
--修改表:
1.添加列:
alter table order_status add modified_by integer;
alter table order_status add initially_created date default sysdate not null;
2.添加虚拟列(oracle11G,虚拟列只引用表中已有的其他列。一般应用与合并表中的列或计算等等)
alter table salary_grades add(average_salary as ((low_salary+high_salary)/2));--新增列average_sarary被设置为low_salary和high_salary的平均值
3.修改列的长度:
alter table order_status modify status varcahr2(15)
4.修改列的精度(只有表还没有任何数据或空值是才可以降低数字列的精度)
alter table order_status modify id number(5)
4.修改列的数据类型:
alter table order_status modify status char(15);
5.修改列默认值:
alter table order_status modify last_modified default sysdate-1;(修改后的默认值只适用于新插入的数据)
6.修改列名:
alter table text rename column textpass to password ;
7.删除列:
alter table order_status drop column initially_created;
---添加约束:
1. 添加check约束:
alter table order_status add constraint order_status__ck check(s_status in('placed','pending','shipped'))--确保s_status只能输入固定值。
alter table order_status add constraint order_status_id_ch check(id>0);--确保输入的值只能大于0,如果表中已经有数据,那么需要确保已有的id大于0,或使用enable novalidate来设置约束值严重新增数据。
2.添加 not null约束
alter table order_status modify modified_by constraint order_status_nn not null(自定义约束名称)
alter table order_status modify last_modified not null;(系统生成约束名称);
3.添加foreign key约束:
alter table order_status add constraint order_status_modified_by_fk modifie_by references employyees(employee_id)
使用一个带有foreign key约束的on delete cascade子句,可以指定在父表中删除一行记录时,子表中匹配的所有行也都被删除。
alter table order_status add constraint order_status_modified_by_fk modified_by references employees(employee_id) no delete cascade;
使用一个带有foreign key约束的on delete set null子句,可以指定在父表中删除一行记录时,子表中匹配列被置为null。
alter table order_status add constraint order_status_modified_by_fk modified_by references employees(employee_id) no delete set null;
4.添加unique约束:
alter table order_status add constraint order_status_uq unique(s_status);
5.删除约束:
alter table order_status drop constraint order_status_uq;
6.禁用约束:
默认约束状态为启用,在创建约束时可以在constraint子句末尾添加disable来禁用约束
alter table order_status add constraint order_status_uq unique(s_status) disable;
禁用一个约束:(在子句后面加cascade可以禁用依赖于指定约束的任何约束)
alter table order_status disable constraint order_status_nn;
启用一个约束(启用一个约束时,表中的数据必须满足这个约束)
alter table order_status enable novalidate constraint order_status_nn;
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
model子句: 用来进行行间计算
例子1:(根据2003的销量预测2004年的销量)
select prd_type_id,year,month,sales_amount
from prd_type_id between 1 and 2
and emp_id=21
model ----------》model语句的开始
partition by(prd_type_id)------------->根据某个关键字段 给指定的结果进行分区(也就是给数组分区,个人理解把后面model生成的数组根据prd_type_id进行分组处理)
dimension by(month,year)---------->定义数组的维数 是month和year这就意味着必须提供月份和年份才能访问后面生成的数组, 和普通数组一样,只不过访问不是arr[1][3],变成了arr[3][2009]进行访问
measures(amount sales_amount)(----->生成数组,数组别名为sales_amount,数租中存储的值为amount销售量字段的值,更加年月维度形成一个二维数组
--下面开始进行行间的计算
sales_amount[1,2004]=sales_amount[1,2003],--将2004年1月的销售量设置为2003年1月份的销售量(另一种写法:sales_amount[month=1,year=2004]=sales_amount[month=1,year=2003])
sales_amount[2,2004]=sales_amount[2,2003]+sales_amount[3,2003],---意思同上
sales_amount[3,2004]=round(sales_amount[3,2003]*1.25,2)--意思同上
)
order by prd_type_id,year,month;
---measures 里面的主要应用计算方式:
0.between和and返回特定范围内的数据单元
......(上面的sql略,同例1相同)
measures(amount sales_amount)(
--计算获取2003年1-3月的平均值
sales_amount[1,2004]=round(avg(sales_amount)[month between 1 and 3,2003],2)
)
......(下面的sql略,同例1相同)
1.用any和is any访问所有数据单元
......(上面的sql略,同例1相同)
measures(amount sales_amount)(
--计算获取所有年的所有月的总和赋予2004年1月份
sales_amount[1,2004]=round(sum(sales_amount)[any,year is any],2)--也可以写成[any,any]或[any,year is any]或[month is any,any]
)
......(下面的sql略,同例1相同)
2.用currentv()获取某个维度的当前值
......(上面的sql略,同例1相同)
measures(amount sales_amount)(
--计算获取2003年当前月(也就是前面的1月)维度的值,就是2003年1月的值。
sales_amount[1,2004]=round(sales_amount[currentv(),2003]*1.25,2)
)
......(下面的sql略,同例1相同)
3.使用for访问数据单元
......(上面的sql略,同例1相同)
measures(amount sales_amount)(
--利用for循环访问,遍历2004年的1-3月,并且进行设置成2003年同月的值的1.25倍,increment 1代表从1月开始遍历
sales_amount[for month from 1 to 3 increment 1,2004]=round(sales_amount[currentv(),2003]*1.25,2)
)
......(下面的sql略,同例1相同)
4.处理空值和缺失值 is present
......(上面的sql略,同例1相同)
measures(amount sales_amount)(
--is present判断是否
sales_amount[for month from 1 to 3 increment 1,2004]=
case when sales_aesent mount[currentv(),2003] is present then
round(sales_amount[currentv(),2003]*1.25,2)
else
0
end
)
......(下面的sql略,同例1相同)
5.使用presentv(cell,expr1,expr2)--在model子句执行之前,如果cell值存在则返回expr1,否则返回expr2
......(上面的sql略,同例1相同)
measures(amount sales_amount)(
--is present判断是否
sales_amount[for month from 1 to 3 increment 1,2004]=
presentv(sales_amount[currentv(),2003],round(sales_amount[currentv(),2003]*1.25,2),0)
)
......(下面的sql略,同例1相同)
6.使用presentnnv(cell,expr1,expr2)--在model子句执行之前,如果cell值存在且不为null则返回expr1,否则返回expr2
......(上面的sql略,同例1相同)
measures(amount sales_amount)(
--is present判断是否
sales_amount[for month from 1 to 3 increment 1,2004]=
presentnnv(sales_amount[currentv(),2003],round(sales_amount[currentv(),2003]*1.25,2),0)
)
......(下面的sql略,同例1相同)
7.使用ignore nav 和keep nav
8.更新已有单元:rules update--默认情况下,如果表达式左端的引用存在,则更新该单元,否则就在新的数组中创建该单元,rules update关键字可以改变这种默认情况,如果左端的更新单元不存在,则不创建新记录。
......(上面的sql略,同例1相同)
measures(amount sales_amount)
rules update(
--这时,如果的les_amount[1,2004]不存在时,则不会创建新记录,这时就查不到2004-1的数据了。
sales_amount[for month from 1 to 3 increment 1,2004]=
presentnnv(sales_amount[currentv(),2003],round(sales_amount[currentv(),2003]*1.25,2),0)
)
----------------------------------------------------------------------------------------------------------------------
-----索引------
---创建B索引(普通索引)
1.create index i_indexName on customers(cus_name)--一般索引
2.create unique iun_indexName on customers(cus_id)---唯一索引(oracle在创建unique constraint时自动创建unique constraint)
注意:当任何单个查询检索的行少于或等于整个表行数的10%,索引非常有用
3.drop index index_name--删除索引
---创建位图索引
1.create bitmap index index_name on customers(status)
注:如果某列的不同值数量少于表中行数的1%,或者如果某列的值重复次数多余100次,适合做位图索引的列(也就是某个列有大量重复值,如100万行数据小于等于1万个不同值)
如果所有列where查询采用了函数,索引将无效,必须创建基于对于函数的索引
------------------------------------------------------------------------------------------------------------------------
-----序列--------------
--创建序列
create sequence s_test2
statr with 1 --起始值为1
increment by 1--增量为1
minvalue 1--最小值
maxvalue 9999999--最大值
cycle--当序列增到最大值或最小值时继续生成序列,默认值为nocycle
cache 5--内存中缓存5个整数
order;--这个序列是有序序列
--获取序列
s_test2.nextval--获取下一个序列值
s_test2.currval--获取序列当前值