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/[email protected]:1521/orcl
[if !supportLists]1.4 [endif]sqlplus /nolog
sqlplus /nolog
conn tiger/scottconntiger/[email protected]/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]