oracle基础文档

1.oracle的sqlplus....................................................................................................... 3

2 oracle 的简单查询................................................................................................... 3

3 oracle的限定查询.................................................................................................... 4

4 oracle查询排序....................................................................................................... 5

5 oracle的单行函数.................................................................................................... 8

5.1字符串函数:.................................................................................................. 8

5.2数字函数..................................................................................................... 10

5.3 时间函数.................................................................................................... 11

5.4 转换函数.................................................................................................... 13

5.5通用函数..................................................................................................... 13

6.Oracle子查询........................................................................................................ 14

6.1单行单列..................................................................................................... 14

6.2.单行多列的信息.......................................................................................... 15

6.3子查询返回单行多列.................................................................................... 16

6.4在having字句之中使用子查询..................................................................... 17

6.5在from字句中使用子查询........................................................................... 18

7分组统计查询........................................................................................................ 20

8多表查询............................................................................................................... 21

9数据库更新........................................................................................................... 25

9.1.1数据增加.................................................................................................. 25

9.1.2数据修改.................................................................................................. 26

9.1.3数据删除.................................................................................................. 27

10事务处理............................................................................................................. 27

11数据伪劣............................................................................................................. 29

11.1行号 ROWNUM.......................................................................................... 29

11.2 行ID :ROWID................................................................................................ 30

12 表的创建............................................................................................................ 31

12.1常见数据类型............................................................................................ 31

12.2创建表....................................................................................................... 31

12.3 复制表...................................................................................................... 32

12.4修改表结构................................................................................................ 34

13 约束................................................................................................................... 35

13.1 非空约束.................................................................................................. 35

13.2唯一约束................................................................................................... 35

13.3主键约束................................................................................................... 36

13.4主外键约束................................................................................................ 37

14  序列.......................................................................................................... 39

15视图.................................................................................................................... 41

16 同义词................................................................................................................ 43

17索引.................................................................................................................... 43

18数据库备份.......................................................................................................... 45

19用户管理............................................................................................................. 46

20数据库设计.......................................................................................................... 47

20.1第一范式................................................................................................... 47

20.2第二范式(多对多).................................................................................. 48

20.2第三范式(多对多)、................................................................................ 49

21sybaase powerdesigner工具............................................................................ 49


          oracle基础文档

1.oracle的sqlplus

但常用的几种连接方式也就几种:

[if !supportLists]1.1 [endif]sqlplus / as sysdb


sqlplus / assysdba


[if !supportLists]1.2 [endif]sqlplus “/as sysdba”


sqlplus "/assysdba"


[if !supportLists]1.3 [endif]sqlplus username/pwd@host/service_name


sqlplustiger/scott@localhost/orcl sqlplustiger/scott@172.16.10.1:1521/orcl


[if !supportLists]1.4 [endif]sqlplus /nolog


sqlplus /nolog

conn tiger/scottconntiger/scott@172.16.0.1/orcl


2 oracle 的简单查询

1,利用select 子句控制要显示的数据列:


1select  empno,ename,ename,job,sal from emp; 

2,可以使用distinct来消除重复的数据行显示:


1select distinct  job from emp; 

3,select子句可以进行四则运算,可以直接输出常量内容,但是对于字符串使用单引号数字直接编写,日期格式按照字符格式:


1select  empno,ename,(sal*15+(200+100)) income from emp;

4,||负责输出内容连接此类的操作很少直接在查询中出现:


1select  empno||ename from emp;

5,where子句一般都写在from子句之后,但是是紧跟着from子句之后执行的,where子句控制显示数据行的操作,而select控制数据列,select子句要落后于where子句执行,所以在select子句之中定义的别名无法在where中使用。


3 oracle的限定查询

1,关系运算符:

1

2

3

4

5

select * from  emp where sal>1500;

select * from  emp where ename ='SMITH'

select  empno,ename,job from emp where job<>'SALESMAN';

2,逻辑运算符:

1

2

3

select * from  emp where sal>1500 and sal<3000;

select * from  emp where sal>2000 or job='CLERK';

select * from

  emp where not sal >=2000;

3,范围查询:

1

2

select * from  emp where sal between 1500 and 2000;

select * from

  emp where hiredate between '01-1月-1981'and'31-12月-1981';

4,空判断(空在数据库上表示不确定,如果在数据列使用null不表示0)

1select * from

  emp where comm is not null;

5,IN操作符(类似于between and 而in给出的是指定的范围):

1select * from  emp where empno in (7369,7566,7788,9999);

关于not in与null的问题:

在使用not in 进行范围判断的时候,如果范围有null,那么不会有任何结果返回。

6,模糊查询:

“-”:匹配任意一位字符;

“%”:匹配任意的0,1,,或者多位字符;

查询姓名是以字母A开头的雇员信息:

1select * from emp where ename like 'A%'

查询姓名第二个字母是A的雇员信息:

1select * from  emp where ename like '_A%';

查询姓名任意位置是A的雇员信息:

1select * from  emp where ename like '%A%';

查询排序:

ASC(默认):按照升序排列;

