韓順平oracle課件_第1頁
韓順平oracle課件_第2頁
韓順平oracle課件_第3頁
韓順平oracle課件_第4頁
韓順平oracle課件_第5頁
已閱讀5頁,還剩47頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、oracle的基本使用-基本命令.連接命令 1.connect用法:conn 用戶名/密碼網(wǎng)絡(luò)服務(wù)名as sysdba/sysoper當(dāng)用特權(quán)用戶身份連接時,必須帶上as sysdba或是as sysoper 2.disconnect 說明: 該命令用來斷開與當(dāng)前數(shù)據(jù)庫的連接 3.psssword說明: 該命令用于修改用戶的密碼,如果要想修改其它用戶的密碼,需要用sys/system登錄。 4.show user 說明: 顯示當(dāng)前用戶名 5.exit 說明: 該命令會斷開與數(shù)據(jù)庫的連接,同時會退出sql*plus .文件操作命令1.start和 說明: 運行sql腳本 案例: sql d:a.

2、sql或是sqlstart d:a.sql 2.edit 說明: 該命令可以編輯指定的sql腳本 案例: sqledit d:a.sql,這樣會把d:a.sql這個文件打開 3.spool 說明: 該命令可以將sql*plus屏幕上的內(nèi)容輸出到指定文件中去。 案例: sqlspool d:b.sql 并輸入 sqlspool off .交互式命令 1.&說明:可以替代變量,而該變量在執(zhí)行時,需要用戶輸入。 select * from emp where job=&job; 2.edit說明:該命令可以編輯指定的sql腳本 案例:sqledit d:a.sql 3.spool說明:該命令可以將s

3、ql*plus屏幕上的內(nèi)容輸出到指定文件中去。 spool d:b.sql 并輸入 spool off .顯示和設(shè)置環(huán)境變量概述:可以用來控制輸出的各種格式,set show如果希望永久的保存相關(guān)的設(shè)置,可以去修改glogin.sql腳本 1.linesize說明:設(shè)置顯示行的寬度,默認是80個字符 show linesize set linesize 90 2.pagesize說明:設(shè)置每頁顯示的行數(shù)目,默認是14 用法和linesize一樣 至于其它環(huán)境參數(shù)的使用也是大同小異 3.oracle用戶管理.創(chuàng)建用戶概述:在oracle中要創(chuàng)建一個新的用戶使用create user語句,一般是具

4、有dba(數(shù)據(jù)庫管理員)的權(quán)限才能使用。 create user 用戶名 identified by 密碼; (oracle有個毛病,密碼必須以字母開頭,如果以字母開頭,它不會創(chuàng)建用戶) .給用戶修改密碼概述:如果給自己修改密碼可以直接使用 password 用戶名 如果給別人修改密碼則需要具有dba的權(quán)限,或是擁有alter user的系統(tǒng)權(quán)限 sql alter user 用戶名 identified by 新密碼 .刪除用戶概述:一般以dba的身份去刪除某個用戶,如果用其它用戶去刪除用戶則需要具有drop user的權(quán)限。 比如 drop user 用戶名 【cascade】 在刪除用戶

5、時,注意: 如果要刪除的用戶,已經(jīng)創(chuàng)建了表,那么就需要在刪除的時候帶一個參數(shù) cascade; 概述:創(chuàng)建的新用戶是沒有任何權(quán)限的,甚至連登陸的數(shù)據(jù)庫的權(quán)限都沒有,需要為其指定相應(yīng)的權(quán)限。給一個用戶賦權(quán)限使用命令grant,回收權(quán)限使用命令revoke。 為了給講清楚用戶的管理,這里我給大家舉一個案例。 sql conn xiaoming/m12; error: ora-01045: user xiaoming lacks create session privilege; logon denied 警告: 您不再連接到 oracle。 sql show user; user 為 sql co

6、nn system/p; 已連接。 sql grant connect to xiaoming; 授權(quán)成功。 sql conn xiaoming/m12; 已連接。 sql 注意:grant connect to xiaoming;在這里,準(zhǔn)確的講,connect不是權(quán)限,而是角色。 看圖: 現(xiàn)在說下對象權(quán)限,現(xiàn)在要做這么件事情: * 希望xiaoming用戶可以去查詢emp表 * 希望xiaoming用戶可以去查詢scott的emp表 grant select on emp to xiaoming * 希望xiaoming用戶可以去修改scott的emp表 grant update on e

