当前位置:文档之家› oracle常用sql查询语句部分

oracle常用sql查询语句部分

oracle常用sql查询语句部分
oracle常用sql查询语句部分

Oracle查询语句

select*from scott.emp ;

1.--dense_rank()分析函数(查找每个部门工资最高前三名员工信息)

select*from(select deptno,ename,sal,dense_rank()

over(partition by deptno order by sal desc)a from scott.emp) where a<=3order by deptno asc,sal desc;

结果:

--rank()分析函数(运行结果与上语句相同)

select*from(select deptno,ename,sal,rank()over(partition by deptno order by sal desc) a from scott.emp )where a<=3 order by deptno asc,sal desc;

结果:

--row_number()分析函数(运行结果与上相同)

select*from(select deptno,ename,sal,row_number()

over(partition by deptno order by sal desc)a from scott.emp) where a<=3order by deptno asc,sal desc;

--rows unbounded preceding 分析函数(显示各部门的积累工资总和)

select deptno,sal,sum(sal)over(order by deptno asc rows unbounded preceding)积累工资总和from scott.emp ;

结果:

--rows 整数值preceding(显示每最后4条记录的汇总值)

select deptno,sal,sum(sal)over(order by deptno rows3 preceding)每4汇总值from scott.emp ;

结果:

--rows between 1 preceding and 1 following(统计3条记录的汇总值【当前记录居中】)

select deptno,ename,sal,sum(sal)over(order by deptno rows between1preceding and1following)汇总值from scott.emp ; 结果:

--ratio_to_report(显示员工工资及占该部门总工资的比例)

select deptno,sal,ratio_to_report(sal)over(partition by deptno)比例from scott.emp ;

结果:

--查看所有用户

select*from dba_users ;

select count(*)from dba_users ;

select*from all_users ;

select*from user_users ;

select*from dba_roles ;

--查看用户系统权限

select*from dba_sys_privs ;

select*from user_users ;

--查看用户对象或角色权限

select*from dba_tab_privs ;

select*from all_tab_privs ;

select*from user_tab_privs ;

--查看用户或角色所拥有的角色

select*from dba_role_privs ;

select*from user_role_privs ;

-- rownum:查询10至12信息

select*from scott.emp a where rownum<=3and a.empno not in(select b.empno from scott.emp b where rownum<=9);

结果:

--not exists;查询emp表在dept表中没有的数据

select*from scott.emp a where not exists(select*from scott.dept b where a.empno=b.deptno);

结果:

--rowid;查询重复数据信息

select*from scott.emp a where a.rowid>(select min(x.rowid) from scott.emp x where x.empno=a.empno);

--根据rowid来分页(一万条数据,查询10000至9980时间大概在0.03秒左右)

select*from scott.emp where rowid in(select rid from(select rownum rn,rid from(select rowid rid,empno from scott.emp order by empno desc)where rownum<10)where rn>=1)order by empno desc;

结果:

--根据分析函数分页(一万条数据,查询10000至9980时间大概在1.01秒左右)

select*from(select a.*,row_number()over(order by empno desc) rk from scott.emp a )where rk<10and rk>=1;

结果:

--rownum分页(一万条数据,查询10000至9980时间大概在0.01秒左右)

select*from(select t.*,rownum rn from(select*from scott.emp order by empno desc)t where rownum<10)where rn>=1;

select*from(select a.*,rownum rn from(select*from scott.emp) a where rownum<=10)where rn>=5;

--left outer join:左连接

select a.*,b.*from scott.emp a left outer join scott.dept b on a.deptno=b.deptno ;

--right outer join:右连接

select a.*,b.*from scott.emp a right outer join scott.dept b on a.deptno=b.deptno ;

--inner join

select a.*,b.*from scott.emp a inner join scott.dept b on a.deptno=b.deptno ;

--full join

select a.*,b.*from scott.emp a full join scott.dept b on a.deptno=b.deptno ;

select a.*,b.*from scott.emp a,scott.dept b where

a.deptno(+)=

b.deptno ;

select distinct ename,sal from scott.emp a group by sal having;

select*from scott.dept ;

select*from scott.emp ;

--case when then end (交叉报表)

select ename,sal,case deptno when10then'会计部'when20 then'研究部'when30then'销售部'else'其他部门'end部门from scott.emp ;

结果:

select ename,sal,case when sal>0and sal<1500then'一级工资'when sal>=1500and sal<3000then'二级工资'when

sal>=3000and sal<4500then'三级工资'else'四级工资'end

工资等级from scott.emp order by sal desc;

结果:

--交叉报表是使用分组函数与case结构一起实现

select姓名,sum(case课程when'数学'then分数end)数学,sum(case课程when'历史'then分数end)历史from学生group by姓名;

--decode 函数

select姓名,sum(decode(课程,'数学',分数,null))数

学,sum(decode(课程,'语文',分数,null))语文,sum(decode(课程,'历史','分数',null))历史from学生group by姓名;

--level。。。。connect by(层次查询)

select level,emp.*from scott.emp connect by prior empno = mgr order by level;

结果:

--sys_connect_by_path函数

select ename,sys_connect_by_path(ename,'/')from scott.emp start with mgr is null connect by prior empno=mgr ;

