落雨

事务的四大属性

数据库事务的属性 
    数据库事务的属性包括四大类,即ACID。ACID分别是四个英文单词的首写字母,这四个英文单词是Atomicity(原子性)、 Consistency(一致性)、Isolation(隔离性)、Durability(持久性)。 
1、原子性 Atomicity 
    事务的原子性指的是,事务中包含的程序作为数据库的逻辑工作单位,它所做的对数据修改操作要么全部执行,要么完全不执行。这种特性称为原子性。 
2、一致性 Consistency 
    事务的一致性指的是在一个事务执行之前和执行之后数据库都必须处于一致性状态。这种特性称为事务的一致性。假如数据库的状态满足所有的完整性约束,就说该数据库是一致的。 
3、隔离性 Isolation 
    隔离性指并发的事务是相互隔离的。即一个事务内部的操作及正在操作的数据必须封锁起来,不被其它企图进行修改的事务看到。 
4、持久性 Durability 
    持久性意味着当系统或介质发生故障时,确保已提交事务的更新不能丢失。即一旦一个事务提交,DBMS保证它对数据库中数据的改变应该是永久性的,耐得住任何数据库系统故障。持久性通过数据库备份和恢复来保证。 
  严格来说数据库事务属性(ACID)都是由数据库管理系统来进行保证的,在整个应用程序运行过程中应用无需去考虑数据库的ACID实现。

Oracle 临时事务表 全局临时表_global temporary table

所有的操作都在一个事务里,事务提交后,此表清空,特别适合做插入删除频率特别高的临时表操作,比如插入完数据就开始查询,查询完就删掉等,用完就扔!

临时表分事务级临时表和会话级临时表。 
事务级临时表只对当前事务有效,通过语句:ON COMMIT DELETE ROWS 指定。 
会话级临时表对当前会话有效,通过语句:ON COMMIT PRESERVE ROWS语句指定。

-- Create table

create global temporary table WFM_TMP_WORKLIST

(

  proc_inst_id NUMBER(10),

  workitem_id  NUMBER(10),

  buzicondi    NVARCHAR2(500)

)

on commit delete rows;


---全局临时表创建语法
SQL> create global temporary table t_global_temp(a int)
  2  on commit delete rows;

Table created.

---查询表名
SQL> select table_name from user_tables where table_name='T_GLOBAL_TEMP';

TABLE_NAME
------------------------------------------------------------
T_GLOBAL_TEMP

--查询表对应的segment
SQL> select segment_name,segment_type from user_segments where segment_name='T_G
LOBAL_TEMP';

no rows selected

---插入数据
SQL> insert into t_global_temp values(1);

1 row created.

SQL> commit;

Commit complete.

--提交查询无记录
SQL> select * from t_global_temp;

no rows selected

--再次查询segment无记录,原因:创建全局临时表指定on commit delete rows一提交即清表
SQL> select segment_name,segment_type from user_segments where segment_name='T_G
LOBAL_TEMP';

no rows selected


SQL> insert into t_global_temp values(1);

1 row created.

--插入不提交即可查询到记录
SQL> select segment_name,segment_type from user_segments where segment_name='T_G
LOBAL_TEMP';

no rows selected

--提交与否皆不占用存储空间,引申问题:哪全局临时表的数据存储在哪儿呢?
SQL> select segment_name,segment_type from user_segments where segment_name='T_G
LOBAL_TEMP';

no rows selected


---以基于会话方式创建全局临时表
SQL> create global temporary table t_global_temp(a int) on commit preserve rows;


Table created.

SQL> insert into t_global_temp values(1);

1 row created.

---提交前查询
SQL> select * from t_global_temp;

         A
----------
         1

SQL> select segment_name,segment_type from user_segments where segment_name='T_G
LOBAL_TEMP';

no rows selected

SQL> commit;

Commit complete.

--提交后查询
SQL> select * from t_global_temp;

         A
----------
         1

---附上提交前后在另一会话查全局临时表测试,全局临时表的数据仅在当前会话可见
SQL> select * from t_global_temp;

no rows selected

SQL> /

no rows selected

SQL> desc t_global_temp;
 Name                                      Null?    Type
 ----------------------------------------- -------- -----------------------

 A                                                  NUMBER(38)

---测试全局临时表的alter table及create index及alter index
--如全局临时表正在使用alter table不能运行
SQL> alter table t_global_temp add b int;
alter table t_global_temp add b int
*
ERROR at line 1:
ORA-14450: attempt to access a transactional temp table already in use

---只有退出当前会话
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64
bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

C:\Users\123>sqlplus scott/system

SQL*Plus: Release 11.2.0.1.0 Production on Wed Jan 9 16:07:10 2013

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