7、mp to xiaoming * 希望xiaoming用戶可以去修改/刪除,查詢,添加scott的emp表 grant all on emp to xiaoming * scott希望收回xiaoming對emp表的查詢權(quán)限 revoke select on emp from xiaoming /對權(quán)限的維護。 * 希望xiaoming用戶可以去查詢scott的emp表/還希望xiaoming可以把這個權(quán)限繼續(xù)給別人。 -如果是對象權(quán)限,就加入 with grant option grant select on emp to xiaoming with grant option 我的操作過程:

8、 sql conn scott/tiger; 已連接。 sql grant select on scott.emp to xiaoming with grant option; 授權(quán)成功。 sql conn system/p; 已連接。 sql create user xiaohong identified by m123; 用戶已創(chuàng)建。 sql grant connect to xiaohong; 授權(quán)成功。 sql conn xiaoming/m12; 已連接。 sql grant select on scott.emp to xiaohong; 授權(quán)成功。 -如果是系統(tǒng)權(quán)限。 syste

9、m給xiaoming權(quán)限時: grant connect to xiaoming with admin option 問題:如果scott把xiaoming對emp表的查詢權(quán)限回收,那么xiaohong會怎樣? 答案:被回收。 下面是我的操作過程: sql conn scott/tiger; 已連接。 sql revoke select on emp from xiaoming; 撤銷成功。 sql conn xiaohong/m123; 已連接。 sql select * from scott.emp;select * from scott.emp * 第 1 行出現(xiàn)錯誤: ora-0094

10、2: 表或視圖不存在 結(jié)果顯示:小紅受到誅連了。 .使用 profile管理用戶口令概述:profile是口令限制,資源限制的命令集合,當(dāng)建立數(shù)據(jù)庫的,oracle會自動建立名稱為default的profile。當(dāng)建立用戶沒有指定profile選項,那么oracle就會將default分配給用戶。 1.賬戶鎖定概述:指定該賬戶(用戶)登陸時最多可以輸入密碼的次數(shù),也可以指定用戶鎖定的時間(天)一般用dba的身份去執(zhí)行該命令。例子:指定scott這個用戶最多只能嘗試3次登陸,鎖定時間為2天,讓我們看看怎么實現(xiàn)。 創(chuàng)建profile文件 sql create profile lock_accoun

11、t limit failed_login_attempts 3 password_lock_time 2; sql alter user scott profile lock_account; 2.給賬戶(用戶)解鎖 sql alter user tea account unlock; 3.終止口令為了讓用戶定期修改密碼可以使用終止口令的指令來完成,同樣這個命令也需要dba的身份來操作。 例子:給前面創(chuàng)建的用戶tea創(chuàng)建一個profile文件,要求該用戶每隔10天要修改自己的登陸密碼,寬限期為2天??纯丛趺醋?。 sql create profile myprofile limit passwo

12、rd_life_time 10 password_grace_time 2; sql alter user tea profile myprofile; 口令歷史概述:如果希望用戶在修改密碼時,不能使用以前使用過的密碼,可使用口令歷史,這樣oracle就會將口令修改的信息存放到數(shù)據(jù)字典中,這樣當(dāng)用戶修改密碼時,oracle就會對新舊密碼進行比較,當(dāng)發(fā)現(xiàn)新舊密碼一樣時,就提示用戶重新輸入密碼。 例子: 1)建立profile sqlcreate profile password_history limit password_life_time 10 password_grace_time 2 p

13、assword_reuse_time 10 password_reuse_time /指定口令可重用時間即10天后就可以重用 2)分配給某個用戶 .刪除 profile概述:當(dāng)不需要某個profile文件時,可以刪除該文件。 sql drop profile password_history 【casade】 注意:文件刪除后,用這個文件去約束的那些用戶通通也都被釋放了。 加了casade,就會把級聯(lián)的相關(guān)東西也給刪除掉 4.oracle表的管理(數(shù)據(jù)類型,表創(chuàng)建刪除,數(shù)據(jù) crud操作)oracle的表的管理 表名和列的命名規(guī)則 .必須以字母開頭 .不能使用oracle的保留字.長度不能超過

