发布新日志

  • admin SQLs - Check session or lock table

    2012-03-06 16:06:06

    select    nvl(ses.USERNAME,'ORACLE PROC') username,
        OSUSER os_user,
        PROCESS pid,
        ses.SID sid,
        SERIAL#,
        PHYSICAL_READS,
        BLOCK_GETS,
        CONSISTENT_GETS,
        BLOCK_CHANGES,
        CONSISTENT_CHANGES
    from    v$session ses,
        v$sess_io sio
    where     ses.SID = sio.SID
    order     by PHYSICAL_READS, ses.USERNAME;


    select  nvl(ss.USERNAME,'ORACLE PROC') username,
        se.SID,
        sn.NAME stastic,
        VALUE usage
    from     v$session ss,
        v$sesstat se,
        v$statname sn
    where      se.STATISTIC# = sn.STATISTIC#
    and      se.SID = ss.SID
    and    se.VALUE > 0
    order      by sn.NAME, se.SID, se.VALUE desc;

    select * from v$session;  --489 491 484


    select    nvl(ses.USERNAME,'ORACLE PROC') username,
        OSUSER os_user,
        PROCESS pid,
        ses.SID sid,
        SERIAL#,
        PHYSICAL_READS,
        BLOCK_GETS,
        CONSISTENT_GETS,
        BLOCK_CHANGES,
        CONSISTENT_CHANGES
    from    v$session ses,
        v$sess_io sio
    where     ses.SID = sio.SID
    order     by PHYSICAL_READS, ses.USERNAME;

    select * from v$session where suser = 'rmai';

    SELECT s.inst_id,
           s.sid,
           s.serial#,
           p.spid,
           s.username,
           s.program
    FROM   gv$session s
           JOIN gv$process p ON p.addr = s.paddr AND p.inst_id = s.inst_id
    WHERE  s.type != 'BACKGROUND'
    and SID in (484,
    491,
    499,
    531);

    --Find out the running sql script
    SELECT SE.sid, SE.serial#, PR.spid, SE.status, SUBSTR(SE.program, 1, 10) PROG,
      SUBSTR(SE.machine, 1, 10) MACH,
      SQ.sql_text
    FROM v$session SE, v$sqlarea SQ, v$process PR
    WHERE SE.paddr = PR.ADDR(+)
      AND SE.sql_address = SQ.address(+)
      AND schemaname <> 'SYS'
    ORDER BY SE.sid;


    --Find out the lock table
    select c.owner, c.object_name, c.object_type, b.sid, b.serial#, b.status, b.osuser, b.machine, b.logon_time, vp.spid
    from v$locked_object a , v$session b,  dba_objects c, v$process  vp
    where b.sid = a.session_id and vp.addr=b.paddr and a.object_id = c.object_id;


    --Kill session
    alter system kill session 'sid,serial#' ;


    --Check Users
    select
      username,
      osuser,
      terminal
    --  utl_inaddr.get_host_address(terminal) IP_ADDRESS
    from
      v$session
    where
      username is not null
    order by
      username,
      osuser;
  • Oracle 查看主外键约束

    2012-03-05 09:17:04

    select a.constraint_name, a.table_name, b.constraint_name 
    from user_constraints a, user_constraints b
    where a.constraint_type = 'R' 
    and b.constraint_type = 'P' 
    and a.r_constraint_name = b.constraint_name

    P 代表主键
    R 代表外键

    通过关联,能查询到你所想要的一切。

     

    select constraint_name from dba_cons_columns where table_name='';
    Alter table XX drop constraint sys…

    /////////////////////////////

    user_constraints,user_cons_columns查看外键

    Oracle 查看一个表对应的主键和外键的约束关系,查看的语句:

    select a.owner 主键拥有者

             ,a.table_name 主键表

            ,b.column_name 主键列

            ,C.OWNER 外键拥有者

            ,c.table_name 外键表

           ,d.column_name 外键列
    from user_constraints a
    left join user_cons_columns b

                on a.constraint_name=b.constraint_name
    left join user_constraints C

                ON C.R_CONSTRAINT_NAME=a.constraint_name
    left join user_cons_columns d

                on c.constraint_name=d.constraint_name
    where a.constraint_type='P'

        and a.table_name='XXX' --需要查看主外键关系的表
    order by a.table_name

    宁外的一种写法

    select
    a.owner 外键拥有者,
    a.table_name 外键表,
    substr(c.column_name,1,127) 外键列,
    b.owner 主键拥有者,
    b.table_name 主键表,
    substr(d.column_name,1,127) 主键列
    from
    user_constraints a,
    user_constraints b,
    user_cons_columns c,
    user_cons_columns d
    where
        a.r_constraint_name=b.constraint_name
    and a.constraint_type='R'
    and b.constraint_type='P'
    and a.r_owner=b.owner
    and a.constraint_name=c.constraint_name
    and b.constraint_name=d.constraint_name
    and a.owner=c.owner
    and a.table_name=c.table_name
    and b.owner=d.owner
    and b.table_name=d.table_name

    数据字典表列说明:

    desc user_constraints

    Name Comments
    OWNER 表的所有者
    CONSTRAINT_NAME 约束名
    CONSTRAINT_TYPE 约束类型
    TABLE_NAME Name associated with table with constraint definition
    SEARCH_CONDITION Text of search condition for table check
    R_OWNER Owner of table used in referential constraint
    R_CONSTRAINT_NAME Name of unique constraint definition for referenced table
    DELETE_RULE The delete rule for a referential constraint
    STATUS Enforcement status of constraint - ENABLED or DISABLED
    DEFERRABLE Is the constraint deferrable - DEFERRABLE or NOT DEFERRABLE
    DEFERRED Is the constraint deferred by default - DEFERRED or IMMEDIATE
    VALIDATED Was this constraint system validated? - VALIDATED or NOT VALIDATED
    GENERATED Was the constraint name system generated? - GENERATED NAME or USERNAME
    BAD Creating this constraint should give ORA-02436. Rewrite it before2000 AD.
    RELY If set, this flag will be used in optimizer
    LAST_CHANGE The date when this column was last enabled or disabled
    INDEX_OWNER The owner of the index used by the constraint
    INDEX_NAME The index used by the constraint
    INVALID  
    VIEW_RELATED  


    desc user_cons_columns;

    Name Default Comments
    OWNER Owner of the constraint definition
    CONSTRAINT_NAME Name associated with the constraint definition
    TABLE_NAME Name associated with table with constraint definition
    COLUMN_NAME Name associated with column or attribute of object column specified in the constraint definition
    POSITION Original position of column or attribute in definition

     

    http://blog.csdn.net/maqinqin/archive/2009/05/26/4217045.aspx

    desc   后发现
    user_constraints是表约束的视图,描述的是约束类型(constraint_type)是什么,属于哪些表(table_name),如 果约束的类型为R(外键)的话,那么r_constraint_name字段存放的就是被引用主表中的主键约束名。   
    user_cons_columns是表约束字段的视图,说明表中的和约束相关的列参与了哪些约束。这些约束有主键约束,外键约束,索引约束.
    两者可以通过(owner,constraint_name,table_name)关联

    select
    a.owner 外键拥有者,
    a.table_name 外键表,
    substr(c.column_name,1,127) 外键列,
    b.owner 主键拥有者,
    b.table_name 主键表,
    substr(d.column_name,1,127) 主键列
    from
    user_constraints a,
    user_constraints b,
    user_cons_columns c,
    user_cons_columns d
    where
        a.r_constraint_name=b.constraint_name
    and a.constraint_type='R'
    and b.constraint_type='P'
    and a.r_owner=b.owner
    and a.constraint_name=c.constraint_name
    and b.constraint_name=d.constraint_name
    and a.owner=c.owner
    and a.table_name=c.table_name
    and b.owner=d.owner
    and b.table_name=d.table_name

    数据字典表列说明:

    desc user_constraints

    Name                                      

    http://www.blogjava.net/sunzhong/articles/289139.html

     

    /////////////////////////////////////

     

    Oracle 查看一个表对应的主键和外键的约束关系,查看的语句:

    select a.owner 主键拥有者

             ,a.table_name 主键表

            ,b.column_name 主键列

            ,C.OWNER 外键拥有者

            ,c.table_name 外键表

           ,d.column_name 外键列
    from user_constraints  a
    left join user_cons_columns b

                on  a.constraint_name=b.constraint_name
    left join user_constraints C

                ON C.R_CONSTRAINT_NAME=a.constraint_name
    left join user_cons_columns d

                on c.constraint_name=d.constraint_name
    where  a.constraint_type='P'

        and  a.table_name='XXX' --需要查看主外键关系的表
    order by a.table_name

    宁外的一种写法

    select
    a.owner 外键拥有者,
    a.table_name 外键表,
    substr(c.column_name,1,127) 外键列,
    b.owner 主键拥有者,
    b.table_name 主键表,
    substr(d.column_name,1,127) 主键列
    from
    user_constraints a,
    user_constraints b,

    user_cons_columns c,
    user_cons_columns d
    where
        a.r_constraint_name=b.constraint_name
    and a.constraint_type='R'
    and b.constraint_type='P'
    and a.r_owner=b.owner
    and a.constraint_name=c.constraint_name
    and b.constraint_name=d.constraint_name
    and a.owner=c.owner
    and a.table_name=c.table_name
    and b.owner=d.owner
    and b.table_name=d.table_name

    数据字典表列说明:

    desc user_constraints

     

    Name Comments
    OWNER 表的所有者
    CONSTRAINT_NAME 约束名
    CONSTRAINT_TYPE 约束类型
    TABLE_NAME Name associated with table with constraint definition
    SEARCH_CONDITION Text of search condition for table check
    R_OWNER Owner of table used in referential constraint
    R_CONSTRAINT_NAME Name of unique constraint definition for referenced table
    DELETE_RULE The delete rule for a referential constraint
    STATUS Enforcement status of constraint -  ENABLED or DISABLED
    DEFERRABLE Is the constraint deferrable - DEFERRABLE or NOT DEFERRABLE
    DEFERRED Is the constraint deferred by default -  DEFERRED or IMMEDIATE
    VALIDATED Was this constraint system validated? -  VALIDATED or NOT VALIDATED
    GENERATED Was the constraint name system generated? -  GENERATED NAME or USERNAME
    BAD Creating this constraint should give ORA-02436.  Rewrite it before2000 AD.
    RELY If set, this flag will be used in optimizer
    LAST_CHANGE The date when this column was last enabled or disabled
    INDEX_OWNER The owner of the index used by the constraint
    INDEX_NAME The index used by the constraint
    INVALID  
    VIEW_RELATED  

    desc user_cons_columns;

     

    Name Default Comments
    OWNER Owner of the constraint definition
    CONSTRAINT_NAME Name associated with the constraint definition
    TABLE_NAME Name associated with table with constraint definition
    COLUMN_NAME Name associated with column or attribute of object column specified in the constraint definition
    POSITION Original position of column or attribute in definition
  • DML、DDL、DCL区别

    2011-09-16 09:44:28

    总体解释:
    DML(data manipulation language):
           它们是SELECT、UPDATE、INSERT、DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作的语言
    DDL(data definition language):
           DDL比DML要多,主要的命令有CREATE、ALTER、DROP等,DDL主要是用在定义或改变表(TABLE)的结构,数据类型,表之间的链接和约束等初始化工作上,他们大多在建立表时使用
    DCL(Data Control Language):
           是数据库控制功能。是用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等)语句。在默认状态下,只有sysadmin,dbcreator,db_owner或db_securityadmin等人员才有权力执行DCL

    详细解释:
    一、DDL is Data Definition Language statements. Some examples:数据定义语言,用于定义和管理 SQL 数据库中的所有对象的语言
          1.CREATE - to create objects in the database   创建
          2.ALTER - alters the structure of the database   修改
          3.DROP - delete objects from the database   删除
          4.TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
          TRUNCATE TABLE [Table Name]。
      下面是对Truncate语句在MSSQLServer2000中用法和原理的说明:
      Truncate table 表名 速度快,而且效率高,因为:
      TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行。但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系统和事务日志资源少。
      DELETE 语句每次删除一行,并在事务日志中为所删除的每行记录一项。TRUNCATE TABLE 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放。
      TRUNCATE TABLE 删除表中的所有行,但表结构及其列、约束、索引等保持不变。新行标识所用的计数值重置为该列的种子。如果想保留标识计数值,请改用 DELETE。如果要删除表定义及其数据,请使用 DROP TABLE 语句。
      对于由 FOREIGN KEY 约束引用的表,不能使用 TRUNCATE TABLE,而应使用不带 WHERE 子句的 DELETE 语句。由于 TRUNCATE TABLE 不记录在日志中,所以它不能激活触发器。
      TRUNCATE TABLE 不能用于参与了索引视图的表。
           5.COMMENT - add comments to the data dictionary 注释
           6.GRANT - gives user's access privileges to database 授权
           7.REVOKE - withdraw access privileges given with the GRANT command   收回已经授予的权限

    二、DML is Data Manipulation Language statements. Some examples:数据操作语言,SQL中处理数据等操作统称为数据操纵语言
           1.SELECT - retrieve data from the a database           查询
           2.INSERT - insert data into a table                    添加
            3.UPDATE - updates existing data within a table    更新
           4.DELETE - deletes all records from a table, the space for the records remain   删除
           5.CALL - call a PL/SQL or Java subprogram
           6.EXPLAIN PLAN - explain access path to data
           Oracle RDBMS执行每一条SQL语句,都必须经过Oracle优化器的评估。所以,了解优化器是如何选择(搜索)路径以及索引是如何被使用的,对优化SQL语 句有很大的帮助。Explain可以用来迅速方便地查出对于给定SQL语句中的查询数据是如何得到的即搜索路径(我们通常称为Access Path)。从而使我们选择最优的查询方式达到最大的优化效果。
           7.LOCK TABLE - control concurrency 锁,用于控制并发

    三、DCL is Data Control Language statements. Some examples:数据控制语言,用来授予或回收访问数据库的某种特权,并控制数据库操纵事务发生的时间及效果,对数据库实行监视等
           1.COMMIT - save work done 提交
            2.SAVEPOINT - identify a point in a transaction to which you can later roll back 保存点
           3.ROLLBACK - restore database to original since the last COMMIT   回滚
           4.SET TRANSACTION - Change transaction options like what rollback segment to use   设置当前事务的特性,它对后面的事务没有影响.



    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/level_level/archive/2009/06/07/4248685.aspx
Open Toolbar