zoukankan      html  css  js  c++  java
  • Oracle 数据库命令个人总结

    一、日志管理

    1.强制日志切换(forcing log switches)
    alter system switch logfile;
    
    2.强制执行检查点(forcing checkpoints)
    alter system checkpoint;
    
    3.增加一个重做日志组 (adding online redo log groups)
    alter fatabases add logfile [ group 4 ] ('/disk3/log4a.rdo','/disk4/log4b.rdo') size 1M;
    
    4.增加一个重做日志文件( adding online redo log memebers )
    alter databases add logfile member
    '/disk3/log1b.rdo' to group 1,
    '/disk4/log2b.rdo' to group 2;
    
    5.改变重做日志文件名 (changes the name of the online redo logfile)
    alter databases rename file 
    'c:/oracle/oradata/oradb/redo01.log'
    to 'c:/oracle/oracdata/redo01.log';
    
    6.删除重做日志 (drio online redo log groups)
    alter database drop logfile groupp 3;
    
    7.删除重做日志组(drop online redo log members)
    alter database drop logfile member 'c:/oracle/readata/red01.log';
    
    8.清空重做日志(clearing online redo log files)
    alter database clear [unarchived] logfile 'c:/oracle/log2a.rdo';
    
    9.使用logminer分析重做日志文件(using logminer analyzing redo logfiles)
    a.  in the init.ora specify utl_file_dir = ' '
    b.  execute dbms_logmnr_d.build('oradb.ora','c:oracleoradblog');
    c.  execute dbms_logmnr_add_logfile('c:oracleoradataoradb
    edo01.log',dbms_logmnr.new);
    d.  execute dbms_logmnr.add_logfile('c:oracleoradataoradb
    edo02.log',dbms_logmnr.addfile);
    e.  execute dbms_logmnr.start_logmnr(dictfilename=>'c:oracleoradblogoradb.ora');
    f.  select * from v$logmnr_contents(v$logmnr_dictionary,v$logmnr_parameters v$logmnr_logs);
    g.  execute dbms_logmnr.end_logmnr;

    二、表空间管理

    1.创建表空间(create tablespaces)
    create tablespace table_name 
    datafile 'c:oracleoradatafile1.dbf'
    size 100M
    minimum extent 550k [logging/nologging]
    default storage (initial 500k next 500k maxextents 500 pctinccease 0)
    [online/offline]
    [permanent/temporary]
    [extent_management_clause]
    
    2.创建本地管理的表空间( locally managed tabalespace )
    create tablespace user_data 
    datafile 'c:oracleoradatauser_data001.dbf'
    size 500M
    extent management 
    local uniform size 10M;
    
    3. 创建临时表空间(temporary tablespace)
    create temporary tablespace temp 
    tempfile 'c:oracleoradata	emppp01.dbf'
    size 500M
    extent management
    local uniform size 10M;
    
    4.改变表空间的存储参数(change the storage setting)
    alter tablespace app_data minimum extent 2M;
    or
    alter tablespace app_date 
    default storage( initial 2M next 2M maxextents 999);
    
    5.使表空间离线或连线(taking tablespace offline or online)
    alter tablespace app_data offline;
    or
    alter tablespace app_data online;
    
    6.设置表空间为只读、可写模式(read_only tablespace)
    alter tablespace app_data read only | write;
    
    7.删除表空间(droping tablespace)
    drop tablespace app_data including contents;
    
    8.允许数据文件自动扩张(enableing automatic extension of data files)
    alter tablespace app_data add 
    datafile 'c:oracleoradataapp_data01.dbf'
    size 200M
    autoextend on next 10M
    maxsize 500M;
    
    9.手动改变数据文件大小(change the size fo data files manually)
    alter database datafile 'c:oracleoradataapp_data.dbf'
    resize 200M;
    
    10.改变表空间中的数据文件(mocing data files:alter tablespace)
    alter tablespace app_data 
    rename datafile 'c:oracleoradataapp_data.dbf'
     to 'c:oracleapp_data.dbf';
    
    11.修改数据库中的数据文件(moving data files:alter databasealter database rename file 'c:oracleoradataapp_data.dbf'
    to 'c:oracleapp_data.dbf'; 

    三、表

    1.创建表
    create table table_name (
        column datatype,
        [column datatype]
        .....  
    );
    tablespace tablespace_name 
    [ ppctfree integer ] 
    [ pctused integer ]
    [ initrans integer ]
    [ maxtrans integer ]
    storage( initial 200k next 200k ppctincrease 0 maxextents 50) [ logging | nologging ]  [ cache | nocaahe ]
    
    2.复制一个已存在的表(copy an existing tablecreate table table_name 
    [ logging | nologging ] 
    as subquery
    
    3.创建一个临时表(create temporary tablecreate global temporary table xay_temp as select * from xay;
    on commint preserve rows/on commit delete rows
    
    4.pctfree 和 pctused 参数计算公式
    pctfree = (average row size - initial row size)*100/average row size
    pctused = 100-pctfree-(average row size * 100/available data space)
    
        pctfree : 指定表内每个数据块中空间的百分比。pctfree的值必须介于0和99之间。如果为零,表示可以通过插入新行来填充整个块。
            缺省值为10.此值表示每个块中保留着10
    %的空间,用于更新现有的行以及插入新行,每个块最多可以填充到90% pctused : 指定为表内每个数据块的已用空间的最小百分比。如果一个块的已用空间低于pctused,则可在该块中插入行。pctused的值介于0和99之间的整数,缺省值为40. 结合pctfree 和 pctused 就可以确定将新行插入到现有数据块中,还是插入到新块中。这两个参数值的和必须小于或等于100.使用这两个参数可以更有效地利用表内的空间。 设置 pctfree 和pctused pctfree 值越高,可为数据库块内的更新提供的空间就越大。如果表存在下面两种情况,则应设置一个更高的值:?某些列最初为null,后来更新为某个值?
          某些列由于更新,大小可能增加pctfree的值越高,块密度就越低,即每个块容纳的行数就越少。上面的公式确保块中有足够的空间供行增长使用。 pctused 以确保只有在具备足够空间来容纳一个平均大小的行时才将块返回到空闲列表中。
          如果空闲列表中的某个块没有足够的空间来插入一行,oracle服务器将查找空闲列表中的下个块、直到找到具备足够空间的块或者到达列表的末尾,这种线性扫描才会结束。
          使用给定的公式可以增加找到具有所有需空闲空间的块的概率,从而缩短扫描空闲列表时间。 注:可以使用 analyze
    table 命令估算平均行大小的值。 注:oracle9i “自动段空间管理” 功能可替代 pctused、freelists 和 freelist groups. 5.改变存储和块利用率参数(change storage and block utilization parameter) alter table table_name pctfree=30 pctused=50 storage(next 500k minextents 2 maxextents 100); 6.手工分配区间(extents)(manually allxoating extents) alter table table_name allocate extent (size 500k datafile 'c:/oracle/data.dbf'); 7.改变表的所属表空间(move tablespace) alter table employee move tablespace users; 8.释放表中未用空间(deallocate of unused spacealter table table_name deallocate unused [ keep integer ] 9.截断表(truncate)(truncate a tabletruncate table table_name; 截断一个表将删除表中所有行,从而释放已使用的空间。 对应的索引将被截断 注:truncate table 不是DML语句,是DDL语句。另外truncate与delete的区别是吗,delete不释放空间,truncate释放空间。 10.删除表(drop a tabledrop table table_name [cascade constraints]; 11.删除列(drop a cloumn) alter table table_name drop column commentd cascade constraints checkppoint 1000; or alter table table_name drop columns continue; 12.表示某一列为未使用(unused)(mark a column sa unused) alter table table_name set unused column comments xascade constraints; or alter table table_name dropp unused columns checkpoint 1000; or alter table orders drop columns continue checkpoint 1000 data_dictionary : dba_unused_col_tabs; 除将列从表中删除以外,还可以先将列标记为 “未使用”,以后再删除。因为没有删除数据,所以此操作不回收磁盘空间, 因而具有速度比较快的优点。被标为 “未使用” 的列可在以后系统活动较少时从表中删除。 未使用的列就像不属于表一样。查询时看不到未使用列中的数据。此外,在执行 DESCRIBE 命令时, 也不会显示这些列的名称和数据类型。用户可以添加与未使用的列同名的新列。 如果想删除同一表中的两列,则可先将列设置为 “未使用” 然后再删除。在删除两列时, 表中的所有行都会更新两次;但如果将这些列设置为 “未使用” 然后再删除,则所有的行仅更新一次。

    四、索引

    1.创建一个基于函数的索引(creating function-based indexes)
    create index summit.item_quantity on summit.item(quantity-quantity_shipped);
    
    基于函数的索引(function-based indexes):如果在表中要建立索引的一列或多列上使用了函数或表达式,则创建的是基于函数的索引。
    基于函数的索引预先计算函数或表达式的值,病将结果存储在索引中。可以将基于函数的索引创建为B树或位图索引。
    2.创建一个B树索引(create a B-tree index) creae [ unique ] index index_name on table_name (column,.. asc/desc) tablespace tablespace_name [ pctfree integer ] [ initrans integer ] [ maxtrans integer ] [ logging | nologging ] [ nosort ] storage(initial 200k next 200k pctincrease 0 maxextents 50); B树:平衡二叉树,oracle中用的最多的索引模式,使用与取值唯一性高的情况。只有两层,非叶级、叶级(指针所在级) 3.索引中pctfree参数计算公式 pctfree(index)=(maximum number of rows-initial number of rows)*100/maximum number of rows 4.创建一个反向键索引(creating reverse key indexes) create unique index xay_id on xay(a) reverse pctfree 30 storage (initial 200k next 200k pctincrease 0 maxextents 50) tablespace indx; 5.创建位图索引(creating reverse key indexes) create unique index say_id on xay(a) reverse pctfree 30 storage (initial 200k next 200k pctincrease 0 maxextents 50) tablespace indx; 在下列情况中,位图索引比 B 树索引更有利: ? 当表包含数百万行且键列的基数很低(即,该列中重复的值很多)时。例如,对于包含护照记录的表的性别列和婚姻状况列而言,位图索引比 B 树索引更适合 ? 当查询经常使用涉及 OR 运算符的多个 WHERE 条件组合时 ? 当键列上存在只读或很少的更新操作时 (位图索引适用于取值的唯一性很低的情况) 6.改变索引的存储参数(change storage parameter of indexalter index xay_id storage ( next 400k maxextents 100 ) 7.为索引分配空间(allocating index spacealter index xay_id allocate extent (size 200k datafile 'c:/oracle/index.dbf') 8. alter index xay_id deallocate unused; 手动分配索引空间: 在表上进行频繁的插入操作前,可能需要向索引添加区。添加区可防止索引动态扩展并导致性能降低。 (其中指定的数据文件一定是索引所在表空间的数据文件)

    五、约束(constraints)

    1.将约束定义为立即(immediate)或延迟(deferred)(define constraints as immediate or deferred)
    alter session set constraint[s]=immediate/deferred/default;
    set constraint[s] constraint_name/all immediate/deferred;
    
    set constraints 语句用于将特定事务的约束设置为 deferred 或 immediate.可以使用此语句设置约束名称列表或约束的模式。
    set constraints模式将一直持续到事务处理完成或者另一个set constraints语句重置模式
    set constraints语句还包含将约束设置为immediate或deferred的子句set constraints.此命令缺省为设置所有(all) 可延迟的约束(不能指定约束名称列表)。
    alter session set constraints 语句仅适用于当前会话 2.删除表或表空间时连带删除其上的外检(约束) drop table table_name cascade constraints or drop tablespace tablespace_name including contents cascade constraints 在删除父表之前,必须先删除外键。可以使用以下一条语句同事执行这两个操作: drop table table_name cascade constraints 在未删除或禁用外键之前无法(truncated)父表。 在删除包含附表的表空间之前,必须先删除外键。可使用下列命令完成该操作: drop tablespace tablespace_name including contents cascade constraints 如果从父表中删除行时没有使用 delete cascade 选项,oracle 服务器必须确保子表中的行不包含相应的外键。同样,仅当子行中不包含旧键值时,才允许更新父键。 如果字表的外键上没有索引,则oracle服务器索引子表并禁止更改以确保引用完整性。 如果表上有索引,则通过锁定索引项并避免子表上有更具限制性的锁来维护引用完整性。 如果必须从不同的事务处理同时更新两个表,则在外键列上创建索引。 3.在创建表时定义约束(define constraints while create a tablecreate table xay(id number(7) constraint xay_id pprimary key deferrable using index storage(initial 100k next 100k ) tablespace indx); primary key / unique / references table(column) / check 4.启用当前禁用的约束(enable constraints) alter table xay enable novalidate constraint xay_id; 启用novalidate:对于当前已有索引的 primary keyunique 约束,启用novalidate 约束比启用validate约束要快的多,这是应为,如果使用该选项启用约束,则不要求锁定表。
    这种方法适合表上有许多DML活动的情况,如在oltp环境中。 但是,如果需要创建索引,使用这中启用约束的方法并不能比enable validate带来更多的好处,因为oracle服务器在建立索引时锁定表。
    5.启用约束(enable constraints) alter table xay enable validate constraint xay_id;

    六、加载(load)数据

    1.使用insert语句从另一张表中“直接加载”数据到新表(loading data using direct_load insertinsert //*+append*/into emp nologging
    
    select * from emp_old;
    
    2.使用sql*loader加载数据(using sql*loader)
    sqlldr scott/tiger 
    control = ulcase6.ctl 
    log = ulcase6.log direct = true

    七、重整数据(reorganizing data)

    1.使用export导出数据(using export)
    $exp scott/tiger tables(dept,emp) file=c:emp.dmp log=exp.log compress=n direct=y
    
    2.使用import导入数据(using impport)
    alter tablespace sales_ts read only;
    $expp sys/..  file=xay.dmp transpport_tablespace=y tablespace=sales_ts triggers=n constraints=n
    $copy datafile
    $imp sys/.. file=xay.dmp transpport_tablespppace=y datafiles=(/disk/sles01.dbf,/disk2/sles02.dbf)
    
    alter tablespace sales_ts read write;
    
    4.checking transport set
    DNMS_tts.transport_set_check(ts_list =>'sales_ts' ..,incl_constraints=>true);
    
    在表transport_set_violations中查看
    dbms_tts.isselfcontained 为true 是,表示自包含

    八、管理口令及相关资源(managing password security and resourecs)

    1.修改用户账号、解锁及口令(controlling account lock and password)
    alter user jumcky identified by oracle account unlock;
    
    2.user_privided password function
    function_name(userid in varchar2(30),password in varchar2(30),old_password in varchar2(30)) return boolean
    
    3.创建概要文件:设置口令参数(create a profile : password setting)
    create profile grace_5 limit failed_login_attempts 3
    password_lock_time unlimited password_life_time 30
    password_reuse_time 30 password_verify_function verify_function
    password_grace_time 5;
    
    4.修改概要文件(altering a profile)
    alter profile default limit
    failed_lofin_attemppts 3
    password_life_time 60;
    
    5.删除概要文件(drop a profile)
    drop profile grace_5 [cascade];
    
    6.创建概要文件:设置资源限制(create a profile : resource limit)
    create profile developer_prof limit sessions_per_user 2
    cpu_per_session 10000 idle_time 60 connect_time 480;
    
    7.view =>resource_cost : alter resource cost dba_users,dba_profiles
    
    8.允许资源限制(enable resource limits)
    alter system set resource_limit = true;

    九、管理用户(managing users)

    1.创建用户(数据库认真方式)(create a user : database authentication)
    create user juncky 
    identified by oracle
    default tablespace users
    temporary tablespace
    temp quota 10m/unlimited
    on data password expire [ account lock | unock ] [ profile profilename | default ]; 2.修改用户的表空间限额(change user quota on tablespace) alter user juncky quota 0 on users; 3.删除用户(drop a userdrop user juncky [ cascade ]; 4.监控用户的视图(monitor userview:dba_users, dba_ts_quotas

    十、管理权限(managing privileges)

    1.系统权限(managing privilegesview =>system_privilege_map ,dba_sys_privs,session_privs
    
    2.授权系统权限(grant system privilege)
    grant create session,create table to managers;
    or
    grant create sessionto scott with admin option;
    with admin option can gran grant or revoke privilege from any user or role;
    
    3.sysdba和sysoper的权限(sysdba and sysoper privileges:)
    sysoper:startup,shtdown,alter database open | mount,alter database backup controlfile,alter tablespace
    sysdba:sysoper privileges with admin option,create database,recover database until
    
    4.口令文件成员视图(password file members:)
    view:=v$pwfile_users
    
    5.07_dictionary_accessibility = true
    restriction access to view or tables in other schema
    
    6.撤销系统权限(revoke system privilege)
    revoke create table from karen;
    or
    revoke create session from scott;
    
    7.授权对象权限(grant object privilege)
    grant execute on dbms_pipe to public;
    grant update(first_name,salary) on employee to karen with grant option;
    
    8.显示对象权限的视图(display object privilege)
    view =>dba_tab_privs,dba_col_privs
    
    9.撤销用户的对象权限(revoke object privilege)
    revoke execute on dbms_pipe from scott [ cascade constraints ];
    
    10.审计记录视图(audit record view)
    sys.aud$
    
    11.保护审计线索(protecting the audit trail)
    audit delete on sys.aud$ by access;
    
    保护审计线索:应保护审计线索,以防添加,修改或删除审计信息。
    发布以下,命令:
    audit delete on sys.aud$ by access;
    
    可防止审计线索未经授权即被删除;只有DBA才拥有delete_catalog_role角色
    
    12.语句审计(statement auditing)
    audit user;
    
    13.权限审计(privilege auditing)
    audit select any table by summit by access;
    
    权限审计:该种审计执行操作应具有的相应系统权限进行选择性审计,如audit create any trigger. 可以设置权限审计对数据库中的所选用户或每个用户进行审计。
    
    14.方案对象审计(schema object auditing)
    sudit lock on summit.employee by access whenever successful;
    
    方案对象审计:该种审计对待特定方案对象上的特定语句进行选择性审计,如 audit select on  hr.employees.方案对象审计始终适用于所有数据库用户。
    
    15.审计选项视图(view audit optionview =>all_def_audit_opts,dba_stmt_audit_opts,dba_priv_audit_opts,dba_obj_audit_opts
    
    16.设计结果视图(view audit result)
    view=>dba_audit_trail,dba_audit_exists,dba_audit_object,dba_audit_session,dba_audit_statement

    十一、管理角色

    1.创建角色(create roles)
    create role sales_clerk;
    or
    create role hr_clerk identified by bonus;
    or
    create role hr_manager identified externally;
    
    2.修改角色(modify role)
    alter role sales_clerk identified by commission;
    or
    alter role hr_clerk identified externally;
    or
    alter role hr_manager not identified;
    
    3.分配角色(assigning roles)
    grant sales_clerk to scott;
    oe
    grant hr_clerk to hr_manager;
    or
    grant hr_manager to scott with admin option;
    
    4.建立缺省角色(establish default role)
    alter user scott default role hr_clerk,sales_clerk;
    or
    alter user scott default role all;
    or
    alter user scott default fole all except hr_clerk;
    or
    alter user scott default role none;
    
    5.允许和禁止角色(enable and disable roles)
    set role hr_clerk;
    or
    set role sales_clerk identified by commission;
    or
    set role all except sales_clerk;
    or
    set role none;
    
    6.撤销用户的角色(remove role from userrevoke sales_clerk from scott;
    or
    revoke hr_manager from public ;
    
    7.删除角色(remove role)
    drop role hr_manager;
    
    8.显示角色信息的视图(display role information)
    view:=>dba_roles.dba_role_privs,role_role_privs,dba_sys_privs,role_sys_privs,role_tab_privs,session_roles

    十二、备份和恢复(backup and recovery)

    1.备份恢复用相关视图
    v$sga,v$instance,v$process,v$bgprocess,v$database,v$datafile,v$sgastat
    
    2.为rman设置初始化参数
    rman need set dbwr_io_slaves or backup_tape_io_slaves and large_pool_size
    
    3.监控并行回滚的视图(monitoring parallel rollback)
    v$fast_start_servers,v$fast_start_transactions
    
    4.执行一个冷备份(perform a closed database baskup ( noarchivelog ))
    shutdown immediate
    cp files /backup/
    startup
    
    5.改变数据库文件的位置(restore to a different location)
    connect system/manager as sysdba
    startupp mount
    alter database rename file '/disk1/../user.dbf' to '/disk/../user.dbf';
    
    6.恢复命令(recover)语法(recover syntax)
    --recover a mounted database
    recover database;
    recover datafile '/disk1/data/df2.dbf';
    alter database recover database;
    --recover an opened database
    recover tablespace user_data;
    recover datafile 2;
    alter database recover datafile 2;
    
    7.设置自动应用重做日志文件(how to apply redo log files automatically)
    set autorecovery on
    recover automatic datafile 4;
    
    8.完全恢复(complete recovery)
    --method 1 ( mounted databe )
    copy c:backupuser.dbf  c:oradatauser.dbf
    startup mount
    recover datafile 'c:oradatauser.dbf';
    alter database oppen;
    
    --mothod 2 ( opened database,initially opened, not system or rollback datafile)
    copy c:backupuser.dbf  c:oradatauser.dbf ( alter tablespace offline)
    recover datafile 'c:oradatauser.dbf' or
    recover tablespace user_data;
    alter database datafile 'c:oradatauser.dbf' online or 
    alter tablespace user_data online;
    
    --method 3 ( opened database,initially closed not system or rollback datafile )
    startup mount
    alter database datafile 'c:oradatauser.dbf' offline;
    alter database open
    copy c:backupuser.dbf  d:oradatauser.dbf
    alter database rename file 'c:oradatauser.dbf' to 'd:oradatauser.dbf'
    recover datafile 'e:oradatauser.dbf' or recover tablespace user_data;
    alter tablespace user_data online;
    
    --method 4 ( loss of data file with no backup and have all archive log )
    alter tablespace user_data offline immediate;
    alter database create datafile 'd:oradatauser.dbf' as  'c:oradatauser.dbf'
    recover tablespace user_data;
    alter tablespace user_data online
    
    9.对一个打开的数据库进行备份(perform an open database backupalter tablespace user_data begin backup;
    copy files /backup/
    alter database datafile '/c:/../data.dbf' end backup;
    alter system switch logfile;
    
    10.备份一个控制文件(backup a control filealter database backup controlfile to 'controll.bkp';
    alter database backup xontrolfile to trace;
    
    11.非归档模式下的恢复(recovery (noarchivelog mode))
    shutdown abort
    cp files
    startup
    
    12.备份模式下的文件恢复(recovery of file in backup mode)
    alter database datafile 2 end backup;
    
    13.清空重做日志文件(rclearing redo log filealter database clear unarchived logfile group 1;
    alter database clear unarchived logfile group 1 unrecoverable datafile;
    
    14.重做日志的恢复(删除与重建)(redo log recovery)
    alter database add logfile group 3 'c:oradata
    edo03.log' size 1000k;
    alter database drop logfile group 1;
    alter database open;
    or >cpp c:oradata
    edo02.log ,c:oradata
    edo01.log
    alter database clear logfile ‘c:oradatalog01.log’; 
  • 相关阅读:
    [导入]C#播放rm文件[转贴]
    新工具软件发布!名称:剪切板记录器
    黑發4/14
    sql20002 4/19
    頁面按百分比設定失調3/27
    廣告控件:AdRotator 3/23
    19992020小農曆JS 3/31
    ASP.NET1.1編譯錯誤代碼:1073741502 4/10
    SiteMapPath控件 3/24
    MYSQL時間問題4/16
  • 原文地址:https://www.cnblogs.com/ChineseIntelligentLanguage/p/6420896.html
Copyright © 2011-2022 走看看