DESC: 按照降序排列;

查询所有的雇员信息,要求按照工资的由高到低:

1select * from

  emp order by sal desc;

查询每个雇员的编号,姓名,年薪,按照年薪由低到高排序:

1select empno

  ,ename,sal*12 income from emp order by income;

语句的执行顺序:from - where -select - order by


4 oracle查询排序

[if !supportLists]1.[endif]升序排序

【训练1】  查询雇员姓名和工资,并按工资从小到大排序。

输入并执行查询:

Sql代码

SELECT

ename, sal FROM emp ORDER BY sal; 


SELECT

ename, sal FROM emp ORDER BY sal;

执行结果为:

Sql代码

ENAME           

SAL  


------------- --------------------  


SMITH            

800  


JAMES            

950 


ENAME           

SAL


------------- --------------------


SMITH            

800


JAMES            

950

注意:若省略ASC和DESC,则默认为ASC,即升序排序。

[if !supportLists]2.[endif]降序排序

【训练2】  查询雇员姓名和雇佣日期,并按雇佣日期排序,后雇佣的先显示。

输入并执行查询:

Sql代码

SELECT

ename,hiredate FROM emp ORDER BY hiredate DESC; 


SELECT

ename,hiredate FROM emp ORDER BY hiredate DESC;

结果如下:

Sql代码

ENAME      

HIREDATE  


------------- -----------------------  


ADAMS       23-5月 -87  


SCOTT       19-4月 -87  

MILLER     

23-1月 -82  


JAMES       03-12月-81  


FORD        03-12月-81 


ENAME      

HIREDATE


------------- -----------------------


ADAMS       23-5月 -87


SCOTT      19-4月 -87

MILLER    

23-1月 -82


JAMES      03-12月-81


FORD       03-12月-81

注意: DESC表示降序排序,不能省略。

[if !supportLists]3.[endif]多列排序

可以按多列进行排序,先按第一列,然后按第二列、第三列......。

 【训练3】  查询雇员信息,先按部门从小到大排序,再按雇佣时间的先后排序。

输入并执行查询:

Sql代码

SELECT

ename,deptno,hiredate FROM emp ORDER BY deptno,hiredate; 


SELECT

ename,deptno,hiredate FROM emp ORDER BY deptno,hiredate;

结果如下:

Sql代码

ENAME      

DEPTNO HIREDATE  


---------------- ----------------- ---------------  


CLARK                 

10 09-6月 -81  


KING                  

10 17-11月-81  


MILLER                

10 23-1月 -82  


SMITH                 

20 17-12月-80  


JONES                 

20 02-4月 -81  


FORD                  

20 03-12月-81  


SCOTT                 

20 19-4月 -87 


ENAME      

DEPTNO HIREDATE


---------------- ----------------- ---------------


CLARK                 

10 09-6月 -81


KING                  

10 17-11月-81


MILLER                

10 23-1月 -82


SMITH                 

20 17-12月-80


JONES                 

20 02-4月 -81


FORD                  

20 03-12月-81


SCOTT                 

20 19-4月 -87

说明:该排序是先按部门升序排序,部门相同的情况下,再按雇佣时间升序排序。

4.在排序中使用别名

如果要对计算列排序,可以为计算列指定别名,然后按别名排序。

 【训练4】  按工资和工作月份的乘积排序。

输入并执行查询:

Sql代码

SELECT

empno, ename, sal*Months_between(sysdate,hiredate) AS total FROM

emp   


ORDER BY total; 


SELECT

empno, ename, sal*Months_between(sysdate,hiredate) AS total FROM emp


ORDER BY total;

执行结果为:

Sql代码

EMPNO

ENAME         TOTAL  


------------ ------------- ----------------------  


7876    ADAMS      

221526.006  


7369    SMITH      

222864.661  


7900    JAMES      

253680.817  


7654   MARTIN      336532.484 


 EMPNO

ENAME         TOTAL


------------ ------------- ----------------------


7876    ADAMS       221526.006


7369    SMITH      222864.661


7900    JAMES      253680.817


7654   MARTIN      336532.484

sysdate获取当前日期。


5 oracle的单行函数

5.1字符串函数:

[if !vml]

[endif]



注意SUBSTR 如果向要从后向前截取,可以使用负数来表示

例如:SUBSTR('helloword',-3),表示截取最后三个字符,不写长度.默认从开始截取到字符串的末尾.

以上函数除了INITCAP以外都可以在mysql中使用

程序中的字符串,下标从0开始,数据库中的下标从1开始,Oracle中,如果下标写0.则按照1处理,在mysql中,不会返回任何结果


[if !vml]

[endif]

[if !vml]

[endif]