结果:

--start with connect by prior 语法

select lpad(ename,3*(level),'')姓

名,lpad(ename,3*(level),'')姓名from scott.emp where

job<>'CLERK'start with mgr is null connect by prior mgr = empno ;

--level与prior关键字

select level,emp.*from scott.emp start with ename='SCOTT' connect by prior empno=mgr;

select level,emp.*from scott.emp start with ename='SCOTT' connect by empno =prior mgr ;

结果:

--等值连接

select empno,ename,job,sal,dname from scott.emp

a,scott.dept b where a.deptno=b.deptno and(a.deptno=10or sal>2500);

结果:

--非等值连接

select a.ename,a.sal,b.grade from scott.emp

a,scott.salgrade b where a.sal between b.losal and b.hisal ;结果:

--自连接

select a.ename,a.sal,b.ename from scott.emp a,scott.emp b where a.mgr=b.empno ;

结果:

--左外连接

select a.ename,a.sal,b.ename from scott.emp a,scott.emp b where a.mgr=b.empno(+);

结果:

--多表连接

select*from scott.emp ,scott.dept,scott.salgrade where scott.emp.deptno=scott.dept.deptno and scott.emp.sal between scott.salgrade.losal and scott.salgrade.hisal ;

结果:

select*from scott.emp a join scott.dept b on

a.deptno=

b.deptno join scott.salgrade s on a.sal between s.losal and s.hisal where a.sal>1000;

select*from(select*from scott.emp a join scott.dept b on a.deptno=b.deptno where a.sal>1000) c join scott.salgrade s on c.sal between s.losal and s.hisal ;

--单行子查询

select*from scott.emp a where a.deptno=(select deptno from scott.dept where loc='NEW YORK');

select*from scott.emp a where a.deptno in(select deptno from scott.dept where loc='NEW YORK');

结果:

--单行子查询在 from 后

select scott.emp.*,(select deptno from scott.dept where

loc='NEW YORK') a from scott.emp ;

--使用 in ,all,any 多行子查询

--in:表示等于查询出来的对应数据

select ename,job,sal,deptno from scott.emp where job

in(select distinct job from scott.emp where deptno=10);

--all:表示大于所有括号中查询出来的对应的数据信息

select ename,sal,deptno from scott.emp where sal>all(select sal from scott.emp where deptno=30);

--any:表示大于括号查询出来的其中任意一个即可(只随机一个)select ename,sal,deptno from scott.emp where sal>any(select sal from scott.emp where deptno=30);

--多列子查询

select ename,job,sal,deptno from scott.emp

where(deptno,job)=(select deptno,job from scott.emp where ename='SCOTT');

select ename,job,sal,deptno from scott.emp

where(sal,nvl(comm,-1))in(select sal,nvl(comm,-1)from scott.emp where deptno=30);

--非成对比较

select ename,job,sal,deptno from scott.emp where sal

in(select sal from scott.emp where deptno=30)and nvl(comm,-1) in(select nvl(comm,-1)from scott.emp where deptno=30);

--其他子查询

select ename,job,sal,deptno from scott.emp where

exists(select null from scott.dept where

scott.dept.deptno=scott.emp.deptno and scott.dept.loc='NEW YORK');

select ename,job,sal from scott.emp join(select

deptno,avg(sal)avgsal,null from scott.emp group by deptno) dept on emp.deptno=dept.deptno where sal>dept.avgsal ;

create table scott.test(

ename varchar(20),

job varchar(20)

);

--drop table test ;

select*from scott.test ;

--Insert与子查询(表间数据的拷贝)

insert into scott.test(ename,job)select ename,job from scott.emp ;

--Update与子查询

update scott.test set(ename,job)=(select ename,job from scott.emp where ename='SCOTT'and deptno ='10');

--创建表时,还可以指定列名

create table scott.test_1(ename,job)as select ename,job from scott.emp ;

select*from scott.test_1 ;

--delete与子查询

delete from scott.test where ename in('');

--合并查询

--union语法(合并且去除重复行,且排序)

select ename,sal,deptno from scott.emp where deptno>10union select ename,sal,deptno from scott.emp where deptno<30;

select a.deptno from scott.emp a union select b.deptno from scott.dept b ;

--union all(直接将两个结果集合并,不排序)

select ename,sal,deptno from scott.emp where deptno>10union all select ename,sal,deptno from scott.emp where deptno<30;

select a.deptno from scott.emp a union all select b.deptno from scott.dept b ;

--intersect:取交集

select ename,sal,deptno from scott.emp where deptno>10 intersect select ename,sal,deptno from scott.emp where deptno<30;

--显示部门工资总和高于雇员工资总和三分之一的部门名及工资

总和

select dname as部门,sum(sal)as工资总和from scott.emp a,scott.dept b where a.deptno=b.deptno group by dname having sum(sal)>(select sum(sal)/3from scott.emp c,scott.dept d where c.deptno=d.deptno);

结果:

--使用with得到以上同样的结果

with test as(select dname ,sum(sal) sumsal from

scott.emp ,scott.dept where

scott.emp.deptno=scott.dept.deptno group by dname)select dname as部门,sumsal as工资总和from scott.test where sumsal>(select sum(sumsal)/3from scott.test);

