oracle学习(一)

1、Oracle登录

Oracle连接有三种客户端工具

  • 使用sqlplus工具连接
  • 使用sqldeveloper图形界面连接
  • 使用第三方工具

Oracle登录有两种方式

  • 用户名和密码
  • 使用用户名和角色登录
>sqlplus / as sysdba

Oracle安装好时已经创建提供了许多用户

用户名:sys  角色:dba    这个用户是活的,即可以直接使用(其他用户需要解锁使用)
用户名:scott
用户名:HR  
...

解锁其他普通用户,并设置密码

SQL> alter user scott account unlock;
SQL> alter user scott identified by tiger01;

普通用户登录

> sqlplus scott/tiger01

当出现密码忘记情况,需要使用管理员超级用户来修改
三次出错时拒绝登录

> sqlplus / as sysdba
> alter user scott identified by abc123;

普通用户修改自己的代码

SQL> password
更改 SCOTT 的口令
旧口令:
新口令:
重新键入新口令:
口令已更改

显示当前用户名

SQL> show user;
USER 为 "SCOTT"

2、Oracle基本使用

查询scott用户下所有对象

select * from tab;

查询表结构

desc emp;

修改表的字段显示宽度
Oracle一共有三种类型,varchar2、 number、 date

-- 字符类型 a12表示12column ename format a12;
-- 数字类型9999代表四位
column empno format 9999;

column mgr format 9999;

/执行上一条执行过的sql语句
host cls;清屏

NVL(a,b)函数,当a为null时,使用b值代替,如果是非null就不用b代替直接返回a值

select NVL(null,10) from dual;

NVL2(a,b,c),当a不为空时使用b,当a为空时使用c

使用别名,可以不使用双引号,但别名中不能有空格,

select empno AS 编号 from emp;
select empno AS "编号" from emp;

获取时间,默认只显示日期

select sysdate from dual;

||管道符,拼接字段,常量字符串使用单引号,将两个字段查询结果拼接在一起

> select 'hello' || ' word' "结果" from dual;
> select ename || '的薪水是' || sal " 结果" from emp;
 结果
-------------------

SMITH的薪水是800
ALLEN的薪水是1600
WARD的薪水是1250
JONES的薪水是2975
MARTIN的薪水是1250
BLAKE的薪水是2850
CLARK的薪水是2450
SCOTT的薪水是3000
KING的薪水是5000
TURNER的薪水是1500
ADAMS的薪水是1100

保存所有的命令及结果

spool e:/oracle.sql;
...
spool off;

从外部读入sql脚本并执行

@ e:/oracle.sql;

单行注释--,多行注释/* */

--select * from dual;
/*
    123
*/

SQL语句关键字大小写不敏感,字段名即列名大小写不敏感,必须使用;结束
单引号中的字符串大小写敏感

3、查询

-- 查询
select * from emp where deptno = 20;
-- 注意单引号中的字符串大小写敏感
select * from emp where ename='SMITH';
select * from emp where sal>1500;
-- 不等于可以使用!=也可以使用<>
select * from emp where sal!=1500;
select * from emp where sal>=1300 and sal<=1600;
-- between a and b 可以使用在数字型和日期型
select * from emp where sal between 1300 and 1600;
select * from emp where sal not between 1300 and 1600;
select * from emp where hiredate between '20-2月-81' and '23-2月-82';
-- 使用in 来替换or
select * from emp where (deptno==20) or (deptno==30);
select * from emp where deptno in (20,30);
select * from emp where deptno not in (20,30);
-- 查询大写字母S开员工,%表示0个,1个或多个字符
-- 精确查询使用=,不精确查询使用like,称为模糊查询,mysql与Oracle都一样
select * from emp where ename like 'S%';
-- 使用_表示一个任意字符
select * from emp where ename like '__I__';
-- 转义字符\,查询名字含有_的员工
select * from emp where ename like 'ac\_' escape '\';
-- 插入姓名含有'单引号的,注意Oracle中字符串使用单引号 
insert into emp(empno,ename) values(100,'''''');
-- null加减乘除一个数还为null,null不能参与精确查询
-- 注意不能使用like或=
select * from emp where comm is null;
select * from emp where comm is not null;
-- 按照升序出结果,不写asc默认升序
select * from emp order by sal asc;
-- 降序
select * from emp order by sal desc;
-- 佣金非空升序排列
select * from emp where comm is not null order by comm asc;
-- 当排序使用多个字段,当第一个一样时 第二个才起作用
select * from emp order by comm,sal asc;

4.单行函数

单行函数:只有一个参数输入一个参数输出
多行函数:可以有多个函数输入,一个输出
单行函数

-- 转小写
select lower('ABC') from dual;
-- 转大写
select upper('abc') from dual;
-- 字符串拼接
select concat('hello','你好') from dual;
select concat('hello','你好','abc') from dual;错误
select concat('hello',concat('你好','abc')) from dual;
select 'hello' || '你好' || 'abc' from dual;
-- 取子串
select substr('helloab你好cdef',5,3) from dual;

-- 从左到右第一次出现的位置instr函数 大小写敏感,第一个结果0,第二个结果5
SQL> select instr('helloworld','O') from dual;
SQL> select instr('helloworld','o') from dual;
-- lpad/rpad:不足指定位数,补位指定字符,lpad在左边补,rpad在右边补
select lpad('hello',10,'#') from dual;
select rpad('hello',10,'#') from dual;
-- trim去掉两边的指定字符 结果:abcooodef
select trim('o' from 'oooabcooodefooo') from dual;
-- replace 替换
select replace('hello','l','LL') from dual;
-- round,保留指定位小数,四舍五入
select round(3.1415,3) from dual;
-- trunc,保留指定位小数,不四舍五入
select trunc(3.1415,3) from dual;
-- mod,求余
select mod(10,3) from dual;

计算时间的单行函数

-- 昨天,今天,明天
select sysdate-1 "昨天",sysdate "今天",sysdate+1 "明天" from dual;
-- 上个月的今天
select add_months(sysdate,-1) from dual;
-- 上个星期三是多少号
select next_day(sysdate,'星期三') from dual;
-- 上上一个星期三
select next_day(next_day(sysdate,'星期三') ,'星期三') from dual;
-- 本月最后一天
select last_day(sysdate) from dual;

5.Oracle类型转换

Oracle三大类型
(1)varchar2是边长的,char固定长度,隐式转换:varchar2->date
(2)number—>varchar2/char
(3)date—–>varchar2/char

-- to_char('日期','格式')将日期转换为字符串
select to_char(sysdate,'yyyy mm dd day') from dual;
select to_char(sysdate,'yyyy "年" mm "月" dd "日" day') from dual;
select to_char(sysdate,'yyyy mm dd day hh24:mi:ss') from dual;
select to_char(sysdate,'yyyy mm dd day hh12:mi:ss AM') from dual;
-- to_char(数字,"格式")
select to_char(12345,'$999,999,999') from dual;

-- to_date('字符串','格式')
select * from emp where hiredate = to_date('1980年12月7日','yyyy"年"mm"月"dd"日"');

-- to_number('123')将字符串转为数字型
select to_number('123') from dual;
-- 字符串隐式转换为数字
select '123'+123 from dual;
-- 数字隐式转换为字符串
select '123'||123 from dual;
  • 14
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值