14、30個字符.只能使用如下字符 a-z,a-z,0-9,$,#等 oracle支持的數(shù)據(jù)類型 . 字符類 char 定長 最大2000個字符。 例子:char(10) 小韓前四個字符放小韓,后添6個空格補全 如小韓 varchar2(20) 變長 最大4000個字符。 例子:varchar2(10) 小韓 oracle分配四個字符。這樣可以節(jié)省空間。 clob(character large object) 字符型大對象 最大4g char 查詢的速度極快浪費空間,查詢比較多的數(shù)據(jù)用。 varchar 節(jié)省空間 數(shù)字型.number范圍 -10的38次方 到 10的38次方 可以表示整數(shù),也可以

15、表示小數(shù) number(5,2) 表示一位小數(shù)有5位有效數(shù),2位小數(shù) 范圍:-999.99到999.99 number(5) 表示一個5位整數(shù) 范圍99999到-99999 . 日期類型 date 包含年月日和時分秒 oracle默認格式 1-1月-1999 timestamp 這是oracle9i對date數(shù)據(jù)類型的擴展??梢跃_到毫秒。 圖片.blob 二進制數(shù)據(jù) 可以存放圖片/聲音 4g 一般來講,在真實項目中是不會把圖片和聲音真的往數(shù)據(jù)庫里存放,一般存放圖片、視頻的路徑,如果安全需要比較高的話,則放入數(shù)據(jù)庫。 建表.-學(xué)生表 create table student ( -表名 xh

16、number(4), -學(xué)號 xm varchar2(20), -姓名 sex char(2), -性別 birthday date, -出生日期 sal number(7,2) -獎學(xué)金 );-班級表 create table class( classid number(2), cname varchar2(40) ); 修改表 添加一個字段. sqlalter table student add (classid number(2); . 修改一個字段的長度 sqlalter table student modify (xm varchar2(30); . 修改字段的類型/或是名字(不能有

17、數(shù)據(jù)) 不建議做 sqlalter table student modify (xm char(30); 刪除一個字段 不建議做(刪了之后,順序就變了。加就沒問題,應(yīng)為是加在后面). sqlalter table student drop column sal; 修改表的名字 很少有這種需求. sqlrename student to stu; 刪除表. sqldrop table student; 添加數(shù)據(jù) 所有字段都插入數(shù)據(jù).insert into student values (a001, 張三, 男, 01-5月-05, 10); oracle中默認的日期格式dd-mon-yy dd日

18、子(天) mon 月份 yy 2位的年 09-6月-99 1999年6月9日 修改日期的默認格式(臨時修改,數(shù)據(jù)庫重啟后仍為默認;如要修改需要修改注冊表) alter session set nls_date_format =yyyy-mm-dd; 修改后,可以用我們熟悉的格式添加日期類型: insert into student values (a002, mike, 男, 1905-05-06, 10); 插入部分字段.insert into student(xh, xm, sex) values (a003, john, 女); 插入空值. insert into student(xh,

19、 xm, sex, birthday) values (a004, martin, 男, null); 問題來了,如果你要查詢student表里birthday為null的記錄,怎么寫sql呢? 錯誤寫法:select * from student where birthday = null; 正確寫法:select * from student where birthday is null; 如果要查詢birthday不為null,則應(yīng)該這樣寫: select * from student where birthday is not null; 修改數(shù)據(jù) 修改一個字段. update stu

20、dent set sex = 女 where xh = a001; 修改多個字段. update student set sex = 男, birthday = 1984-04-01 where xh = a001; 修改含有null值的數(shù)據(jù) 不要用 = null 而是用 is null; select * from student where birthday is null; 刪除數(shù)據(jù). delete from student; 刪除所有記錄,表結(jié)構(gòu)還在,寫日志,可以恢復(fù)的,速度慢。 delete 的數(shù)據(jù)可以恢復(fù)。 savepoint a; -創(chuàng)建保存點 delete from stude

21、nt; rollback to a; -恢復(fù)到保存點 一個有經(jīng)驗的dba,在確保完成無誤的情況下要定期創(chuàng)建還原點。 drop table student; -刪除表的結(jié)構(gòu)和數(shù)據(jù); delete from student where xh = a001; -刪除一條記錄; truncate table student; -刪除表中的所有記錄,表結(jié)構(gòu)還在,不寫日志,無法找回刪除的記錄,速度快。 5. oracle表查詢(1)oracle表基本查詢 在我們講解的過程中我們利用scott用戶存在的幾張表(emp,dept)為大家演示如何使用select語句,select語句在軟件編程中非常有用,希望大

22、家好好的掌握。 emp 雇員表 clerk 普員工 salesman 銷售 manager 經(jīng)理 analyst 分析師 president 總裁 mgr 上級的編號 hiredate 入職時間 sal 月工資 comm 獎金 deptno 部門 dept部門表 deptno 部門編號 accounting 財務(wù)部 research 研發(fā)部 operations 業(yè)務(wù)部 loc 部門所在地點 salgrade 工資級別 grade 級別 losal 最低工資 hisal 最高工資 查看表結(jié)構(gòu). desc emp; 查詢所有列. select * from dept; 切忌動不動就用select

23、 * set timing on; 打開顯示操作時間的開關(guān),在下面顯示查詢時間。 create table users(userid varchar2(10), uname varchar2 (20), upassw insert into users values(a0001, 啊啊啊啊, aaaaaaaaaaaaaaaaaaaaaaa); -從自己復(fù)制,加大數(shù)據(jù)量 大概幾萬行就可以了 可以用來測試sql語句執(zhí)行效率 insert into users (userid,uname,upassw) select * from users; select count (*) from users

24、;統(tǒng)計行數(shù) 查詢指定列. select ename, sal, job, deptno from emp; 如何取消重復(fù)行distinct . select distinct deptno, job from emp; ?查詢smith所在部門,工作,薪水 select deptno,job,sal from emp where ename = smith; 注意:oracle對內(nèi)容的大小寫是區(qū)分的,所以ename=smith和ename=smith是不同的 使用算術(shù)表達式 . nvl null 問題:如何顯示每個雇員的年工資? select sal*13+nvl(comm, 0)*13 年薪

25、 , ename, comm from emp; 使用列的別名.select ename 姓名, sal*12 as 年收入 from emp; 如何處理null值 .使用nvl函數(shù)來處理 如何連接字符串(|). select ename | is a | job from emp; 使用where子句 .問題:如何顯示工資高于3000的 員工? select * from emp where sal 3000; 問題:如何查找1982.1.1后入職的員工? select ename,hiredate from emp where hiredate 1-1月-1982; 問題:如何顯示工資在2

26、000到3000的員工? select ename,sal from emp where sal =2000 and sal 500 or job = manager) and ename like j%; 使用order by . 字句 默認asc 問題:如何按照工資的從低到高的順序顯示雇員的信息? select * from emp order by sal; 問題:按照部門號升序而雇員的工資降序排列 select * from emp order by deptno, sal desc; 使用列的別名排序.問題:按年薪排序 select ename, (sal+nvl(comm,0)*1

27、2 年薪 from emp order by 年薪 asc; 別名需要使用“”號圈中,英文不需要“”號 clear 清屏命令 oracle表復(fù)雜查詢 數(shù)據(jù)分組 max,min, avg, sum, count 問題:如何顯示所有員工中最高工資和最低工資? select max(sal),min(sal) from emp e; 最高工資那個人是誰? 錯誤寫法:select ename, sal from emp where sal=max(sal); 正確寫法:select ename, salfrom empwhere sal=(select max(sal) fromemp);注意:sel

28、ect ename, max(sal) from emp;這語句執(zhí)行的時候會報錯,說ora-00937:非單組分組函數(shù)。因為max是分組函數(shù),而ename不是分組函數(shù).但是select min(sal), max(sal) from emp;這句是可以執(zhí)行的。因為min和 max都是分組函數(shù),就是說:如果列里面有一個分組函數(shù),其它的都必須是分組函數(shù),否則就出錯。這是語法規(guī)定的 問題:如何顯示所有員工的平均工資和工資總和? 問題:如何計算總共有多少員工問題:如何 擴展要求: 查詢最高工資員工的名字,工作崗位 select ename, job, sal from emp e where sal

29、= (select max(sal) from emp);顯示工資高于平均工資的員工信息 select * from emp e where sal (select avg(sal) from emp); group by 和 having子句 .group by用于對查詢的結(jié)果分組統(tǒng)計, having子句用于限制分組顯示結(jié)果。 問題:如何顯示每個部門的平均工資和最高工資? select avg(sal), max(sal), deptno from emp group by deptno; (注意:這里暗藏了一點,如果你要分組查詢的話,分組的字段deptno一定要出現(xiàn)在查詢的列表里面,否則會

30、報錯。因為分組的字段都不出現(xiàn)的話,就沒辦法分組了) 問題:顯示每個部門的每種崗位的平均工資和最低工資? select min(sal), avg(sal), deptno, job from emp group by deptno, job; 問題:顯示平均工資低于2000的部門號和它的平均工資? select avg(sal), max(sal), deptno from emp group by deptno having avg(sal) 2000; 對數(shù)據(jù)分組的總結(jié).1 分組函數(shù)只能出現(xiàn)在選擇列表、having、order by子句中(不能出現(xiàn)在where中) 2 如果在select語

31、句中同時包含有g(shù)roup by, having, order by 那么它們的順序是group by, having, order by 3 在選擇列中如果有列、表達式和分組函數(shù),那么這些列和表達式必須有一個出現(xiàn)在group by 子句中,否則就會出錯。 如select deptno, avg(sal), max(sal) from emp group by deptno having avg(sal) select * from salgrade; grade losal hisal 1 700 1200 2 1201 1400 3 1401 2000 4 2001 3000 5 3001

32、9999 select e.ename, e.sal, s.grade from emp e, salgrade s where e.sal between s.losal and s.hisal; 擴展要求: 問題:顯示雇員名,雇員工資及所在部門的名字,并按部門排序? select e.ename, e.sal, d.dname from emp e, dept d where e.deptno = d.deptno order by e.deptno; (注意:如果用group by,一定要把e.deptno放到查詢列里面) 自連接.自連接是指在同一張表的連接查詢 問題:顯示某個員工的上級

33、領(lǐng)導(dǎo)的姓名? 比如顯示員工ford的上級 select worker.ename, boss.ename from emp worker,emp boss where worker.mgr = boss.empno and worker.ename = ford; 子查詢 子查詢是指嵌入在其他sql語句中的select語句,也叫嵌套查詢。 . 單行子查詢 單行子查詢是指只返回一行數(shù)據(jù)的子查詢語句 請思考:顯示與smith同部門的所有員工? 思路: 1 查詢出smith的部門號: select deptno from emp where ename = smith; 2 顯示: select *

34、 from emp where deptno = (select deptno from emp where ename = smith); 數(shù)據(jù)庫在執(zhí)行sql 是從左到右掃描的, 如果有括號的話,括號里面的先被優(yōu)先執(zhí)行。 多行子查詢.多行子查詢指返回多行數(shù)據(jù)的子查詢 請思考:如何查詢和部門10的工作相同的雇員的名字、崗位、工資、部門號 select distinct job from emp where deptno = 10; select * from emp where job in (select distinct job from emp where deptno = 10); (

35、注意:不能用job=.,因為等號=是一對一的) 在多行子查詢中使用all操作符 .問題:如何顯示工資比部門30的所有員工的工資高的員工的姓名、工資和部門號? select ename, sal, deptno from emp where sal all (select sal from emp where deptno = 30); 擴展要求: 大家想想還有沒有別的查詢方法。 select ename, sal, deptno from emp where sal (select max(sal) from emp where deptno = 30); 執(zhí)行效率上, 函數(shù)高得多 在多行子查

36、詢中使用any操作符 .問題:如何顯示工資比部門30的任意一個員工的工資高的員工姓名、工資和部門號? select ename, sal, deptno from emp where sal any (select sal from emp where deptno = 30); 擴展要求: 大家想想還有沒有別的查詢方法。 select ename, sal, deptno from emp where sal (select min(sal) from emp where deptno = 30); 多列子查詢.單行子查詢是指子查詢只返回單列、單行數(shù)據(jù),多行子查詢是指返回單列多行數(shù)據(jù),都是針

37、對單列而言的,而多列子查詢是指查詢返回多個列數(shù)據(jù)的子查詢語句。請思考如何查詢與smith的部門和崗位完全相同的所有雇員。 select deptno, job from emp where ename = smith; select * from emp where (deptno, job) = (select deptno, job from emp where ename = smith); 在from子句中使用子查詢 .請思考:如何顯示高于自己部門平均工資的員工的信息 思路: 1. 查出各個部門的平均工資和部門號 select deptno, avg(sal) mysal from e

38、mp group by deptno; 2. 把上面的查詢結(jié)果看做是一張子表 select e.ename, e.deptno, e.sal, ds.mysal from emp e, (select deptno, avg(sal) mysal from emp group by deptno) ds where e.deptno = ds.deptno and e.sal ds.mysal; 小總結(jié): 在這里需要說明的當(dāng)在from子句中使用子查詢時,該子查詢會被作為一個視圖來對待,因此叫做內(nèi)嵌視圖,當(dāng)在from子句中使用子查詢時,必須給子查詢指定別名。 注意:別名不能用as,如:selec

39、t e.ename, e.deptno, e.sal, ds.mysal from emp e, (select deptno, avg(sal) mysal from emp group by deptno) as ds where e.deptno = ds.deptno and e.sal ds.mysal; 在ds前不能加as,否則會報錯 (給表取別名的時候,不能加as;但是給列取別名,是可以加as的) 分頁查詢.按雇員的id號升序取出 oracle的分頁一共有三種方式 1.根據(jù)rowid來分 select * from t_xiaoxi where rowid in (select

40、rid from (select rownum rn, rid from(select rowid rid, cid from t_xiaoxi order by cid desc) where rownum9980) order by cid desc; 執(zhí)行時間0.03秒 2.按分析函數(shù)來分 select * from (select t.*, row_number() over(order by cid desc) rk from t_xiaoxi t) where rk9980; 執(zhí)行時間1.01秒 3.按rownum來分 select * from (select t.*,rownu

41、m rn from(select * from t_xiaoxi order by cid desc)t where rownum9980; 執(zhí)行時間0.1秒 其中t_xiaoxi為表名稱,cid為表的關(guān)鍵字段,取按cid降序排序后的第9981-9999條記錄,t_xiaoxi表有70000多條記錄。 個人感覺1的效率最好,3次之,2最差。 /測試通過的分頁查詢okokok select * from (select a1.*, rownum rn from(select ename,job from emp) a1 where rownum=5; 下面最主要介紹第三種:按rownum來分 1

42、. rownum 分頁 select * from emp; 2. 顯示rownumoracle分配的 select e.*, rownum rn from (select * from emp) e; rn相當(dāng)于oracle分配的行的id號 3.挑選出610條記錄 先查出1-10條記錄 select e.*, rownum rn from (select * from emp) e where rownum =6是不行的, 4. 然后查出6-10條記錄 select * from (select e.*, rownum rn from (select * from emp) e where

43、rownum = 6; 5. 幾個查詢變化 a. 指定查詢列,只需要修改最里層的子查詢 只查詢雇員的編號和工資 select * from (select e.*, rownum rn from (select ename, sal from emp) e where rownum = 6; b. 排序查詢,只需要修改最里層的子查詢 工資排序后查詢6-10條數(shù)據(jù) select * from (select e.*, rownum rn from (select ename, sal from emp order by sal) e where rownum = 6; 用查詢結(jié)果創(chuàng)建新表.這個命

44、令是一種快捷的建表方式 create table mytable (id, name, sal, job, deptno) as select empno, ename, sal, job, deptno from emp; 創(chuàng)建好之后,desc mytable;和select * from mytable;看看結(jié)果如何? 合并查詢.有時在實際應(yīng)用中,為了合并多個select語句的結(jié)果,可以使用集合操作符號 union,union all,intersect,minus 多用于數(shù)據(jù)量比較大的數(shù)據(jù)局庫,運行速度快。 1). union 該操作符用于取得兩個結(jié)果集的并集。當(dāng)使用該操作符時,會自動去

45、掉結(jié)果集中重復(fù)行。 select ename, sal, job from emp where sal 2500 union select ename, sal, job from emp where job = manager; 2).union all 該操作符與union相似,但是它不會取消重復(fù)行,而且不會排序。 select ename, sal, job from emp where sal 2500 union all select ename, sal, job from emp where job = manager; 該操作符用于取得兩個結(jié)果集的并集。當(dāng)使用該操作符時,會自動

46、去掉結(jié)果集中重復(fù)行。 3). intersect 使用該操作符用于取得兩個結(jié)果集的交集。 select ename, sal, job from emp where sal 2500 intersect select ename, sal, job from emp where job = manager; 4). minus (minus就是減法的意思)使用改操作符用于取得兩個結(jié)果集的差集,他只會顯示存在第一個集合中,而不存在第二個集合中的數(shù)據(jù)。 select ename, sal, job from emp where sal 2500 minus select ename, sal, j

47、ob from emp where job = manager; 創(chuàng)建數(shù)據(jù)庫有兩種方法: 1). 通過oracle提供的向?qū)Чぞ?。database configuration assistant 【數(shù)據(jù)庫配置助手】 2) .我們可以用手工步驟直接創(chuàng)建。 7.java操作 oracle1.上節(jié)回顧 2.java程序如何操作oracle 3.如何在oracle中操作數(shù)據(jù) 4.oracle事務(wù)處理 5.sql函數(shù)的使用 期望目標(biāo) 1.掌握oracle表對數(shù)據(jù)操作技巧 2.掌握在java程序中操作oracle 3.理解oracle事物概念 4.掌握oracle各種sql函數(shù) java連接oracle

48、下面我們舉例說明,寫一個java,分頁顯示emp表的用戶信息。 java代碼 (1)1. package com.sp; 2. 3. import java.sql.connection; 4. import java.sql.drivermanager; 5. import java.sql.resultset; 6. import java.sql.statement; 7.8./演示 如何使用 jdbc_odbc橋連接方式 9. public class testoracle 10. 11. public static void main(string args) 12. try 13.

49、 14. / 1.加載驅(qū)動 15. class.forname(sun.jdbc.odbc.jdbcodbcdriver); 16. 17. / 2.得到連接 18. connection ct = drivermanager.getconnection( 19. jdbc.odbc:testconnectoracle, scott, 21.tiger); 22. 23. / 從下面開始,和sql server一模一樣 24. statement sm = ct.createstatement(); 25. resultset rs = sm.executequery(select * fro

50、m emp) ; 26. while (rs.next() 27. /用戶名 28. system.out.println(用戶名: +rs.getstring(2); 29. /默認是從1開始編號的 30. 31. catch (exception e) 32. e.printstacktrace(); 33. 34. 35. 在得到連接那里,要去配置數(shù)據(jù)源,點擊控制面板-系統(tǒng)和安全-管理工具-數(shù)據(jù)源(odbc), 注:在windows7中使用odbcab32在dos運行打開odbc這里要注意:jdbcodbc能不能遠程連接呢?不能遠程連接,也就是你這樣寫的話就意味著java程 序和orac

51、le數(shù)據(jù)庫應(yīng)該是在同一臺機器上,因為這里沒有指定ip地址,肯定默認就是本地。 如果要遠程連,就用jdbc,jdbc是可以遠程連的。 運行testoracle.java,控制臺輸出. 可惜我沒運行成功,說 java.sql.sqlexception: no suitable driver found for jdbc.odbc:testconnectoracle at java.sql.drivermanager.getconnection(unknown source) at java.sql.drivermanager.getconnection(unknown source) at com.sp.testoracle.main(testoracle.java:18) 不知道為什么。 接下來講解用jdbc的方式連接oracle 記得要把驅(qū)動包引入,classes12.

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論