结果:

--分析函数

select ename,sal,sum(sal)over(partition by deptno order by sal desc)from scott.emp ;

--rows n preceding(窗口子句一)

select deptno,sal,sum(sal)over(order by sal rows5preceding) from scott.emp ;

结果:

--rum(..) over(..)..

select sal,sum(1)over(order by sal) aa from scott.emp ;

select deptno,ename,sal,sum(sal)over(order by ename)连续求和,sum(sal)over()总和,100*round(sal/sum(sal)over(),4) as份额from scott.emp;

结果:

select deptno,ename,sal,sum(sal)over(partition by deptno order by ename)部门连续求和,sum(sal)over(partition by deptno)部门总和,100*round(sal/sum(sal)over(),4)as总份额from scott.emp;

结果:

select deptno,sal,rank()over(partition by deptno order by sal),dense_rank()over(partition by deptno order by sal)from scott.emp order by deptno ;

结果;

select*from(select rank()over(partition by课程order by 分数desc) rk,分析函数_rank.*from分析函数_rank)where

rk<=3;

--dense_rank():有重复的数字不跳着排列

--row_number()

select deptno,sal,row_number()over(partition by deptno order by sal) rm from scott.emp ;

结果:

--lag()和lead()

select deptno,sal,lag(sal)over(partition by deptno order by sal)上一个,lead(sal)over(partition by deptno order by sal) from scott.emp ;

结果:

--max(),min(),avg()

select deptno,sal,max(sal)over(partition by deptno order by sal)最大,min(sal)over(partition by deptno order by sal)最小,avg(sal)over(partition by deptno order by sal)平均from scott.emp ;

结果:

--first_value(),last_value()

select deptno,sal,first_value(sal)over(partition by deptno)最前,last_value(sal)over(partition by deptno )最后from scott.emp ;

结果:

--分组补充 group by grouping sets

select deptno ,sal,sum(sal)from scott.emp group by grouping sets(deptno,sal);

select null,sal,sum(sal)from scott.emp group by sal union all select deptno,null,sum(sal)from scott.emp group by deptno ;

结果:

--rollup

select deptno,job,avg(sal)from scott.emp group by

rollup(deptno,job);

--理解rollup等价于

select deptno,job,avg(sal)from scott.emp group by deptno,job union select deptno ,null,avg(sal)from scott.emp group by deptno union select null,null,avg(sal)from scott.emp ;

结果:

select deptno,job,avg(sal) a from scott.emp group by

cube(deptno,job);

--理解CUBE

select deptno,job,avg(sal)from scott.emp group by

cube(deptno,job);

--等价于

select deptno,job,avg(sal)from scott.emp group by grouping sets((deptno,job),(deptno),(job),());

结果:

--查询工资不在1500至2850之间的所有雇员名及工资

select ename,sal from scott.emp where sal not in(select sal from scott.emp where sal between1500and2850);

--部门10和30中的工资超过1500的雇员名及工资

select deptno,ename,sal from scott.emp a where a.deptno

in(10,30)and a.sal>1500order by sal desc;

结果:

--在1981年2月1日至1981年5月1日之间雇佣的雇员名,岗位及雇佣日期,并以雇佣日期先后顺序排序

select ename as姓名,job as岗位,hiredate as雇佣日期from scott.emp a where a.hiredate between

to_date('1981-02-01','yyyy-mm-dd')and

to_date('1981-05-01','yyyy-mm-dd')order by a.hiredate asc; 结果:

select*from scott.emp where

hiredate >to_date('1981-02-01','yyyy-MM-dd');

--查询获得补助的所有雇佣名,工资及补助额,并以工资和补助的降序排序

select ename,sal,comm from scott.emp a where https://www.doczj.com/doc/9d8852075.html,m >all(0)

order by comm desc;

--工资低于1500的员工增加10%的工资,工资在1500及以上的增加5%的工资并按工资高低排序(降序)

select ename as员工姓名,sal as补发前的工资,case when

sal<1500then(sal+sal*0.1)else(sal+sal*0.05)end补助后的工资from scott.emp order by sal desc;

结果:

--查询公司每天,每月,每季度,每年的资金支出数额

select sum(sal/30)as每天发的工资,sum(sal)as每月发的工资,sum(sal)*3as每季度发的工资,sum(sal)*12as每年发的工资from scott.emp;

结果:

--查询所有员工的平均工资,总计工资,最高工资和最低工资select avg(sal)as平均工资,sum(sal)as总计工资,max(sal) as最高工资,min(sal)as最低工资from scott.emp;

结果:

--每种岗位的雇员总数和平均工资

select job as岗位,count(job)as岗位雇员总数,avg(sal)as 平均工资from scott.emp group by job order by平均工资desc;结果:

ORACLE常用SQL语句大全

ORACLE常用SQL语句大全 一、基础 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:/mssql7backup/MyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not nul l],..) 根据已有的表创建新表: A:select * into table_new from table_old (使用旧表创建新表) B:create table tab_new as select col1,col2… from tab_old definition only<仅适用于Oracle> 5、说明:删除表 drop table tablename

