常用SQL语句收集
2010-05-20 22:40
393 查看
1.删除表
select 'drop table ' || table_name || ' purge;'
from user_tables
where length(table_name) <= 4;
删除带约束的表
Drop table 表名 cascade constraints;
2.查询view&同义词
--说明:可以根据视图的text_length值设定set long 的大小
set long 2000;
select * from user_views where view_name='MY_VIEW_NAME';
select * from all_views where view_name='MY_VIEW_NAME';
select * from user_synonyms;
3.临时表
create global temporary table xay_temp as select * from xay;
on commit preserve rows/on commit delete rows
在Oracle中,可以创建以下两种临时表:
a 会话特有的临时表:
create global temporary table () on commit preserve rows;
会话指定,当中断会话时ORACLE将截断表
b 事务特有的临时表:
create global temporary table () on commit delete rows;
事务指定,每次提交后ORACLE将截断表(删除全部行)
c 说明
临时表只在当前连接内有效
临时表不建立索引,所以如果数据量比较大或进行多次查询时,不推荐使用
数据处理比较复杂的时候时表快,反之视图快点
4.填加注释
comment on table 表名 is '注释';
comment on column 表名.列名 is '注释';
5. ROWNUM
按设定排序的行的序号
SELECT * FROM emp WHERE ROWNUM < 10 ;
6. ROWID
返回行的物理地址
SELECT ROWID, ename FROM emp WHERE deptno = 20 ;
7. 复制表
create table table_name [logging|nologging] as subquery
8.删除字段
alter table table_name drop column column_name [Cascade Constrains];
9.移动表空间(move tablespace)
ALTER TABLE TABLE_NAME MOVE TABLESPACE_NAME;
ALTER INDEX INDEX_NAME REBUILD TABLESPACE TABLESPACE_NAME;
10.表改名:
rename xsb1 to xsb2;
11.改表的列名
ALTER TABLE UserName.TabName
RENAME COLUMN SourceColumn TO DestColumn
12.删除列
ALTER TABLE UserName.TabName
SET UNUSED (ColumnName) CASCADE CONSTRAINTS
ALTER TABLE UserName.TabName
DROP (ColumnName) CASCADE CONSTRAINTS
13.失效OBJECT
COLUMN object_name FORMAT A30;
SELECT owner, object_type, object_name, status
FROM dba_objects
WHERE status = 'INVALID'
ORDER BY owner, object_type, object_name;
重新编译:
SELECT 'ALTER '||OBJECT_TYPE||' '||OWNER||'.'||OBJECT_NAME||' COMPILE;',OWNER
FROM ALL_OBJECTS
WHERE STATUS='INVALID'
ORDER BY OWNER
14.如何查看某个表给哪些用户授权
SELECT * FROM USER_TAB_PRIVS WHERE TABLE_NAME = 'table_name';
15.truncate表分区
alter table test truncate partition p20090531;
16.查表分区
select partition_name from DBA_TAB_PARTITIONS
where table_name ='table_name'
and table_owner='table_owner'
order by partition_name;
select 'drop table ' || table_name || ' purge;'
from user_tables
where length(table_name) <= 4;
删除带约束的表
Drop table 表名 cascade constraints;
2.查询view&同义词
--说明:可以根据视图的text_length值设定set long 的大小
set long 2000;
select * from user_views where view_name='MY_VIEW_NAME';
select * from all_views where view_name='MY_VIEW_NAME';
select * from user_synonyms;
3.临时表
create global temporary table xay_temp as select * from xay;
on commit preserve rows/on commit delete rows
在Oracle中,可以创建以下两种临时表:
a 会话特有的临时表:
create global temporary table () on commit preserve rows;
会话指定,当中断会话时ORACLE将截断表
b 事务特有的临时表:
create global temporary table () on commit delete rows;
事务指定,每次提交后ORACLE将截断表(删除全部行)
c 说明
临时表只在当前连接内有效
临时表不建立索引,所以如果数据量比较大或进行多次查询时,不推荐使用
数据处理比较复杂的时候时表快,反之视图快点
4.填加注释
comment on table 表名 is '注释';
comment on column 表名.列名 is '注释';
5. ROWNUM
按设定排序的行的序号
SELECT * FROM emp WHERE ROWNUM < 10 ;
6. ROWID
返回行的物理地址
SELECT ROWID, ename FROM emp WHERE deptno = 20 ;
7. 复制表
create table table_name [logging|nologging] as subquery
8.删除字段
alter table table_name drop column column_name [Cascade Constrains];
9.移动表空间(move tablespace)
ALTER TABLE TABLE_NAME MOVE TABLESPACE_NAME;
ALTER INDEX INDEX_NAME REBUILD TABLESPACE TABLESPACE_NAME;
10.表改名:
rename xsb1 to xsb2;
11.改表的列名
ALTER TABLE UserName.TabName
RENAME COLUMN SourceColumn TO DestColumn
12.删除列
ALTER TABLE UserName.TabName
SET UNUSED (ColumnName) CASCADE CONSTRAINTS
ALTER TABLE UserName.TabName
DROP (ColumnName) CASCADE CONSTRAINTS
13.失效OBJECT
COLUMN object_name FORMAT A30;
SELECT owner, object_type, object_name, status
FROM dba_objects
WHERE status = 'INVALID'
ORDER BY owner, object_type, object_name;
重新编译:
SELECT 'ALTER '||OBJECT_TYPE||' '||OWNER||'.'||OBJECT_NAME||' COMPILE;',OWNER
FROM ALL_OBJECTS
WHERE STATUS='INVALID'
ORDER BY OWNER
14.如何查看某个表给哪些用户授权
SELECT * FROM USER_TAB_PRIVS WHERE TABLE_NAME = 'table_name';
15.truncate表分区
alter table test truncate partition p20090531;
16.查表分区
select partition_name from DBA_TAB_PARTITIONS
where table_name ='table_name'
and table_owner='table_owner'
order by partition_name;
相关文章推荐