|
Oracle 应用技巧
1.Oracle中如何实现某一字段自动增加1? 软件环境: 1、Windows NT4.0+ORACLE 8.0.4 2、ORACLE安装路径为:C:\ORANT 实现方法: 建立一个最小为1,最大为999999999的一个序列号会自动循环的序列 create sequence 序列名 increment by 1 start with 1 maxvalue 999999999 cycle; 当向表中插入数据时,SQL语句写法如下: SQL> insert into 表名 values(序列名.nextval,列1值,列2值); 2.如何改变表中列的名字? 软件环境: 1、Windows NT4.0+ORACLE 8.0.4 2、ORACLE安装路径为:C:\ORANT 实现方法: SQL> create table t2(新列名) as select * from t1; --以新名字命名列名 SQL> drop table t1; --删除表1 SQL> rename t2 to t1; --把表2改名为表1 3.如何删除表中的列? 软件环境: 1、Windows NT4.0+ORACLE 8.0.4 2、ORACLE安装路径为:C:\ORANT 实现方法: SQL> create table t2 as select from t1; --以第一个表中的某列建立第二个新表 SQL> drop table t1; --删除表1 SQL> rename t2 to t1; --把表2改名为表1 Oracle 8i及以上版本中,可以使用以下语句 SQL> alter table 表1 drop column 列1; 4.如何查找、删除表中重复的记录 软件环境: 1、Windows NT4.0+ORACLE 8.0.4 2、ORACLE安装路径为:C:\ORANT 问题提出: 1、当我们想要为一个表创建唯一索引时,如果该表有重复的记录,则无法创建成功。 方法原理: 1、Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的, rowid确定了每条记录是在ORACLE中的哪一个数据文件、块、行上。 2、在重复的记录中,可能所有列的内容都相同,但rowid不会相同,所以只要确定出重复记录中 那些具有最大rowid的就可以了,其余全部删除。 3、以下语句用到了3项技巧:rowid、子查询、别名。 实现方法: SQL> create table a ( 2 bm char(4), --编码 3 mc varchar2(20) --名称 4 ) 5 / 表已建立. SQL> insert into a values('1111','1111'); SQL> insert into a values('1112','1111'); SQL> insert into a values('1113','1111'); SQL> insert into a values('1114','1111'); SQL> insert into a select * from a; 插入4个记录. SQL> commit; 完全提交. SQL> select rowid,bm,mc from a; ROWID BM MC ------------------ ---- ------- 000000D5.0000.0002 1111 1111 000000D5.0001.0002 1112 1111 000000D5.0002.0002 1113 1111 000000D5.0003.0002 1114 1111 000000D5.0004.0002 1111 1111 000000D5.0005.0002 1112 1111 000000D5.0006.0002 1113 1111 000000D5.0007.0002 1114 1111 查询到8记录. 查出重复记录 SQL> select rowid,bm,mc from a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc); ROWID BM MC ------------------ ---- -------------------- 000000D5.0000.0002 1111 1111 000000D5.0001.0002 1112 1111 000000D5.0002.0002 1113 1111 000000D5.0003.0002 1114 1111 删除重复记录 SQL> delete from a a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc); 删除4个记录. SQL> select rowid,bm,mc from a; ROWID BM MC ------------------ ---- -------------------- 000000D5.0004.0002 1111 1111 000000D5.0005.0002 1112 1111 000000D5.0006.0002 1113 1111 000000D5.0007.0002 1114 1111 5.查找a表中有,而在b表中没有的记录 软件环境: 1、Windows NT4.0+ORACLE 8.0.4 2、ORACLE安装路径为:C:\ORANT 实现方法: SQL> create table a ( 2 bm char(4), --编码 3 mc varchar2(20) --名称 4 ) 5 / 表已建立. SQL> insert into a values('1111','1111'); SQL> insert into a values('1112','1111'); SQL> insert into a values('1113','1111'); SQL> insert into a values('1114','1111'); SQL> insert into a values('1115','1111'); SQL> create table b as select * from a where 1=2; 表已建立. SQL> insert into b values('1111','1111'); SQL> insert into b values('1112','1111'); SQL> insert into b values('1113','1111'); SQL> insert into b values('1114','1111'); SQL> commit; 完全提交. SQL> select * from a; BM MC ---- -------------------- 1111 1111 1112 1111 1113 1111 1114 1111 1115 1111 SQL> select * from b; BM MC ---- -------------------- 1111 1111 1112 1111 1113 1111 1114 1111 SQL> select bm from a where not exists (select bm from b where a.bm=b.bm); BM MC ---- -------------------- 1115 1111 6.查看本用户下的各种对象的SQL脚本 软件环境: 1、Windows NT4.0+ORACLE 8.0.4 2、ORACLE安装路径为:C:\ORANT SQL语句: 表: select * from cat; select * from tab; select table_name from user_tables; 视图: select text from user_views where view_name=upper('&view_name'); 索引: select index_name,table_owner,table_name,tablespace_name,status from user_indexes order by table_name; 触发器: select trigger_name,trigger_type,table_owner,table_name,status from user_triggers; 快照: select owner,name,master,table_name,last_refresh,next from user_snapshots order by owner,next; 同义词: select * from syn; 序列: select * from seq; 数据库链路: select * from user_db_links; 约束限制: select TABLE_NAME,CONSTRAINT_NAME,SEARCH_CONDITION,STATUS from user_constraints; 本用户读取其他用户对象的权限: select * from user_tab_privs; 本用户所拥有的系统权限: select * from user_sys_privs; 用户: select * from all_users order by user_id; 表空间剩余自由空间情况: select tablespace_name,sum(bytes) 总字节数,max(bytes),count(*) from dba_free_space group by tablespace_name; 数据字典: select table_name from dict order by table_name; 锁及资源信息: select * from v$lock;不包括DDL锁 数据库字符集: select name,value$ from props$ where name='NLS_CHARACTERSET'; inin.ora参数: select name,value from v$parameter order by name; SQL共享池: select sql_text from v$sqlarea; 数据库: select * from v$database 控制文件: select * from V$controlfile; 重做日志文件信息: select * from V$logfile; 来自控制文件中的日志文件信息: select * from V$log; 来自控制文件中的数据文件信息: select * from V$datafile; NLS参数当前值: select * from V$nls_parameters; ORACLE版本信息: select * from v$version; 描述后台进程: select * from v$bgprocess; 查看版本信息: select * from product_component_version; 17.如何在PL/SQL中读写文件? 软件环境: 1、服务器端:Windows NT4.0+ORACLE 8.0.4 2、ORACLE安装路径为:C:\ORANT 实现方法: PL/SQL 3.3以上的版本中,UTL_FILE包允许用户通过PL/SQL读写操作系统文件。如下: declare file_handle UTL_FILE.FILE_TYPE; begin
来源:upschool.com.cn 作者: 关键字:Oracle,应用技巧 发表日期:2007-1-6 0:45:06 网页显示有限 阅读全文请下载本文完整版WORD文档
上一篇:Oracle约束管理脚本 下一篇:用外联接提高表连接的查询速度
2008-12-2 4:36:34
|