6、说明:增加一个列,删除一个列 A:alter table tabname add column col type B:alter table tabname drop column colname 注:DB2DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、添加主键: Alter table tabname add primary key(col) 删除主键: Alter table tabname drop primary key(col) 8、创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、创建视图:create view viewname as select statement 删除视图:drop view viewname 10、几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count as totalcount from table1 求和:select sum(field1) as sumvalue from table1 平均:select avg(field1) as avgvalue from table1 最大:select max(field1) as maxvalue from table1 最小:select min(field1) as minvalue from table1 11、几个高级查询运算词 A:UNION 运算符 UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。 B:EXCEPT 运算符 EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。 C:INTERSECT 运算符 INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。 注:使用运算词的几个查询结果行必须是一致的。 12、使用外连接

oracle的Select语句

oracle的Select语句 select 查询的一般格式是 select {[distinct|all] columns | *} //1 选择列 from {tables | views | other select} //2 数据来源 where conditions //3 选择条件 group by columns //4 分组 having conditions //5 选择 order by columns //6 排序 一、选择列 1. select后面出现的内容可以是from后面的数据来源中的列,也可以是*,也可以是常量或者T-SQL函数。 2. 如果列名很长,或者多个表的列名有冲突,可以使用as来定义别名。 二、数据来源 1. 数据来源可以是表,视图,还可以是其他的select语句(即,行集)。 2. from子句中可以包含连接说明,即inner join,outer join这样的内容。这个内容参见下面的内容。 3. 可以在from子句中为表,视图,或者其他select语句的结果指定别名,但是不要用as。 三、where子句 1. 多个条件之间可以用and 或者or连接。 2. null值查询要使用is null,或者is not null,而不是=null或者<>null 3. like是进行模式匹配的一种方式,列的数据类型可以是任何字符或者日期数据。它的否定形式是not like。%和_是通配符,一个表示0或多个任意字符,一个表示一个任意字符。但是这两个字符如果不出现在like后面的模式中,就是两个普通字符。 4. text列的where条件只能是like,is null,patindex。 5. 如果要在like中匹配普通字符%和_,可以使用escape定义一个转义字符,这个转义字符可以随意指定。然后将这个转义字符放在一个通配符或者单引号之前,就表示这个通配符或者单引号是一个普通的字符。 6. in ,not in,between and,not between and.“between a and b”将会包括a和b在内。in可以转换为一个连接,但是not in不能。 7. where exists R.当且仅当R非空时,条件exists R为真。其否定形式是where not exists R. 8. where s <| >| = | <>| >=|<= all/any R.否定形式是在s前对整个表达式加not. 四、连接查询 两张表的连接可以用from子句的ansi连接语法或者where子句中的sql连接语法实现。ansi 连接语法格式为From table1 join_type table2 on(conditions) join_type table3 on(conditions)。 连接有等值连接,笛卡尔积(交叉连接),自然连接,theta连接,外部链接五种。 1 等值连接 select students.* , stu_course.* from students inner join stu_course on(students.id = stu_course.studentid)--31条记录,stu_course的记录数目

oracle命令行大全

SQL*PLUS命令的使用大全 Oracle的sql*plus是与oracle进行交互的客户端工具。在sql*plus中,可以运行sql*plus命令与sql*plus 语句。 我们通常所说的DML、DDL、DCL语句都是sql*plus语句,它们执行完后,都可以保存在一个被称为sql buffer的内存区域中,并且只能保存一条最近执行的sql语句,我们可以对保存在sql buffer中的sql 语句进行修改,然后再次执行,sql*plus一般都与数据库打交道。 除了sql*plus语句,在sql*plus中执行的其它语句我们称之为sql*plus命令。它们执行完后,不保存在sql buffer的内存区域中,它们一般用来对输出的结果进行格式化显示,以便于制作报表。 下面就介绍一下一些常用的sql*plus命令: 1. 执行一个SQL脚本文件 SQL>start file_name SQL>@ file_name 我们可以将多条sql语句保存在一个文本文件中,这样当要执行这个文件中的所有的sql语句时,用上面的任一命令即可,这类似于dos中的批处理。 2. 对当前的输入进行编辑 SQL>edit 3. 重新运行上一次运行的sql语句 SQL>/ 4. 将显示的内容输出到指定文件 SQL> SPOOL file_name 在屏幕上的所有内容都包含在该文件中,包括你输入的sql语句。 5. 关闭spool输出 SQL> SPOOL OFF 只有关闭spool输出,才会在输出文件中看到输出的内容。 6.显示一个表的结构 SQL> desc table_name 7. COL命令: 主要格式化列的显示形式。 该命令有许多选项,具体如下: COL[UMN] [{ column|expr} [ option ...]] Option选项可以是如下的子句: ALI[AS] alias CLE[AR] FOLD_A[FTER] FOLD_B[EFORE] FOR[MA T] format

Oracle-SQL简单查询语句处理

