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;

oracle基础语句学习的更多相关文章

  1. MySQL 总结篇 [在Oracle基础上学习的MySQL]

    一.学习环境 MySQL 5.7社区版 Navicat for MySQL 百度网盘下载链接:https://pan.baidu.com/s/1yGnznwoZ8CaMgrCsDBhKAQ 二.MyS ...

  2. mysql常用基础语句学习

    常用sql语句 查询: SELECT 列名(或者*,表示所有列) FROM 表名 WHERE 筛选条件; FROM 表名:顾名思义,就是从表名指定的这张表格中: WHERE 筛选条件:意思是" ...

  3. oracle基础语句练习

    1. 创建相关表结构 Emp----员工信息表 Ename ), --姓名 Empno ), --编号 Deptno ), --所在部门 Job ), --工种(人员类别),如:manager 经理, ...

  4. Oracle基础语句练习记录

    1.往scott的emp表插入一条记录 insert into scott.emp(empno,ename,job) values(9527,'EAST','SALESMAN'); 2.scott的e ...

  5. 蜗牛—ORACLE基础之学习(二)

    如何创建一个表,这个表和还有一个表的结构一样但没有数据是个空表,旧表的数据也插入的 create table newtable as select * from oldtable 清空一个表内的数据 ...

  6. oracle sql语句学习(一)

    oraclexe 11.0.2.0 输出到文件 SQL>spool /*完整路径*/; SQL>spool off; 多表自然链接 select spj.sno from spj join ...

  7. oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by

    select语句学习 . 创建表 create table user(user varchar2(20), id int); . 查看执行某条命令花费的时间 set timing on: . 查看表的 ...

  8. Oracle基础学习笔记

    Oracle基础学习笔记 最近找到一份实习工作,有点头疼的是,有阶段性考核,这...,实际想想看,大学期间只学过数据库原理,并没有针对某一数据库管理系统而系统的学习,这正好是一个机会,于是乎用了三天时 ...

  9. Oracle Select语句

    Oracle Select语句 作者:初生不惑 Oracle基础 评论:0 条 Oracle技术QQ群:175248146 在本教程中,将学习如何使用Oracle SELECT语句从单个表中查询数据. ...

随机推荐

  1. postgresql的基本使用,以及数据库的备份与恢复

    安装过程没什么说的,基本是一直下一步下一步,就是中间需要输入密码之类的 postgresql使用模式是数据库表,视图等的集合,我们可以自荐创建一个schema 创建表的方法 进入sql命令行窗口的方法 ...

  2. Explicit

    Prefixing the explicit keyword to the constructor prevents the compiler from using that constructor ...

  3. 深入详解美团点评CAT跨语言服务监控(一) CAT简介与部署

    前言: CAT是一个实时和接近全量的监控系统,它侧重于对Java应用的监控,除了与点评RPC组件融合的很好之外,他将会能与Spring.MyBatis.Dubbo 等框架以及Log4j 等结合,支持P ...

  4. 如何使用百度bae部署web项目

    百度bae提供了支持各种开发环境的的应用引擎,包括node.js.php.java等,而且还免费提供了一定容量的mysql.mongodb.redis等数据库,所以,可以把它当作一个云服务器来使用.而 ...

  5. 1. docker 在 macOS 中的架构 2. 在macOS系统中,docker pull 下来的镜像存储在哪里?

    docker 在 macOS 中的架构: 在macOS中,docker的实现跟在其它Linux系统中略有不同,在其它Linux系统中,操作系统本身就是docker容器的宿主机,docker镜像都是直接 ...

  6. jwt再度理解

    1,负载部分只用base64编码,是可逆的,不能存放密码 2,加密算法不在乎是对称还是非对称,因为jwt的验签不需要解密 3,一般的验签是用私钥加密签名,公钥验签,和加密反过来,加密是公钥加密,服务器 ...

  7. 1、Zookeeper安装及问题与集群

    1.下载zookeeper.tat.gz压缩包 2.解压 tar –xvf file.tar //解压 tar包 tar -xzvf file.tar.gz //解压tar.gz tar -xjvf ...

  8. Windows系统下MySQL数据库出现Access denied for user 'root'@'localhost' (using password:YES) 错误

    Windows系统下MySQL数据库出现Access denied for user 'root'@'localhost' (using password:YES) 错误,(root密码错误) 处理方 ...

  9. excel技巧--多行排成单列

    要将上图的多行排成单列的效果,做法如下: 1.在倒数第二列的下方单元格,写入=号,然后再点击倒数第一列第一个单元格.这样复制该单元格的公式. 2.然后对着这个复制好的单元格的右下角一直往下拖拉,尽量拖 ...

  10. Scrapy环境安装

    开始安装前,建议安装Visual C++ 2015 Build Tools,否则会一直出现如下提示: 下载地址:http://landinghub.visualstudio.com/visual-cp ...