---再次alter table即可成功
SQL> alter table t_global_temp add b int;

Table altered.

---在全局临时表构建索引
SQL> create index idx_temp on t_global_temp(a);

Index created.

--删除全局临时表索引
SQL> drop index idx_temp;

Index dropped.

SQL> select count(*) from t_global_temp;

  COUNT(*)
----------
         0

SQL> insert into t_global_temp select 1,3 from dual connect by level<3e5;

299999 rows created.

SQL> commit;

Commit complete.

---收集全局临时表的统计信息
SQL> exec dbms_stats.gather_table_stats(user,'t_global_temp');

PL/SQL procedure successfully completed.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------

Plan hash value: 62698482

----------------------------------------------------------------------------
| Id  | Operation          | Name          | Rows  | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |               |     1 |   257   (4)| 00:00:04 |
|   1 |  SORT AGGREGATE    |               |     1 |            |          |
|   2 |   TABLE ACCESS FULL| T_GLOBAL_TEMP |   599K|   257   (4)| 00:00:04 |
----------------------------------------------------------------------------

9 rows selected.

---仅插一条a值888888888888888888888的记录到全局临时表
SQL> insert into t_global_temp select 888888888888888888888,1 from dual;

1 row created.

SQL> commit;

Commit complete.

SQL> explain plan for select count(*) from t_global_temp where a=88888888888888
888888;

Explained.

--执行计划显示走了索引
SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------

Plan hash value: 1743356947

------------------------------------------------------------------------------
| Id  | Operation         | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |          |     1 |     3 |     3   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE   |          |     1 |     3 |            |          |
|*  2 |   INDEX RANGE SCAN| IDX_TEMP |     1 |     3 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------

Predicate Information (identified by operation id):

PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------

---------------------------------------------------

   2 - access("A"=888888888888888888888)

14 rows selected.

----全局临时表的操作限制

----不能分区,不能集簇化,不能iot化
Temporary tables cannot be partitioned, clustered, or index organized.
 
---不能指定外键约束
You cannot specify any foreign key constraints on temporary tables.
 
---不能包含nested table column
Temporary tables cannot contain columns of nested table.
 
----不能指定lob_storage_clause的参数:tablespace,storage_clause or logging_clause
You cannot specify the following clauses of the LOB_storage_clause: TABLESPACE, storage_clause, or logging_clause.
 
---不能启用并行update,delte,merge
Parallel UPDATE, DELETE and MERGE are not supported for temporary tables.
 
---在segment_atrributes_clause子句中,唯一可指定的参数是:tablespace
The only part of the segment_attributes_clause you can specify for a temporary table is TABLESPACE, which allows you to specify a single temporary tablespace.
 
---不支持分布式事务
Distributed transactions are not supported for temporary tables.


小结:全局临时表特别适用于存储中转结果,即临时计算的结果,非最终结果;
     可用于报表统计存储过程.


oracle 分页sql 分段查数据和分段求和 sql语句 和java代码

自己搞报表,数据量比较海的时候就不能一次性查询出来,要分段才可以,我们采用rownum伪列进行查询,伪列有个特点只能从1开始,想实现 x > 10 and x <20不能按照常规的那种一条sql搞定,必须要双层嵌套才可以的,这个算是个经验吧,同理,求和sum(某列)的时候,如果想查找从第100行到第200行的数据某列之和的时候,你也要嵌套一下,between and 是不行的...


1.最佳oracle分页sql

1

oracle 报错 column ambiguously defined

column ambiguously defined

ORA-00918: column ambiguously defined

java.sql.SQLException: ORA-00918: column ambiguously defined
ORA-00918: column ambiguously defined。中文名称:未明确定义的列!

可能定义了两个相同的列


你找找你后面的t里面是不是有两个相同的列,我也是这个问题,就是多了一个相同的列的查询,所以sb了..


我出错的sql语句是


select *

  from (select rownum as seq, t.*

          from (select item_code,

                       item_abv,

                       order_no,

                       unit_name,

                       assist_unit_name,

                       mb_qty,

                       inspection_qty,

                       mi_qty,

                       assist_mi_qty,

                       mo_qty,

                       assist_mo_qty,

                       on_hand_qty,

                       assist_qty,

                       ma_qty,

                       assist_ma_qty

                       assist_ma_qty -- 这一行出错了,列写重复了(删掉 运行,就ok啦)

                        -- assist_ma_qty as assist_ma_qty2 -- 不想删掉就这样写

                  from inv_book_master) t

         where rownum <= 10)

 where seq > 0


或者你参考 我的另一个帖子,教你怎么换一种方式rownum 分页,http://hi.baidu.com/ae6623/item/aaa12115f2e4ca09e75c3635