Oracle-SQL简单查询语句处理笔记一 数据处理及进展–数据:是指所有能输入到计算机中并被计算机程序处理的符号的总称。 数据处理:是指对各种形式的数据进行收集、储存、加工和传播的一系列活动的综合。 其目的是从大量的、原始的数据中抽取、推导出对人们有价值的信息。 数据处理的3个阶段: ?人工管理阶段 ?文件系统阶段 ?数据库系统阶段 关系型数据库 ?数据模型–是数据库系统中,用于抽象、表示、处理现实世界中数据的一种形式架构。 ?数据模型三层次 概念模型:是现实世界到信息世界的第一层抽象,常用E-R 图表示。逻辑模型:是用户从数据库所看到的模型,是具体的DBMS 所支持的数据模型,常用的包括层次模型、网状模型、关系模型。

物理模型:是面向计算机物理表示的模型,描述了数据在储存介质上的组织结构,它不但与具体的DBMS有关,而且 V1.0 还与操作系统和硬件有关 关系型数据库 ?关系模型 关系模型有关系数据结构、关系操作集合和关系完整性约束三部分组成的。 关系数据结构:在关系模型中,现实世界的实体以及实体间的各种联系均用关系来表示。 在用户看来,关系就是一张由行和列组成的二维数据表。 关系操作:包括:选择、投影、连接、增加、删除、修改等。 关系完整性约束:包括实体完整性、参照完整性和用户定义的完整性。 算术运算符? 算术运算符优先级 乘除优先于加减 相同优先权的表达式按照从左至右的顺序依次计算 括弧可以提高优先权,并使表达式的描述更为清晰 空值NULL

?空值NULL –空值是指一种无效的、未赋值、未知的或不可用的值。 空值不同于零或者空格。 列别名 好处:方便查看 用来重新命名列的显示标题,如果SELECT语句中包含计算列,通常使用列别名来重新定义列标题。 ?使用列别名的方法 方式1:列名列别名 方式2:列名 AS 列别名 ?以下三种情况列别名两侧需要添加双引号 列别名中包含有空格 列别名中要求区分大小写 列别名中包含有特殊字符 连接操作符使用 || 去连接,相当于java中的 + 号呢。 消除重复行使用DISTINCT 关键字 习题整理一:简单的查询语句(查询oracle已经创建的表格,使用用户scott普通用户登陆) 说明:

Oracle查询语句基本命令一

oracle查询语句大全--基本命令大全一 1.create user username identified by password;//建用户名和密码oracle ,oracle 2.grant connect,resource,dba to username;//授权grant connect,resource,dba,sysdba to username; 3.connect username/password//进入。 4.select table_name,column_name from user_tab_columns where table_name='mview_log';//查询表中的表名,字段名等等。 5. 如何执行脚本SQL文件? SQL>@PATH/filename.sql; 6.Oracle oledb 提供者在command中执行多条SQL语句与SQL SERVER有少许差别,SQL Server只需使用";"分割多条SQL语句,而Oracle需要遵守ORACLE调用规范,即除分号分割外,还需以begin /end;包围语句体. 使用C#描述应如下所示: https://www.doczj.com/doc/9d8852075.html,mandText = "begin INSERT INTO GROUP_INFO (GROUP_ID, GROUP_NAME) V ALUES (1, \'2\'); INSERT INTO GROUP_INFO(GROUP_ID, GROUP_NAME) V ALUES (2, \'2\'); end;"; 7.查询用户下的表的信息select distinct table_name from user_tab_columns; 8.如何搜索出前N条记录?Select a.*,rownum from (select * from cardkind order by cardkind ) a where rownum show user 3、查看系统拥有哪些用户SQL> select * from all_users; 4、新建用户并授权 SQL> create user a identified by a;(默认建在SYSTEM表空间下) SQL> grant connect,resource to a; 5、连接到新用户SQL> conn a/a

oracle 经典SQL语句大全

一、基础 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1. dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2… from tab_old definition only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明:添加主键:Alter table tabname add primary key(col) 说明:删除主键: Alter table tabname drop primary key(col) 8、说明:创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明:创建视图:create view viewname as select statement 删除视图:drop view viewname 10、说明:几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count as totalcount from table1

Oracle常用的45个查询语句

日期/时间 相关查询 1、获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天。你可以用任何的日期值替换 “SYSDATE”来指定查询的日期。 1 SELECT TRUNC (SYSDATE, 'MONTH') "First day of current month " 2 FROM DUAL; 2、获取当前月份的最后一天 这个查询语句类似于上面那个语句,而且充分照顾到了闰年,所以当二月份有 29 号,那么就会返回 29/2 。你可以用任何的日期值替换 “SYSDATE”来指定查询的日期。 view source print? 1 SELECT TRUNC (LAST_DAY (SYSDATE)) "Last day of current mont h" 2 FROM DUAL; 3、获取当前年份的第一天 每年的第一天都是1 月1日,这个查询语句可以使用在存储过程中,需要对当前年份第一天做一些计算的时候。你可以用任何的日期值替换 “SYSDATE”来指定查询的日期。 1 SELECT TRUNC (SYSDATE, 'YEAR') "Year First Day" FROM DUAL; 4、获取当前年份的最后一天 类似于上面的查询语句。你可以用任何的日期值替换 “SYSDATE”来指定查询的日期。 view source print? 1 SELECT ADD_MONTHS (TRUNC (SYSDATE, 'YEAR'), 12) - 1 "Year Last Day" FROM DUAL 5、 获取当前月份的天数 这个语句非常有用,可以计算出当前月份的天数。你可以用任何的日期值替换 “SYSDATE”来指定查询的日期。 view source print? 1 SELECT CAST (TO_CHAR (LAST_DAY (SYSDATE), 'dd') AS INT) num ber_of_days 2 FROM DUAL; 6、获取当前月份剩下的天数 下面的语句用来计算当前月份剩下的天数。你可以用任何的日期值替换 “SYSDATE”来指定查询的日期。 1 SELECT SYSDATE, 2 LAST_DAY (SYSDATE) "Last",

