oracle基础语句学习

1.寻找公司所有部门信息

select * from dept;

2.寻找特定列

select dept_name from dept;

3.使用列别名

基本书写方法:列名 列别名

       列名 as 列别名

以下三种情况,列别名两侧需要添加双引号(""):

  列别名中包含有空格。

  列别名中要求区分大小写。

  列别名中包含有特殊字符。

select emp_id id,last_name as emp_name,salary "Salary",(400+salary)*12 "Annual Salary" from emp;

4.连接运算符的使用

采用竖线(||)来连接做连接运算符

select empno||' '||ename from emp;

5.distinct关键字的用法

去重

select distinct dept_id,job_id from emp;

6.选择表中的部分行,where子句

select last_name,hire_date from emp where hire_date >='01-1月-1999';

7.between...and...

select emp_id,last_name,salary from emp where salary between 4200 and 5000;

8.in运算符

select emp_id,last_name,salary,dept_id from emp where dept_id in (10,90,110);

9.like运算符

模糊查询。常用的通配符有"%"和"_",百分号代表多个字符,下划线代表一个字符。

查询的有特殊字符时,会用到escape

select emp_id,last_name,salary from emp where last_name like 'S%';

select emp_id,last_name,salary from emp where last_name like '_b%';

select emp_id,last_name,salary,job_id from emp where job_id like 'FI\_%' escape '\';--查询job_id以“FI_”开头的信息

10.is null运算符

select emp_id,last_name,dept_id from emp where dept_id is null;

11.and运算符

select emp_id,last_name,sal from emp wjere sal >=4200 and sal <=6000;

12.or运算符

select last_name,salary,dept_id from emp where salart > 10000 or dept_id in (60,90);

13.not运算符

select last_name,job_id,salary from emp where job_id not in ('IT_PROG','ST_CLERK','FI_ACCOUNT');

14.order by子句

select last_name,job_id,salary,dept_id from emp order by dept_id desc;--默认是asc

使用列别名排序,多列排序

select last_name,job_id,salary*12 annual,dept_id from emp order by annual;

select last_name,job_id,salary*12 annual,dept_id from emp order by job_id,annual desc;

order by子句可以出现在select子句中没有出现过的列。

select last_name,job_id,hire_date from emp order by salary;

order by子句后的列名可以用数字来代替,数字为select后列的顺序。

select last_name,job_id,salary,dept_id form emp order by 2,3 desc;

15.字符函数

lower:将大写或小写混合的字符转换成小写。

upper:将大写或小写混合的字符转换成大写。

initcap:将每个单词的第一个字母转换成大写,其余的变为小写。

    函数          结果

lower('SQL Course')       sql course

upper('SQL Course')      SQL COURSE

initcap('SQL Course')        Sql Course

字符处理函数

concat:连接两个值,相当于‘||’

substr:返回第一个参数中从n1字符开始长度为n2的子串,如果n1是负值,表示从后向前数的abs(n1)位,如果n2省略,则取n1之后的所有字符

length:取字符长度

instr:返回s1中,子串s2从n1开始,第n2次出现的位置

lpad:返回s1被s2从左面填充到n1长度

rpad:返回s1被s2从右面填充到n1长度

trim:去除字符串头部或者尾部的字符

replace:把s1中的s2用s3替换

    函数          结果

concat('Good','string')    Goodstring

substr('string',1,3)      str

length('string')         6

instr('string','r')        3

lpad(sal,10,'*')          ******5000

rpad(sal,10,'*')         5000******

trim('S' from 'SSMITH')    MITH

replace('abc','b','d')      adc

select emp_id,concat(first_name,last_name) name,job_id,length(last_name) length from emp where substr(job_id,4)='ACCOUNT' and instr(last_name,'e') > 0;

16.数字函数

round:将列活表达书所表示的值四舍五入到小数点后第n位

trunc:截取到小数点后第n位

mod:取m除以n得到的余数

select round(65.654,2),round(65.654,0),round(65.654,-1) from dual;

select trunc(65.654,2),trunc(65.654,0),trunc()(65.654,-1) from dual;

select emp_id,last_name,sal,mod(sal,900) from emp where dept_id = 90;

17.日期函数

sysdate:系统日期

months_between:两个日期相隔的月数

add_months:在指定日期基础上加相应的月数

next_day:某一日期的下一个指定日期

last_day:指定日期当月最后一天的日期

round:将date按照fmt指定的格式进行四舍五入 round(date,['fmt'])

trunc:按照指定格式进行截取

extract:从日期类型中取出指定年、月、日

select last_name,sal,months_between(sysdate,hire_date) months from emp order by months;