[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

5.2数字函数

[if !vml]

[endif]

注意,ROUND,和TRUNC函数都可以是用以下格式ROUND(889.99,-2) 这样中格式,小数位数可以是负数,当小数位数是负数时,例子中的结果是900

在mysql中没有TRUNC函数,有功能相同的TRUNCATE函数,用法也和Oracle中的TRUNC函数相同[if !vml]

[endif]



[if !vml]

[endif]


5.3 时间函数

在日期中有如下三个操作:

日期+数字=日期(表示若干天之后的天数)

日期-数字=日期(表示若干天前的天数)

日期-日期=天数(表示两个日期相差多少天)

但是这种计算的结果不精确,在oracle中不精确,在mysql中结果会是一种错误的结果

所以给出以下日期函数


[if !vml]

[endif]


[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

5.4 转换函数

[if !vml]

[endif]


[if !vml]

[endif]

[if !vml]

[endif]

5.5通用函数

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

6.Oracle子查询

子查询可以出现在很多位置,比如: 当列、当表、当条件等

语法:

SELECT (

子查询)

FROM (

子查询)

WHERE (

子查询)

GROUP BY

子句

HAVING (

子查询)

ORDER BY

子句

注:子查询要用括号括起来。

6.1单行单列

[if !vml]

[endif]

[if !vml]

[endif]

6.2.单行多列的信息


[if !vml]

[endif]


[if !vml]

[endif]

6.3子查询返回单行多列

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]


[if !vml]

[endif]

6.4在having字句之中使用子查询

[if !vml]

[endif]

6.5在from字句中使用子查询

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]


[if !vml]

[endif]

[if !vml]

[endif]



7分组统计查询

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]


[if !vml]

[endif]


[if !vml]

[endif]


8多表查询

[if !vml]

[endif]


笛卡尔积问题:

本问题在数据库的操作之中被称为笛卡尔积,就表示多张表的数据乘积的意思,但是这种查询结果肯定不是用户所希望的,那么该如何去掉笛卡尔积呢?

最简单的方式是采用关联字段的形式,emp表和dept表之间现在存在了deptno的关联字段,所以现在可以从这个字段上的判断开始

[if !vml]

[endif]

[if !vml]

[endif]


[if !vml]

[endif]


[if !vml]

[endif]

[if !vml]

[endif]


[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]


[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

9数据库更新

[if !vml]

[endif]

9.1.1数据增加

 [if !vml]

[endif]


[if !vml]

[endif]

9.1.2数据修改

[if !vml]

[endif]

[if !vml]

[endif]


[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

9.1.3数据删除

[if !vml]

[endif]

[if !vml]

[endif]


10事务处理

[if !vml]

[endif]


[if !vml]

[endif]


[if !vml]

[endif]

[if !vml]

[endif]

11数据伪劣

11.1行号 ROWNUM

[if !vml]

[endif]


[if !vml]

[endif]

[if !vml]

[endif]


[if !vml]

[endif]

[if !vml]

[endif]

11.2 行ID :ROWID

[if !vml]

[endif]


12 表的创建


[if !vml]

[endif]

12.1常见数据类型

[if !vml]

[endif]

[if !vml]

[endif]

12.2创建表

[if !vml]

[endif]


[if !vml]

[endif]


[if !vml]

[endif]


12.3 复制表


[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]


12.4修改表结构

[if !vml]

[endif]

[if !vml]

[endif]


[if !vml]

[endif]

修改列结构

[if !vml]

[endif]

13 约束

[if !vml]

[endif]

13.1 非空约束

[if !vml]

[endif]

13.2唯一约束

[if !vml]

[endif]


[if !vml]

[endif]

[if !vml]

[endif]

13.3主键约束

[if !vml]

[endif]

[if !vml]

[endif]


[if !vml]

[endif]

13.4主外键约束

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]


14 序列

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

15视图

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]


16 同义词

[if !vml]

[endif]

[if !vml]

[endif]

17索引

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]


[if !vml]

[endif]

[if !vml]

[endif]

18数据库备份


[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]


19用户管理

[if !vml]

[endif]

[if !vml]

[endif]‘’[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

20数据库设计

20.1第一范式

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]


20.2第二范式(多对多)

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

20.2第三范式(多对多)、


[if !vml]

[endif]

[if !vml]

[endif]

[if !vml]

[endif]

21sybaase powerdesigner工具

[if !vml]

[endif]

[if !vml]

[endif]

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容

  • 1.简介 数据存储有哪些方式?电子表格,纸质文件,数据库。 那么究竟什么是关系型数据库? 目前对数据库的分类主要是...
    乔震阅读 1,616评论 0 2
  • SQL ==SQLPLUS== DML(Data Manipulation Language,数据操作语言)---...
    蝌蚪1573阅读 547评论 0 4
  • ——你走之后,我没有说出半分怀念,所看到的一切,却都是对你无限的怀念。佐佑一〇 四月是你的谎言,美如桜 短暂 难以...
    佐佑一阅读 383评论 0 1
  • 叮零零,叮零零,还在做美梦的苏沫不幸被闹钟吵醒,“哎呀,还让不让睡觉啊,真是。”苏沫顶着个鸡窝头愤怒的关掉了闹钟,...
    鹿雨萱阅读 248评论 0 1
  • 本文介绍在未安装openjdk的Ubuntu下oracle java8环境搭建 1、sudo add-apt-re...
    海外党一枚阅读 339评论 0 1