Oracle表及字段查询语句

下面为您介绍的语句用于实现Oracle查询用户所有表,如果您对o r a c l e查询方面感兴趣的话,不妨一看。 select * from all_tab_comments 查询所有用户的表,视图等 select * from user_tab_comments 查询本用户的表,视图等 select * from all_col_comments 查询所有用户的表的列名和注释. select * from user_col_comments 查询本用户的表的列名和注释 select * from all_tab_columns 查询所有用户的表的列名等信息(详细但是没有备注). select * from user_tab_columns 查询本用户的表的列名等信息(详细但是没有备注). 一般使用 1: select t.table_name,https://www.doczj.com/doc/9d8852075.html,ments from user_tab_comments t 一般使用 2: select r1, r2, r3, r5

from (select a.table_name r1, a.column_name r2, https://www.doczj.com/doc/9d8852075.html,ments r3 from user_col_comments a), (select t.table_name r4, https://www.doczj.com/doc/9d8852075.html,ments r5 from user_tab_comments t) where r4 = r1 以上就是oracle查询用户所有表的语句介绍。如何在oracle中查询所有用户表的表名、主键名称、索引、外键等 1、查找表的所有索引(包括索引名,类型,构成列): select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_ name and t.table_name = i.table_name and t.table_name = 要查询的表 2、查找表的主键(包括名称,构成列): select cu.* from user_cons_columns cu, user_constraints au where cu.constraint_name = au. constraint_name and au.constraint_type = 'P' and au.table_name = 要查询的表 3、查找表的唯一性约束(包括名称,构成列): select column_name from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'U' and au.table_name = 要查询的表 4、查找表的外键(包括名称,引用表的表名和对应的键名,下面是分成多步查询): select * from user_constraints c where c.constraint_type = 'R'and c.table_name = 要查询的表

oracle查询语句大全

oracle查询语句大全oracle 基本命令大全一 1.create user username identified by password;//建用户名和密码oracle ,oracle 2.grant connect,resource,dba to username;//授权grant connect,resource,dba,sysdba to username; 3.connect username/password//进入。 4.select table_name,column_name from user_tab_columns where table_name='mview_log';//查询表中的表名,字段名等等。 5. 如何执行脚本SQL文件? SQL>@PA TH/filename.sql; 6.Oracle oledb 提供者在command中执行多条SQL语句与SQL SERVER有少许差别,SQL Server只需使用";"分割多条SQL语句,而Oracle需要遵守ORACLE调用规范,即除分号分割外,还需以begin /end;包围语句体. 使用C#描述应如下所示: https://www.doczj.com/doc/9d8852075.html,mandText = "begin INSERT INTO GROUP_INFO (GROUP_ID, GROUP_NAME) V ALUES (1, \'2\'); INSERT INTO GROUP_INFO(GROUP_ID, GROUP_NAME) V ALUES (2, \'2\'); end;"; 7.查询用户下的所有表select distinct table_name from user_tab_columns; 8.如何搜索出前N条记录?Select a.*,rownum from (select * from cardkind order by cardkind ) a where rownum show user 3、查看系统拥有哪些用户 SQL> select * from all_users; 4、新建用户并授权 SQL> create user a identified by a;(默认建在SYSTEM表空间下)

Oracle数据库常用的Sql语句

Oracle数据库常用的Sql语句 今天想查询一下Oracle数据库下所有的表名或某个用户下的所有表,半天没想起来.还是在网上找到了答案. select table_name from all_tables;//所有的表明 select table_name from user_all_tables;//用户的所有的表 一下是转贴的sql语句的帖子. select * from user_objects; //查询所有的表 select * from dba_tables; //查询所有的表 select * from all_tables; //查询所有的表 select * from user_users //查出一个用户 select * from all_users //查询所有用户 select * from dba_users //查询所有用户 select name,dbid from v$database; //查询数据库名和它的ID select * from https://www.doczj.com/doc/9d8852075.html,er_tab_columns; //查询表名,并显示列名 describe 表名//查询表结构 2: 查询数据库参数 show parameter db; 3:查询数据库的实例名 select instance_name from v$instance; 4: 数据库域名 数据库安装结束后,如果要知道正在运行额数据库是否有域名以及数据库域名名称可以用select value from v$parameter where name='db_domain' show parameter domain 5:数据库服务名 如果数据库有域名,则数据库服务名就是全局数据库名,如果该数据库没有定义域名,则数据库服务名与数据库名相同 show parameter service_name 6:显示当前用户 show user 7:直接登陆 sqlplus "/as sysdba" 8:当前ORACLE系统时间 select sysdate from dual; 9:查询数据库字典v$nls_parameter产看字符集相关参数 select * from v$nls_parameters; //************* oracle基本操作语句(适合初学者) oracle操作语句:

oracle基本操作语句(适合初学者)

1. select * from table_name where rownum>begin and rownum< end 2.sql = "select * from table" con.prepareCall("SELECT * FROM(SELECT A.*, rownum r FROM("+sql+") A WHERE rownum <= "+intPage*intPageSize+") B WHERE r > "+(intPage-1) *intPageSize); 今天想查询一下Oracle数据库下所有的表名或某个用户下的所有表,半天没想起来.还是在网上找到了答案. select table_name from all_tables;//所有的表明 select table_name from user_all_tables;//用户的所有的表 一下是转贴的sql语句的帖子. select * from user_objects; //查询所有的表 select * from dba_tables; //查询所有的表 select * from all_tables; //查询所有的表 select * from user_users //查出一个用户 select * from all_users //查询所有用户 select * from dba_users //查询所有用户 select name,dbid from v$database; //查询数据库名和它的ID select * from https://www.doczj.com/doc/9d8852075.html,er_tab_columns; //查询表名,并显示列名 describe 表名//查询表结构 select * from https://www.doczj.com/doc/9d8852075.html,er_tab_columns where table_name=表名//查询指定表名的字段 2: 查询数据库参数 show parameter db;

Oracle数据库语句大全

Oracle数据库语句大全 一.入门部分 1.创建表空间 create tablespace schooltbs datafile ‘D:\oracle\datasource\schooltbs.dbf’ size 10M autoextend on; 2.删除表空间 drop tablespace schooltbs[including contents and datafiles]; 3.查询表空间基本信息 select *||tablespace_name from DBA_TABLESPACES; 4.创建用户 create user lihua identified by lihua default tablespace schooltbs temporary tablespace temp; 5.更改用户 alter user lihua identified by 123 default tablespace users; 6.锁定用户 alter user lihua account lock|unlock; 7.删除用户 drop user lihua cascade;--删除用户模式 8.oracle数据库中的角色 connect,dba,select_catalog_role,delete_catalog_role,execute_catalo g_role,exp_full_database,imp_full_database,resource 9.授予连接服务器的角色 grant connect to lihua; 10.授予使用表空间的角色 grant resource to lihua with grant option;--该用户也有授权的权限 11.授予操作表的权限 grant select,insert on user_tbl to scott;--当前用户 grant delete,update on https://www.doczj.com/doc/9d8852075.html,er_tbl to scott;--系统管理员 二.SQL查询和SQL函数 1.SQl支持的命令: 数据定义语言(DDL):create,alter,drop 数据操纵语言(DML):insert,delete,update,select 数据控制语言(DCL):grant,revoke 事务控制语言(TCL):commit,savepoint,rollback 2.Oracle数据类型 字符,数值,日期,RAW,LOB 字符型 char:1-2000字节的定长字符

Oracle查询用户所有表的语句

Oracle查询用户所有表的语句 下面为您介绍的语句用于实现Oracle查询用户所有表,如果您对oracle查询方面感兴趣的话,不妨一看。 select * from all_tab_comments -- 查询所有用户的表,视图等 select * from user_tab_comments -- 查询本用户的表,视图等 select * from all_col_comments --查询所有用户的表的列名和注释. select * from user_col_comments -- 查询本用户的表的列名和注释 select * from all_tab_columns --查询所有用户的表的列名等信息(详细但是没有备注). select * from user_tab_columns --查询本用户的表的列名等信息(详细但是没有备注). --一般使用1: select t.table_name,https://www.doczj.com/doc/9d8852075.html,ments from user_tab_comments t --一般使用2: select r1, r2, r3, r5 from (select a.table_name r1, a.column_name r2, https://www.doczj.com/doc/9d8852075.html,ments r3

from user_col_comments a), (select t.table_name r4, https://www.doczj.com/doc/9d8852075.html,ments r5 from user_tab_comments t) where r4 = r1 以上就是oracle查询用户所有表的语句介绍。 1、查找表的所有索引(包括索引名,类型,构成列):select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name = 要查询的表 2、查找表的主键(包括名称,构成列): select cu.* from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'P' and au.table_name = 要查询的表 3、查找表的唯一性约束(包括名称,构成列): select column_name from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'U' and au.table_name = 要查询的表 4、查找表的外键(包括名称,引用表的表名和对应的键名,

oracle基础select查询语句

select dname "工资" from dept; --查询全部员工 select * from emp; --查询指定的编号,姓名和职位 select empno,ename,job from emp; --修改显示别名空格,数字和特殊符号(#$除外)做别名需要双引号 select empno as "1" ,ename 员工姓名,job "职位" from emp; --去除重复行 select distinct job from emp; --列拼接 select concat(concat(concat(concat(concat(' 编号是:',empno ),'的雇员,姓名是:'),ename) ,'工作是:'),job) from emp; --oracle用简单办法||拼接 select '编号:' || empno || '的雇员,姓名是:' || ename || ',工作是:' || job from emp; --使用nvl (v1,v2)处理空值v1不为空返回,v2为空返回v2 select ename,sal,comm,sal*12 + nvl (comm,0) from emp; --使用decode select ename,sal,comm,sal*12 + decode(comm,null,0,comm)from emp; --单行单列虚拟表dual select sysdate from dual --得到一个32位的唯一guid select sys_guid() from dual; --进行加减乘除运算 select 1+3 from dual --oracle条件语句查询 --查询出基本工资大于2000的所有雇员信息 select * from emp where sal > 2000; --查询职位是办事员的所有雇员信息 select * from emp where job ='CLERK' --查询工资在2000-3000之间的员工信息 select * from emp where sal>2000 and sal < 3000; --查询工资在2000-3000之间的全部雇员信息*(包含) select * from emp where sal >= 2000 and sal <=3000; select * from emp where sal between 2000 and 3000; --查询职位是办事员或者销售人员的全部信息 select * from emp where job='CLERK' or job='SALESMAN'; select * from emp where job in ('CLERK','SALESMAN'); --查询所用不是办事员的雇员信息 select * from emp where job <>'CLERK'; select * from emp where job != 'CLERK'; select * from emp where not job = 'CLERK';

16种oracle查询日期语句

16种oracle查询日期语句 https://www.doczj.com/doc/9d8852075.html, 2010-10-27 13:04 佚名互联网我要评论() ?摘要:查询日期是使用oracle数据库过程中经常要遇到的操作,下文列举了一些oracle查询日期语句的例子,如果您感兴趣的话,不妨一看。 ?标签:oracle查询日期 ?限时报名参加“甲骨文全球大会·2010·北京”及“JavaOne和甲骨文开发者大会 2010” oracle查询日期语句在我们使用数据库过程中是经常要用到的,下面就为您介绍16种oracle查询日期语句,每一种oracle查询日期语句实现了一个功能,希望对您能有所帮助。 №1:取得当前日期是本月的第几周 SQL> select to_char(sysdate,'YYYYMMDD W HH24:MI:S S') from dual; TO_CHAR(SYSDATE,'YY ------------------- 20030327 4 18:16:09 SQL> select to_char(sysdate,'W') from dual; T 4

№2:取得当前日期是一个星期中的第几天,注意星期日是第一天SQL> select sysdate,to_char(sysdate,'D') from dual; SYSDATE T --------- - 27-MAR-03 5 类似: select to_char(sysdate,'yyyy') from dual; --年 select to_char(sysdate,'Q' from dual; --季 select to_char(sysdate,'mm') from dual; --月 select to_char(sysdate,'dd') from dual; --日 ddd 年中的第几天 WW 年中的第几个星期 W 该月中第几个星期 DAY 周中的星期几 D 今天对映的NUMBER '1','星期日', '2','星期一', '3','星期二', '4','星期三', '5','星期四', '6','星期五', '7','星期六' hh 小时(12) hh24 小时(24)

oracle表查询

使用逻辑操作符号 问题:查询工资高于500或者是岗位为MANAGER的雇员,同时还要满足他们的姓名首字母为大写的J? SELECT*FROMempWHERE(sal>500orjob='MANAGER')andenameLIKE'J% '; ν使用orderby字句默认asc 问题:如何按照工资的从低到高的顺序显示雇员的信息? SELECT*FROMempORDERbysal; 问题:按照部门号升序而雇员的工资降序排列 SELECT*FROMempORDERbydeptno,salDESC; ν使用列的别名排序 问题:按年薪排序 selectename,(sal+nvl(comm,0))*12"年薪"fromemporderby"年薪"asc; 别名需要使用“”号圈中,英文不需要“”号 ν分页查询 等学了子查询再说吧。。。。。。。。 Clear清屏命令 oracle表复杂查询 ν说明 在实际应用中经常需要执行复杂的数据统计,经常需要显示多张表的数据,现在我们给大家介绍较为复杂的select语句 ν 数据分组——max,min,avg,sum,count

问题:如何显示所有员工中最高工资和最低工资? SELECTMAX(sal),min(sal)FROMempe; 最高工资那个人是谁? 错误写法:selectename,salfromempwheresal=max(sal); 正确写法:selectename,salfromempwheresal=(selectmax(sal)fromemp); 注意:selectename,max(sal)fromemp;这语句执行的时候会报错,说 ORA-00937:非单组分组函数。因为max是分组函数,而ename不是分组函数....... 但是selectmin(sal),max(sal)fromemp;这句是可以执行的。因为min和max 都是分组函数,就是说:如果列里面有一个分组函数,其它的都必须是分组函数,否则就出错。这是语法规定的 问题:如何显示所有员工的平均工资和工资总和? 问题:如何计算总共有多少员工问题:如何 扩展要求: 查询最高工资员工的名字,工作岗位 SELECTename,job,salFROMempewheresal=(SELECTMAX(sal)FROMemp); 显示工资高于平均工资的员工信息 SELECT*FROMempewheresal>(SELECTAVG(sal)FROMemp); groupby和having子句 groupby用于对查询的结果分组统计, having子句用于限制分组显示结果。 问题:如何显示每个部门的平均工资和最高工资?

相关主题
文本预览
相关文档 最新文档