1.解锁用户
请输入用户名:sys
输入口令:sys as sysdba
alter user scott accout unlock;
用户已更改.
SQL> commit;
SQL> conn scott/tiger
更改scott口令
新口令:tiger
重新键入新口令:tiger
增加权限 用sys登陆完 grant create table, create view to scott; conn scott/root
2.查看表结构
desc tbname
3.从表中查询数据
select * from tbname;
4.字段运算后再查询
select ename, sal*12 from emp;
5.纯数字运算查询
select 2*3 from dual; dual是oracle无意义的表
6.查询当前系统的日期
select sysdate from dual;
7.双引号保持原来的格式
select ename, sak*12 "anuual sal" from emp;
8.查询数字时,把为空值的换为0,oracle中空值运算结果都为null
select ename, sal*12 + nvl(comm, 0) from; nvl(,) 如果字段comm为空值时,用0代替
select ename, sal, comm from emp where comm is null;(is not null)(选出comm为空的数据)
9.字符串连接(把两个字段查询出来的数据作为一条字符串输出)两个单引号代替一个
select ename||sal from emp; select ename || 'ds''fsdf' from emp;
10.去掉重复的值(也会去掉多个字段组合重复的值)
select distinct ziduan from tbname;
11.条件 where
select * from tbname where ziduan > 'CBA' ; =, <, 不等于号<>,
12.条件 between and (包含800和1500)
select ename, sal from emp where sal between 800 and 1500;
select ename, sal from emp where sal >= 800 and sal <= 1500;
13.条件 in (谁的薪水值=800或1500或2000)
select ename, sal comm from where sal in (800, 1500, 2000); 也可以not in ('df', 'dfsd')
14.条件 or
select ename, sal from emp where deptno = 10 or sal > 1000;
15. 模糊查询 %零个或多个,下横线_代表一个
select ename from emp where ename like '%All%';
16. 转义字符 \ 可以制定转义字符 escape
select ename from emp where ename like '%\%%'; like '%$%%' escape '$';
17.排序 order by 默认升序asc
select empno, ename from emp order by deptno asc, ename desc;先按deptno,再按ename
18.函数 转化为小写lower()
select lower(ename) from emp;
19.函数 截子串substr(ename,2,3) 从字符串ename中第2个开始截,一个截3个字符
select substr(ename,2,3) from emp;
20.函数 把数字转化为相应的字母,相反 ascii('A')
select chr(65) from dual; a
21.函数 四舍五入 round(23.652)
select round(23.652) from dual; 24
select round(23.652, 2) from dual; 23.65 2代表舍到小数点后2位
select round(23.652,-1) from dual; 20 可以是负数
22.函数 把数字或字母或日期转化为特定的格式 to_char(sal,'$99,999.9999'), $换成L,显示¥
select to_char(sal, '$99,999.9999') from emp; 百千等位没有的不显示
select to_char(sal, '$00,000.0000') from emp; 没有的位用0补齐
select to_char(hircdate, 'YYY-MM-DD HH24:MI:SS') from emp; 转化为特定的日期,24位24进制
23.函数 把特定的字符转化为日期 to_date('', '')
select ename, hiredate from emp where hiredate > to_date('1981-2-20 12:34:56', 'YYYY-MM-DD HH24:MI:SS');
24.函数 把特定的字符转化为数字 to_number('$1,250.00', '$9,999.99')
select sal from emp where sal > to_number('$1,250.00', '$9,999.99');
25.组函数 取最大max(),最小min(),平均avg(),函数可以组合使用
select to_char(avg(sal), '99999999.99') from emp;
26.组函数 总和 sum()
select sum(sal) from emp;
27.组函数 求出总共多少条数据 count(*),count(ename), 凡是不是空值的字段一共有几个
select count(*) from emp;
select count(distinct ziduan) from tbname; distinct去掉重复
28.函数 分组查询 group by
select avg(sal), deptno from emp group by deptno;
select deptno, job, max(sal) from emp group by deptno, job;
29.分组查询,多条输入,一条输出
查询出薪水最高的人的名字(可能不止一个人)
select ename from emp where sal = (select max(sal) from emp);
查询出每个组中薪水最高的人的名字
select ename from emp where sal in (select max(sal) from emp group by deptno);
group by使用规则,要查询的字段如果没出现在组函数中则必须出现在group by中,否则出错
30.取出按部门编号分组后每个部门的平均薪水
select avg(sal) from emp group by deptno;
31.where语句是处理单条语句,有此语句先执行where语句再进行分组(有group by的话)
having 用来对分组进行限制 此处代替where
查询出部门平均薪水大于2000的平均薪水和部门编号
select deptno, avg(sal) from emp group by deptno having avg(sal) > 2000;
32.完整的select语句,按此顺序执行
select * from emp
where sal > 1000
group by deptno
having avg(sal) > 2000
order by
33.查询出薪水大于平均薪水人的名字
select ename, sal from emp where sal > (select avg(sal) from emp);
34.查出按