select last_name,sal,hire_date,add_months(hire_date,3) new_date from emp where hire_date > '01-1月-1999';

select next_day('02-2月-06','星期一') next_day from dual;

select last_day('02-2月-06') "LAST_DAY" from dual;

select emp_id,hire_date,round(hire_date,'MONTH') from emp where substr(hire_date,-2,2) = '98';-- -2,2为从右向左数2位

select emp_id,hire_date,trunc(hire_date,'MONTH') from emp where substr(hire_date,-2)='98';

select last_name,hire_date,extract(MONTH FROM hire_date) MONTH from emp where dept_id = 90;

18.数据类型显性转换

to_char函数:转换成字符型

to_date函数:转换成日期

to_number:转换成数字型

19.NVL函数:NVL(表达式1,表达式2) 空值转换

NVL(comm,0)

NVL(hire_date,'01-JAN-06')

NVL(job_id,'No Job Yet')

20.NVL2函数:NVL2(表达式1,表达式2,表达式3),对第一个参数进行检查,如果第一个参数不为空,则输出第二个参数,否则输出第三个参数

select last_name,salary,NVL2(commission_pct,salary+commission_pct,salary) income from emp where last_name like '_a%';

21.NULLIF函数:NULLIF(表达式1,表达式2),对两个参数比较,当两个参数不相等时,返回第一个参数值,相等返回空值

select last_name,length(last_name),NULLIF(length(last_name),length(email)) result from emp where last_name like 'D%';

22.coalesce函数:是对NVL函数的扩展,返回第一个不为空的参数,参数个数不受限制

select last_name,coalesce(commission_pct,salary*1.3,100) comm,dept_id from emp where dept_id in (50,80) order by comm;

23.case语句

select last_name,commission_pct,

  (case commission_pct

    when 0.1 then '低'

    when 0.2 then '中'

    when 0.3 then '高'

    else '无'

  end) commission from emp where commission_pct is not null order by last_name;

24.decode语句

select last_name,commission_pct,

  decode(commission_pct,

      0.1 '低',

      0.2 '中',

      0.3 '高',

      '无'

) commission from emp where commission_pct is not null order by last_name;

25.多表关联:

笛卡尔积:把表中所有的记录作乘积操作,生成大量的结果,行程这种情况的原因通常是犹豫连接条件缺失

等值连接:简单连接成内连接,当两个表有公共字段

select emp.last_name,emp.job_id,emp.dept_id,dept.dept_name from emp,dept where emp.dept_id=dept.dept_id;

select emp.last_name,emp.job_id,emp.dept_id,dept.dept_name from emp,dept where emp.dept_id=dept.dept_id and emp.job_id like '%MAN%';

ps:sql语句的书写顺序是:select from where group by order by

     sql实际的执行顺序是:from where group by select order by

两个以上表连接:

select e.last_name,e.jpb_id,e.dept_id,d.dept_name,l.city from emp e,dept d,locations l where e.dept_id=d.dept_id and d.location_id=l.location_id and l.city in ('Southlake','Oxford');

不等值连接:between...and...

select e.last_name,e.job_id,e.salary,s.grade_level from emp e,salgrades s where e.salary between s.lowest_salary and s.highest_salary and e.job_id in ('IT_PROG','SA_REP') order by s.grade_level;

左外连接:以左表为基准,左表中的每个记录都必须显示,即使右表中没有记录

右外连接:以右表为基准,右表中的每个记录都必须显示,即使左表中没有记录

select e.last_name,e.job_id,e.dept_id,d.dept_name from emp e,dept d where e.dept_id(+)=d.dept_id;--查询所有部门信息,不管部门是否有员工

select e.last_name,e.job_id,e.dept_id,d.dept_name from emp e,dept d where e.dept_id=d.dept_id(+);--查询所有员工信息,不管员工是否有部门

cross join:交叉连接,生成笛卡尔积

natural join:自然连接

26.using:using(column_name),using子句通过名字来具体指定连接

join table2 on (table1.column_name=table2.column_name) 等值连接

left|right|full outer join:左外|右外|全外连接

27.注意:using子句和natural join不能再一套语句中同时书写

28.on子句

select e.last_name,e.job_id,e.dept_id,d.dept_name from emp e join dept d on (e.dept_id=e.dept_id);

29.全外连接:select e.last_name,e.job_id,e.dept_id,d.dept_name from emp e full outer join dept d on e.dept_id = d.dept_id;

30.union操作符:取并集(去掉重复值)

 union all:取并集以及重复部分

 intersect:去交集

 minus:取差集

31.分组函数:

min和max:最小值和最大值

sum和avg:总和和平均值

count:条数

select avg(commission_pct) from emp;--忽略空值

select avg(nvl(commission_pct,0)) from emp;

group by:select job_id,manager_id,avg(salary) from emp group by job_id,manager_id order by job_id;

使用group by子句时,group by后的列可以不再select语句中出现;select厚的费分组函数列必须在group 不用子句中出现。

32.having子句:

select job_id,max(salary) from emp group by job_id having max(salary) >= 9000;

注意:where与having的区别:

  where是一个约束声明,是在结果返回之前起作用的,不能再where中使用聚合函数

  having是一个过滤声明,是在结果返回之后起作用的,可以使用聚合函数

33.子查询:

select last_name,job_id,salary,dept_id from emp where dept_id = (select dept_id from emp where last_name='Char');

34.多行子查询-in操作符

select a.last_name,a.salary from emp a where a.emp_id in (select b.manager_id from emp b);

35.相关子查询:

select last_name,salary,job_id from emp e where salary > (select avg(salary) from emp where job_id = e,job_id);

36.insert

insert into dept values(300,'oper',110,1500);

insert into emp(emp_id,last_name) values(20,'Qiuj');

insert into hemp select * from emp where hire_date < '09-1月-06';

insert into emp((emp_id,last_name)  select * from (select 300,'qiuj1') from dual union all select * from (select 301,'qiuj2')  from dual);

mysql中value后面可以直接接,用逗号分隔就可以。

37.delete

delete [from] table1 [where...];

38.merge

merge into table1 [t_alias] using {table|view|subquery} [t_alias];

39.自动提交

set autocommit [on|off];

40.事务回滚

rollback;

savepoint a;--定义保存点a

rollback to a;--撤销操作到a保存点

commit;--提交写入数据库,commit之后不能回滚

41.锁:用来在多用户并发访问和操作数据库时保证数据的一致性。

42.创建表

create table table_name [column datatype [default expr]]...

默认值:default

使用只查询创建表

create table dept as select emp_id,last_name,sal+1000 newsalary from emp where dept=10;--select列表中的表达式要给定别名,没有别名会发生错误

引用另一个用户的表

select * from scott.emp;--注意权限

43.oracle表的分类

用户表;数据字典

查询数据字典:select table_name from user_tables;

查询数据字典结构:desc user_tables;

rowid:伪列,是表中虚拟的列,系统自动产生,唯一地址

44.alter:列的操作

alter table dossier add (sex char(1));

alter table dossier modify(sex default '1');

alter table emp drop (last_name,newsalary);

45.drop:

drop table table1;

46.rename:

rename old_name to new_name;

47.截断表:

truncate tabletbale_name;

48.约束:

not null:非空,列级指定,不可指定为表级

unique:唯一

primary key:主键(包含非空和唯一)

foreign key:外键

check:检查

约束可以通过alter...add...和alter...drop...和alter...disable\enable来增加、删除、禁用、启用

49.视图:是虚表,是一个命名的查询,用于改变基表数据的显示,简化查询,本身不占用数据库内存

50.概念:

删除视图:drop vireview_name;

内联视图:select last_name,dept_name from dept a,(select last_name,dept_id from emp) b where a,dept_id = b.dept_id;

TOP-N:查找表中最大或最小的N条记录功能

oracle:rownum

序列:create sequence [schema] 序列名

    [increment by n] --序列中间的间隔,默认为1

    [start with n] --起始序列值,默认为1

    [maxvalue n | nomaxvalue]

    [minvalue n | nominvalue]

    [cycle | nocycle] --循环,nocycle默认

    [cache n | nocache]; --预先分配并存储,cache 20是默认

伪列:nextval和currval

  nextval:在序列中增加新值并返回此值

  currval:当前序列正被分配的序列值

索引:

create index indexname on table(column[,column,...]);

drop index indexname;

同义词:

create synonym s_emp for hr.emp;

drop synonym s_emp;

创建用户:

create user user_name identified by password [default tablespace]

默认表空间名 temp tablespace 临时表空间名 quota 配额大小 on表空间名

登录权限:grant create session to test;

用户建表权限:create table emp(id number,last_name varchar2(20));

查询数据库表空间:select * from v$tablesapce;

修改默认表空间配额:alter user user_name default 默认表空间名;

密码修改:alter user user identified by newpaword;

状态管理语句:alter user user password expire;--密码过期

       alter user user account lock\unlock;--账户锁定\解锁

删除用户:drop user user;

授予权限:grant create session to test with admin option;

创建角色:create role role;

通过角色为用户test授权:grant tr to test;

 

posted on 2018-01-03 18:11  qiujiababy  阅读(4215)  评论(0编辑  收藏  举报

导航