*********************************
**? oracle 學(xué)習(xí)筆記第一天????? **
**? author Ice Xu?? (XuBin)??? **
**? date? 2006-10-30?????????? **
***********************************
初始化表的位置:
cd $ORACLE_HOME/rdbms?? cd demo???? summit2.sql
這個(gè)角本可以初始化練習(xí)用的表
set? LANG = AMERICAN_AMERICA.US7ASCII
*********************************
我們目前使用的是oralce 9i?? 9201 版本
恢復(fù)練習(xí)表命令:
sqlplus? openlab/open123 @summit2.sql
登陸oracle的命令:
sqlplus?? 用戶名/密碼
show?? user??????? 顯示當(dāng)前登陸的身份.
set??? pause on
set??? pause off?? 分頁顯示.
oracle中默認(rèn)日期和字符是左對齊,數(shù)字是右對齊
table or view does? not? exist ; 表或示圖不存在
edit 命令用于自動(dòng)打開vi修改剛修執(zhí)行過的sql的命令。
修改方法二:
l? 3 先定位到行??? c?? /舊串/新串
執(zhí)行出錯(cuò)時(shí),利用錯(cuò)誤號來查錯(cuò)誤:
!oerr ora 942? (裝完系統(tǒng)后會(huì)裝一個(gè)oerr工具,用于通過錯(cuò)誤號來查看錯(cuò)
誤的具體信息)
想在sql中執(zhí)行unix命令時(shí),把所有的命令前加一個(gè)!就可以, 或者h(yuǎn)ost( 用
于從sql從切換至unix環(huán)境中去)
/*** 初次使用時(shí)注意? ****
運(yùn)行角本時(shí)的命令:
先切換到unix環(huán)境下,cd $oracle_home?? cd sqlplus? cd demo 下面有兩
個(gè)角本建表語句。
@demobld.sql
sqlplus nanjing/nanjing @demobid.sql 直接運(yùn)行角本,后面跟當(dāng)前目錄或
者是絕對路徑
保存剛才的sql語句:?? save 命令???? 第二次保存時(shí)要替換之前的角本
save 文件名?? replace
把剛才保的sql重新放入? buffer中
spool? 文件名
此命令會(huì)把所有的操作存在某個(gè)文件中去
spool off
練習(xí)1:查看s_emp表中員工的年工資
select? first_name? , salary*12? salary from s_emp;
給列起別名的命令:
利用關(guān)鍵字? as? 或者用空格? "別名"? 雙引號內(nèi)大小寫敏感保持引號內(nèi)容
原樣輸出,如果不加雙引號時(shí),默認(rèn)為大寫
拼接字段:
select?? first_name||last_name? "employees"? from?? s_emp ;
oracle中表達(dá)字符串用單引號來表達(dá):
select first_name||' '||last_name? from?? s_emp;(在兩個(gè)字段之間拼接
一個(gè)空格)
查看當(dāng)前用戶所有的表:
練習(xí)2:(常用于批量更改數(shù)據(jù))
set? echo off
spool? selecttab.sql;
select 'select * from ' || table_name ||' ; ' "table name " from
user_tables;
spool off;
set? head off(去除第一行)
set? feed off(去除最后一行)
練習(xí)3:(查出s_emp表中所有員工的一年的總收入)
select first_name , salary*12*( 1+nvl(commission_pct/100 , 0 ) ) "
year salary " from s_emp;
nvl函數(shù) 專用于處理空值的影響.
*******************************************************************
***************************************************************
下午:
column? 定義格式化輸出
column last_name? Heading?? format a15;
column last_name;
column salary justify left format $99,999.00? ( 定義工資的顯示形式
)
$ echo $LANG
zh_CN.hp15CN
$ echo $NLS_LANG
simplified chinese_china.zhs16cgbk
ORDER BY 排序? 升序和降序?? ASC? 升序(默認(rèn))??? DESC 降序
select * from s_emp? order by dept_id , salary desc? 部門號升序,工
資降序
關(guān)鍵字distinct也會(huì)觸發(fā)排序操作。
過濾操作:? where 子句
select * from s_emp? where dept_id=42;? 查看部門號為42的所有員工
select * from s_emp? where salary>1000? 查看工資高于1000的所有員工
select salary from? s_emp where first_name='Geroge'? 找出名字為
Geroge的員工的工資數(shù)
select? table_name from? user_tables? where table_name='S_EMP';? 查
某個(gè)具體表名時(shí),表名的字符串必須要為大寫
日期的默認(rèn)的格式? DD-MON-RR(天-月-年)
BETWEEN? AND?? 在什么之間??????????? NOT??????? BETWEEN????? AND???
??????????? 注意區(qū)間:[? ]是一個(gè)閉區(qū)間
IN( LIST)????? 在某個(gè)集合中????????? NOT??????? IN???????? (list)
空值會(huì)有影響???????? (等于list其中任何一個(gè)就行,為提高效率常把比例
高的放在前面)
LIKE?????????? 模糊配置????????????? NOT??????? LIKE?????????????
通配比較
IS NULL??????? 是空
AND
OR
NOT
練習(xí)4:(找出表名以S_開頭的所有表)對于一些特殊字符,要用到escape轉(zhuǎn)義,
并不是一定要用\,escape后面定義是什么字符為轉(zhuǎn)義字符,那就用哪個(gè)字符
select? table_name from user_tables where? table_name like?? 'S\_%'
?escape '\';
當(dāng)有多個(gè)條件時(shí),要用邏輯運(yùn)算符:AND OR
寫對where語句:正確的數(shù)據(jù)類型判斷、邏輯運(yùn)算符
sql函數(shù)的作用:
sql函數(shù)的分類:單行函數(shù)、多行函數(shù)
單行函數(shù): (dual?? 啞表 )
字符函數(shù):
lower????? 轉(zhuǎn)小寫????????? select? lower('SQLPLUS')? from dual;-->
對純字符串處理的時(shí)候
upper????? 轉(zhuǎn)大寫????????? select? upper('sqlplus')? from dual;
initcap??? 首字符大寫????? select? initcap('tarena') from dual;
concat???? 連接字符串????? select? concat(first_name , last_name)??
from s_emp;等效于||
substr???? 求子串????????? select? substr('tarenasd0603' ,1,6) from
dual; (取前六個(gè)字符)?? select substr('tarenasd0603',-2) from dual;
(取后兩個(gè)字符)
length???? 求字符長度????? select? length('tarena') from dual;
nvl??????? 空值函數(shù)??? 兩個(gè)參數(shù)的類型要匹配,統(tǒng)一的,表示:如果有,
則返回前面的參數(shù),如果沒有就返回后面的參數(shù)
eg:select first_name,salary from s_emp where lower(first_name)
='george';
select? first_name , substr(first_name , -2 ) from? s_emp;? (查出
s_emp表中所有用戶名字的最后兩個(gè)字符)
默認(rèn)的是從左向右,如果是-2則表示從右向左數(shù)
練習(xí)5:?? select?? first_name? , salary? from s_emp?? where? lower
(first_name)='george';
數(shù)值函數(shù):
round 函數(shù)(四舍五入)?? select? round(45.935, 2) from dual;?? 不帶參
數(shù)時(shí)默認(rèn)為0位小數(shù)
trunc 函數(shù)(截取,不管后面的數(shù)字)??? select? trunc(45.995, 1) from
dual;
日期函數(shù):DD-MON-RR ,默認(rèn)不顯示世紀(jì)、時(shí)、分、秒?????? 日期格式敏感
世紀(jì)、年、月、日
sysdate 返回當(dāng)前系統(tǒng)時(shí)間?????? select sysdate from dual;????
更改當(dāng)前會(huì)話的設(shè)置格式:?????
alter session set nls_date_format='yyyy mm dd hh24:mi:ss';
select? sysdate-1, sysdate+1, sysdate , sysdate+1 from dual;? 注意
單位是以天為單位,也可以得到多少小時(shí)、多少分鐘之后的時(shí)間
MONTHS_BETWEEN (DATE1 , DATE2 ) 求兩個(gè)日期之前相差的月數(shù)
add_months(date , 4 ) 在 date上再添加4個(gè)月
select round(last_day(sysdate),'month') from dual;
select? next_day(sysdate,'FRIDAY') from dual ; 求這個(gè)日期的下一個(gè)
FRIDAY
last_day 求月的最后一天
round 函數(shù):???? select?? round(sysdate, 'MONTH') from dual;???? 參
數(shù)可以為:? MONTH YEAR(看上半年還是下半年)??
select? trunc(last_day(sysdate)+1)? from? dual;
select? add_months(trunc(sysdate, 'MONTH'), 1 )? from? dual ;
關(guān)于日期的兩種形式:
轉(zhuǎn)換函數(shù):
to_char顯示日期:
從數(shù)字轉(zhuǎn)化為char? to_char(date,'格式')
從日期轉(zhuǎn)化為char?????????? to_char(date,? 'fmt' )????????????
select to_char(sysdate, 'yyyy mm dd hh24:mi:ss') from dual;
?????????
?????????
?????????
????select to_char(sysdate, 'fmyyyy mm
dd hh24:mi:ss') from dual;去掉前導(dǎo)名
????????????????????????????? select? to_char(sysdate ,'YEAR MONTH
dy
eg:查出三月分入職的員工:select first_name,start_date from s_emp
where to_char(start_date,'mm')='03';
to_date表達(dá)日期:
????? 字符轉(zhuǎn)日期???? select?? to_date('2000 11 20', 'yyyy mm dd ')?
from dual;
?????????????????????????????? select? round(to_date('10-OCT-06'
,'dd-mon-RR') ) from?? dual;
to_number
????? 字符轉(zhuǎn)數(shù)字
??????????????????????????? select to_number('10')? from? dual ;
day2
where 條件一定是根據(jù)某個(gè)字段來進(jìn)行過濾操作.
多表連接操作:
兩表沒有任何關(guān)聯(lián)時(shí)會(huì)產(chǎn)生迪卡爾機(jī):
select?? first_name , name? from??? s_emp , s_dept;
等值連接:
練習(xí)一:查看員工的姓名和員工部門號:(要考慮到表中實(shí)際數(shù)據(jù)中空值的影響)
select?? first_name ,?? name from? s_emp e, s_dept? d where e.dept_id=d.id;同時(shí)起了別名
select?? first_name ,?? name from? s_emp e, s_dept? d where e.dept_id=d.id and e.first_name='George';具體到哪個(gè)人所在的部門
練習(xí)二:每個(gè)員工所在的部門和部門所在的地區(qū)
select first_name , name?? from s_emp, s_dept,? s_region? where? s_emp.dept_id=s_dept.id and s_dept.region_id=s_region.id;
eg:select first_name,d.name,r.name
?from s_emp e,s_dept d,s_region r
?where e.dept_id=d.id and d.region_id=r.id;
等值連接:
練習(xí)三:找出每個(gè)員工和每個(gè)員工的工資級別
? select??? a.ename , a.sal, b.grade from emp a , salgrade b? where a.sal between b.losal and b.hisal;
? select??? a.ename , a.sal, b.grade from? emp a , salgrade b? where a.sal>=b.losal? and? a.sal<=b.hisal;
自連接:當(dāng)一個(gè)表的插入行之間有了關(guān)系時(shí)就發(fā)生了(又名:內(nèi)連接)
select?? first_name?? , manager_id?? from? s_emp;
查出所有員工的部門領(lǐng)導(dǎo)的名稱:( 這種sql會(huì)少一條記錄,總經(jīng)理沒有被配置上)
select? e.first_name , m.first_name?? from s_emp e , s_emp m? where?? e.manager_id=m.id;
外連接:(防止空值時(shí),用(+)的一方會(huì)模擬一條記錄配置另一方)這就稱為外連接,一個(gè)記錄都不能少;
select? e.first_name , m.first_name?? from s_emp e , s_emp m? where?? e.manager_id=m.id(+);
+號放在哪邊就表示在哪邊補(bǔ)空,來跟對方來匹配,使得數(shù)據(jù)一個(gè)都不會(huì)漏掉,這個(gè)例子中的領(lǐng)導(dǎo)有可能會(huì)沒有(最高領(lǐng)導(dǎo)就再?zèng)]有領(lǐng)導(dǎo)了,所以就
方法領(lǐng)導(dǎo)的那邊)
?標(biāo)準(zhǔn)寫法:select e.deptno,d.name from emp e,dept d where e.deptno(+)=d.depton and e.depton is null;
查看員工分部的部門:
select? distinct(deptno) from emp ;
找出沒有員工的部門:(很經(jīng)典的一個(gè)例子,用外連接來解決的標(biāo)準(zhǔn)做法,這是一種方式)
第一步:
select???? e.deptno , d.deptno? from emp e , dept d? where? e.deptno(+)=d.deptno;
第二步:
select???? e.deptno , d.deptno? from emp e , dept d? where? e.deptno(+)=d.deptno?? and?? e.deptno is null;
組函數(shù)(group function):
group by? 分組子句??? 對分組后的子句進(jìn)行過濾還可以用having??? 條件? 對分組后的條件進(jìn)行過濾?? where 是對記錄進(jìn)行過濾
avg(distinct | all )求平均值????????????????????????????
count(distinct | all )統(tǒng)計(jì)
max(distinct | all ) 求最大值
min(distinct | all )求最小值
sum(distinct | all )? 求和
(所有組函數(shù)會(huì)忽略空值 , avg?? sum只能作用于數(shù)字類型)
求有提成員工的提成的平均值;
select??? avg(nvl(commission_pct ,0 )? ) from s_emp;
有多少人有提成:
select? count( commission_pct ) from??? s_emp ;
count(*)? 用于統(tǒng)計(jì)記錄數(shù):
select?? sum(commission_pct)/ count(*)?? from???? s_emp;
?員工分部在多少個(gè)不同的部門:count? 默認(rèn)為作all的動(dòng)作
?select?? count(dept_id)? from s_emp;
?select?? count(distinct dept_id) from?? s_emp;
?求各個(gè)部門的平均工資:group? by? 子句也會(huì)觸發(fā)排序
?select? dept_id ,? avg(salary) aa??? from??? s_emp??? group by?? dept_id?? order by? aa ;
?select? dept_id ,? avg(salary) aa??? from??? s_emp??? group by?? dept_id??? ;
?注意:group by 子句后面跟有條件只能是查詢的結(jié)果中的字段,所以我們會(huì)人為在結(jié)果要加入一些group by? 要用的字段
select?? region_id , count(*)? from? s_dept 此句會(huì)有錯(cuò)
select?? max(region_id)? , count(*) from?????? s_dept;? (強(qiáng)制語法上可以正確,但是不能保證結(jié)果也會(huì)正確)
求各個(gè)部門不同工種的平均工資:
select???? dept_id , title,? avg(salary)? from s_emp?? group?? by dept_id , title? ;
哪些部門的平均工資比2000高:
select??? dept_id,? avg(salary) aa? from s_emp?? group by (dept_id)??? having????? avg(salary)>2000;
除了42部門以外的部門的平均工資:
select?? dept_id? ,? avg(salary)?? from? s_emp? group by (dept_id ) having??? dept_id!=42;
select?? dept_id? ,? avg(salary)?? from? s_emp?? where?? dept_id!=42? group by (dept_id ) ;(此種sql效率要高,先過濾再計(jì)算)
where?????? 單行函數(shù)。
having????? 組函數(shù)。
求各個(gè)部門的平均工資:
// 這樣統(tǒng)計(jì)不詳細(xì)
select??? max(d.name) ,? avg (s.salary)?? from?? s_emp? s,? s_dept? d where??? s.dept_id=d.id?? group by??? d.name;?
//****這問題很經(jīng)典,為了過 oracle sql 語法關(guān)而寫max(d.name)? ***
select?? max(d.name)? , avg(e.salary)? , max(r.name)? from s_emp e,?? s_dept? d ,?? s_region? r? where? e.dept_id = d.id? and? d.region_id=r.id group? by?? d.id ;
下午:
關(guān)于子查詢:? Subqueries
找出所有員工中,工資最低的那個(gè)員工:( 利用子查詢 )
select??? first_name,? salary??? from s_emp?? where?? salary = (? select? min(salary)? from s_emp)??? ;
//這樣寫會(huì)出錯(cuò)姓名和工資不一致
select max(first_name),? min(salary)? from s_emp;(利用子查詢可以解決)
子查詢運(yùn)行的順序: 先運(yùn)行子查詢再運(yùn)行主查詢??? 子查詢一般出現(xiàn)在運(yùn)算符的右邊
單值運(yùn)算符:運(yùn)算后面只能跟一個(gè)值
多值運(yùn)算符:可以對兩個(gè)以上的值進(jìn)行操作
查詢誰跟Smith干一樣的活:
select?? last_name from? s_emp? where last_name='Smith';
//下種寫法可能還存在bug,沒有考慮到數(shù)據(jù)的全面性,有潛在性問題
select? last_name? , title? from s_emp?? where title =(? select?? title? from s_emp? where? last_name='Smith'? )??? and? last_name <> 'Smith'? ;
//這種寫法才考慮的比較全面
select? last_name? , title? from s_emp?? where title?? in?? (? select?? title? from s_emp? where? last_name='Smith'? )??? and? last_name <> 'Smith'? ;
使用子查詢時(shí)應(yīng)注意:? 單行子查詢返回多個(gè)結(jié)果時(shí)會(huì)有錯(cuò)誤??? single-row? subquery returns? more? than one value
查出哪些員工的工資比平均工資低:
select??? *? from s_emp? where???? salary?? <? ( select? avg(salary)? from?? s_emp)? ;
哪些部門的平均工資比32部門的平均工資要低:
第一步先查出各個(gè)部門的平均工資:
select? min(avg(salary? )? ) from?? s_emp?? group by? dept_id;
第二步再查出哪個(gè)部門的工資是最低的:
select??? dept_id,? avg(salary)? from? s_emp?? group by dept_id?? having?? avg(salary) =? (select? min(avg(salary)? ) from? s_emp? group by? dept_id ) ;
哪個(gè)部門里沒有員工:
select?? deptno? from??? dept??? where?? deptno?? not? in ( select???? deptno??? from?? emp );
哪些人是普通員工:(用子查詢形式來做)
select?? *?? from? s_emp?? where?? id? not??? in (? select? manager_id?? from?? s_emp);
E--R圖? 實(shí)體關(guān)系圖entity? relation?
開發(fā)流程先進(jìn)行需求分析,進(jìn)行系統(tǒng)設(shè)計(jì),建表,再進(jìn)行開發(fā)編碼,測試最終產(chǎn)品上線試運(yùn)行。
把軟件設(shè)計(jì)模型轉(zhuǎn)化為數(shù)據(jù)中的表,設(shè)計(jì)時(shí)要考慮性能的設(shè)計(jì)
第一范式:最簡單的一種建方式,一張表只有一個(gè)主鍵。
第二范式:表的自連接存在原因,一張表,學(xué)生表中也有班級的信息。
第三范式:表連接存在的原因,兩張表,其中一張表引用其它一張表。
約束:
為了保證數(shù)據(jù)的一致性,
primary key?? (pk)? 主鍵約束?????? 不允許有重復(fù)和空值(唯一且非空)
foregin? key?? (fk)?? 外鍵約束?????? 兩張表parent? table????? child?? table
unique? key?? (uk)? 唯一可以為空
not?? null
數(shù)據(jù)庫設(shè)計(jì)時(shí)的注意:
索引: 為了提高效率而設(shè)計(jì)的一種與業(yè)務(wù)無關(guān)的
考慮表點(diǎn)用的物理空間:
考慮表之間的關(guān)系:
一對多關(guān)系: 利用FK+PK實(shí)現(xiàn),多的一方引用外鍵
一對一關(guān)系: 可以利用FK+UK實(shí)現(xiàn),
多對多關(guān)系: 通過中間增加一個(gè)附加表來實(shí)現(xiàn),附加表利用聯(lián)合主鍵來實(shí)現(xiàn),聯(lián)合起來的主鍵唯一。
DDL語句:數(shù)據(jù)庫定義語句:
table (表)
view(示圖)
sequence(序列號)
index(索引)
創(chuàng)建表語句:
create??? table??? [schema].表名?? (? 字段名,?? 字段類型?? 約束條件);??????????????????? schema?? 默認(rèn)就是當(dāng)前用戶,嚴(yán)格來訪問表名完整的寫法是schema.tablename
數(shù)據(jù)類型:
表名的命令規(guī)則: 首字母為字母,不得超過30個(gè)字符
char(size)??????????????? 定長? 不管是否達(dá)到最大寬度,都會(huì)點(diǎn)最大的寬度。
varchar2(size)???????? 可變長?? 按實(shí)際的字節(jié)占用空間
number??????????? 所有的數(shù)字類型都稱為number
number(n, m )? n------n位寬度?? m-----小數(shù)點(diǎn)后的寬度
number(2,4)小數(shù)點(diǎn)后4 位,有效位2位??? values(0.0099) 這樣可以?? values(0.01)這樣出錯(cuò)
LONG??? 2GB?? 大文本一個(gè)表最我只允許定義一個(gè)LONG類型(不建議使用)
CLOB??? 大對象形式存放(在表里只存一個(gè)指針)
BLOB???? 存二進(jìn)制大對象(聲音,圖像之類)
default?? 作用演示:
create? table?? test(c1?? number??? default? 10,???? c2??? number);
約束的演示:
主鍵約束的定義:
create table?? test(c?? number? primary key? );???? 列級約束
create table? test(c? number , primary key(c) )? ; 表級約束
create table?? test( c1? number? constraints?? pkc1? primary key );?? 此約束有名字:? pkc1
create table?? test(c number , c2? number ,? primary key (c ,c1) )? ; 用表級約束可以實(shí)現(xiàn)聯(lián)合主鍵
外鍵約束的定義:(先定義父表,再定義子表)
carete?? table???? parent(c1 number? primary key );
create?? table??? child? (c? number primary key ,?? c2 number? references parent(c1));
或表級約束定義:
create?? table? child( c number primary key ,? c2? number? , foreign key(c2)? references? parent(c1));
或表級約束定義:
create?? table? child( c number primary key ,? c2? number? , foreign key(c2)? references? parent(c1));
on? delete?? cascade? (及聯(lián)刪除,刪除父表時(shí)子表也跟著刪除)
on? delete?? set?? null? (及聯(lián)刪除父表時(shí)子表中引用的字段為null)
day3
不給約束起名字時(shí),系統(tǒng)給約束起名時(shí)的規(guī)律為:數(shù)據(jù)庫用戶名_數(shù)字(約束名也不能重名)
定義一個(gè)約束的兩種形式:
列級約束????? 表級約束
非空約束:
??not??? null? (利用desc可能看到)primary key? 自動(dòng)具有非空約束的特點(diǎn)
primary key約束:
主鍵約束的定義:
第一種定義形式:
create table?? test(c?? number? primary key? );???? 列級約束
第二種定義形式:
create table? test(c? number , primary key(c) )? ; 表級約束
create table?? test( c1? number? constraints?? pkc1? primary key );?? 此約束有名字:? pkc1
create table?? test(c number , c2? number ,? primary key (c ,c1) )? ; 用表級約束可以實(shí)現(xiàn)聯(lián)合主鍵
foregin? key?? (fk)?? 外鍵約束:
(先定義父表,再定義子表)
create?? table???? parent(c1 number? primary key );
create?? table??? child? (c? number primary key ,?? c2 number? references parent(c1));
或表級約束定義:
create?? table? child( c number primary key ,? c2? number? , foreign key(c2)? references? parent(c1));
check 約束:
create?? table?? test(c1?? number? check(c1>1000));
此表中要求c1的值必須要大于1000 才為有效值 .??
怎么創(chuàng)建一個(gè)角本文件: xxx.sql結(jié)尾
?執(zhí)行角本的方法:
?在sqlplus環(huán)境中執(zhí)行:@filename.sql
?在shell環(huán)境中執(zhí)行: sqlplus?? nanjing/nanjing?? @filename.sql
創(chuàng)建表的語法:
?create??? table??? 表名 (?? 字段名??? 字段類型???? 約束類型(可選));
?利用已知表建一張新表:注會(huì)把非空約束帶過來,其它約束要自己添加
?create? table s_emp_42??? as select?? *? from?? s_emp???? where?? dept_id = 42;
只取要表結(jié)構(gòu),不想要表中數(shù)據(jù)的建表方式:
create table? s_emp_copy??? as?? select? *??? from? s_emp?? where?? 1=2;
(這是一個(gè)小技巧,在JDBC的學(xué)習(xí)中會(huì)用到 where 1=1 的形式,注意體會(huì))
查看一張表的約束:( 查數(shù)據(jù)字典示圖)
?desc? user_constraints;(這個(gè)數(shù)據(jù)字典中會(huì)查到相應(yīng)的信息)
?select??? constraint_name,? constraint_type??? from?? user_constraints? where?? table_name='S_EMP';
?P?? pk
?R?? fk
?C?? check
?U??? UK
?V??? 這種只定義在示圖中(with check? option 相當(dāng)于組示圖加了一個(gè)約束)
?O??? 也是出現(xiàn)在示圖中
?非空約束和CHECK都是用C來表示
查看字段約束的方法:
?desc??? user_cons_columns;
?select?? column_name,? position? from??? user_cons_columns??? where?? constraint_name='S_EMP_ID_PK' ;
?position 的含義:聯(lián)合主鍵,約束名一樣。
?user_constraints??? user_cons_columns?? 兩張表的約束名相等,表名相等,兩張表一關(guān)聯(lián)就可以查出所需的信息。
select? constraint_name , r_constraint_name? from user_constraints where? constraint_type='R'?? and table_name='S_EMP' ;
數(shù)據(jù)庫建立時(shí),數(shù)據(jù)字典就會(huì)建好。
user_constraints; 自己擁有的
all_constraints;?? 你自己擁有的加上你可以訪問的
dba_constraints? 所有的
查看當(dāng)前數(shù)據(jù)庫數(shù)據(jù)字典的字典(這個(gè)示圖很重要)
desc?? dict;
select table_name form? dict where table_name like?? '%cons%;
示圖:
user_objects;?????????? user_tables;
select? distinct?? object_type? from user_objects;??
介紹事務(wù)的概念:
commit? 提交,此時(shí)說明前面所有語句都成功執(zhí)行
rollback 回退操作,此時(shí)會(huì)恢復(fù)至上一次提交時(shí)的狀態(tài)。
savepoint 設(shè)置保存點(diǎn)
?注意?? insert?? into? 后面可以跟子查詢
insert into? s_emp_42?? select *?? from s_emp? where??? dept_id =42;
UPDATE 修改字段值:
update?? s_emp? set dept_id =10?? where?? id =2 ;
update? s_emp? set commission_pct =10? ;? 沒有where條件時(shí)說明是改表中所有的值.
注意:如有外鍵引用時(shí)常會(huì)出現(xiàn)外鍵引用值沒有找到等錯(cuò)誤?
delete? 刪除記錄命令語法:
delete from?? s_emp? where? dept_id=42;
delete form?? s_emp ;????? 沒有where條件時(shí)說明刪除表中所有的值
注意:如有外鍵引用時(shí),刪除一張表時(shí)常會(huì)出現(xiàn)不能刪除的情況,
原因一?? 是因?yàn)榇藭r(shí)正在有人操作表中記錄
原因二?? 此表有其他的表引用,沒能設(shè)及聯(lián)刪除:
delete 刪除一張大表時(shí)空間不釋放,非常慢是因?yàn)檎加么罅康南到y(tǒng)資源,支持回退操作,空間還被這張表占用著。
truncate table 表名? (刪除表中記錄時(shí)釋放表空間)
DML 語句:
表級共享鎖: 對于操作一張表中的不同記錄時(shí),互不影響
行級排它鎖:對于一行記錄,oracle 會(huì)只允許只有一個(gè)用戶對它在同一時(shí)間進(jìn)行修改操作
wait()?? 等到行級鎖被釋放,才進(jìn)行數(shù)據(jù)操作
drop一張表時(shí)也會(huì)對表加鎖,DDL排它鎖,所以在刪除一張表時(shí)如果當(dāng)前還有用戶操作表時(shí)不能刪除表
alter table 命令用于修改表的結(jié)構(gòu)(這些命令不會(huì)經(jīng)常用):
增加約束:
alter table? 表名 add?? constraint 約束名? primary key? (字段);
解除約束:(刪除約束)
alter? table 表名? drop? primary? key(對于主鍵約束可以直接用此方法,因?yàn)橐粡埍碇兄挥幸粋€(gè)主鍵約束名, 注意如果主鍵此時(shí)還有其它表引用時(shí)刪除主鍵時(shí)會(huì)出錯(cuò))
alter? tbale?? father?? drop? primary key??? cascade ;? (如果有子表引用主鍵時(shí),要用此語法來刪除主鍵,這時(shí)子表還存在只是子表中的外鍵約束被及聯(lián)刪除了)
alter table? 表名 drop? constraint?? 約束名;
(怎樣取一個(gè)約束名:1、人為的違反約束規(guī)定根據(jù)錯(cuò)誤信息獲取!
???????????????????????????????? 2、查詢示圖獲取約束名!)
alter? table?? 表名? disable??? from?? primary? key ;? (相當(dāng)于把一個(gè)表的主鍵禁用)
alter? table?? 表名? enable??? primary key ;(enable 時(shí)會(huì)自動(dòng)去檢查表的記錄是不是符合要求,如果有臟數(shù)據(jù)時(shí)必須要先刪除臟數(shù)據(jù)才可以 enable)
?
*******************************************************************
增加字段:
?alter? table 表名 add(字段字? 字段類型)
刪除字段:
?alter table 表名???? drop(字段)
?alter tbale???????? 表名??? drop??? column?? 字段 ; (8i 以后才支持)
給列改名:920才支持
?alter? table?? 表名?? rename?? column?? 舊字段名??? to???? 新字段名;
修改字段
(此時(shí)應(yīng)注意的問題,更改時(shí)要看具體值情況之間的轉(zhuǎn)達(dá)換, 改為字符類型時(shí),必須要為空)
?alter? table?? 表名???? modify( 字段,類型)
更改表中的字段:
?update 表名?? set???? 字段???? =????? 值???? where?????? 條件
更改表名
?rename?????? 舊表名?????????? to ? 新表名?????????? ;
刪除表:
?trucate?? table??? 表名:(表結(jié)構(gòu)還在,數(shù)據(jù)全部刪除,釋放表所占的空間,不支持回退,常用刪除大表)
?
關(guān)于oralce中產(chǎn)生序列(sequence):
create sequence?? 序列名alter system? flush?? shared_pool;
(不帶參數(shù)時(shí)默認(rèn)為從1 開始每次遞增 1,oracle中為了提高產(chǎn)生序列的效率一般一次性產(chǎn)生20個(gè)序列放入當(dāng)前會(huì)話的序列池中備用以加快效率,序列會(huì)出現(xiàn)不連續(xù)的動(dòng)作回退操作不會(huì)影響序列取值)
sequence 的參數(shù):
?increment by? n 起始值,??? start with? n 遞增量, maxvalue? n 最大值,? minvalue n? 最小值,cycle | no cycle 輪回,? cache n? 綬存(第一次取時(shí)會(huì)一次取多少個(gè)id存起來)
查看?? sequence 示圖:
desc??? user_sequences ;
select?? sequence_name , cache_size , last_number? from? user_sequences?? where?? sequence_name? like 's_';
select? 序列名.currval? from?? dual??? 查看當(dāng)前的序列數(shù)
select? 序列名.nextval? from?? dual??? 查看下一個(gè)序列數(shù),它會(huì)自動(dòng)給當(dāng)前的序列加1
為列:nextval????????? currval
(開另一個(gè)session時(shí)取當(dāng)前值不成功時(shí),應(yīng)該先取下一個(gè)值,再取當(dāng)前值)
清空當(dāng)前會(huì)話的內(nèi)存:
alter system? flush?? shared_pool;(執(zhí)行此命令要有DBA權(quán)限,一般用戶執(zhí)行出錯(cuò))
修改序列:(此命令不常用,只需了解就行不必深究)
alter? sequence? 序列名? 修改項(xiàng);
刪除序列sequence
drop? sequence 序列名;
創(chuàng)建示圖: creating????? views(屬于了解知識)
desc? user_views;
select?? text?? from? user_views??? where?? view_name='TEST1_V1' ;
示圖就相當(dāng)于一條select 語句,定義了一個(gè)示圖就是定義了一個(gè)sql語句,示圖不占空間,使用view 不會(huì)提高性能,但是能簡單化sql語句
(擴(kuò)展知識: oracle? 8i 以后的新示圖)MV?? 物化視圖(占存儲(chǔ)空間,把select 結(jié)果存在一個(gè)空間,會(huì)提高查詢視圖,增強(qiáng)實(shí)時(shí)性,但是存在刷新問題, 主要應(yīng)用在數(shù)據(jù)倉庫中用要用于聚合表)
使用示圖的好處:控制數(shù)據(jù)訪問權(quán)限.
如何創(chuàng)建一個(gè)示圖:
create?? or replace?? views?? test_vi??? as?????? select?????? *?? from??? test1?? where c1=1;
此時(shí)往表test1(base?? table? 基表)中插入數(shù)據(jù)時(shí):表中沒能變化,示圖中的數(shù)據(jù)發(fā)生改變
從示圖中插數(shù)據(jù)時(shí)相對應(yīng)的表會(huì)發(fā)生改變:
往示圖中插數(shù)據(jù)時(shí),會(huì)直接插進(jìn)基表中,查看示圖中的數(shù)據(jù)時(shí),相當(dāng)于就是執(zhí)行創(chuàng)建時(shí)的select語句。
簡單示圖:能進(jìn)行DML操作。
復(fù)雜示圖:來源于多張表,不能執(zhí)行DML操作。
關(guān)于rownum:
rownum? 有個(gè)特點(diǎn)要么等于1 要么小于某個(gè)值, 不能直接等于某個(gè)值, 不能大于某個(gè)值。rownum常用于分頁顯示。
練習(xí):查詢出第5條數(shù)據(jù)和第10條數(shù)據(jù)之間:
?select?? first_name , rnum??? from?? (? select?? rownum?? rnum??? , first_name?? from??? s_emp? where rownum <=10 )???? where rnum? between 5? and? 10 ;
分面顯示:
SELECT * FROM (SELECT a.*, rownum r FROM?? S_EMP? a? WHERE r between 5? AND? 10 );
練習(xí):哪些員工的工資比本部門的平均工資高?
select?? first_name? , salary?? , avgsal???? from? s_emp?? e , ( select?? dept_id? , avg (salary )?? avgsal? from?? s_emp? group? by dept_id )? a?? where?? e.dept_id =a.dept_id and e.salary > a.avgsal;
?在示圖上加一個(gè) with? check?? option 就相當(dāng)于給示圖加上了約束
create??? view??? test_v? as? select?? *? from?? test? where c =1? with check option ;
同義詞:相當(dāng)于別名的作用(***只需了解***)系統(tǒng)自建的同義詞:??? user_tables
create? synonym??? asd_s_emp?? for??? asd_0607.s_emp ;
目的就是為了給asd_0607_s_emp表起另一個(gè)代替的名稱asd.s_emp;注意這個(gè)同義詞只能自己使用;
create? public???? synonym? p_s_emp? fro asd_0607.s_emp; 創(chuàng)建公共的同義詞,但是要權(quán)限.
刪除同義詞:
drop? synonym??? 同義詞名稱
創(chuàng)建索引:? Creating??? indexes(概念很重要對系統(tǒng)的性能影響非常大)
建索引的目的就是為了加快查詢速度。
索引就相于一本的書的目錄。索引點(diǎn)系統(tǒng)空間,屬于表的附屬物。刪除一個(gè)表時(shí),相對應(yīng)的索引也會(huì)刪除。truncate 表時(shí)索引結(jié)構(gòu)在,但是數(shù)據(jù)不存在。
full?? table??? scan? 全表掃描
用索引就是為了快速定位數(shù)據(jù):(理解時(shí)就以字典的目錄為例)
查看表的rowid:
select???? rowid? , first_name??? from? s_emp;
rowid 定義的信息有:? object?? block? table
每條記錄都有自己的rowid
索引由誰創(chuàng)建:用戶,建索引后會(huì)使DML操作效率慢,但是對用戶查詢會(huì)提高效率,這就是我們建索引的最終目的,
創(chuàng)建一個(gè)索引:
create? index???? 索引名???? on?? 表名 (? 字段名);
create?? insex testindex? on test(c1, c2);
哪些字段應(yīng)該建索引:
經(jīng)常要用where的子句的地方,所以要用索引.用不用索引,關(guān)鍵要看所查詢的數(shù)據(jù)與所有數(shù)據(jù)的百分比,表越大,查詢的記錄越少,索引的效率最高.
替換變量:用&符號來定義替換變量支持交互性提示,對于字符性的數(shù)字,一定要寫在單引號之間
set??? verify on
set??? verify off;
相當(dāng)于開關(guān)變量,用于控制是否顯示新舊的sql語句
select?? id ,last_name? ,salary?? from s_emp? where? title='&job_title';
更改交互的提示信息:
accept? p_dname prompt ' 提示信息';
定義變量:
define???? p_dname='abc';
分頁的實(shí)現(xiàn)語句:(可以正常運(yùn)行)
? select?? *?? from? (? select?? rownum?? rnum? , a.*?? from?? (select * from s_emp) a? )???? where rnum? between 5? and? 10 ;
-------------------------------------------------------------------------------------------------------------------------
1、關(guān)于約束的知識:
primary key約束:
主鍵約束的定義:
第一種定義形式:
create table?? test(c?? number? primary key? );???? 列級約束
第二種定義形式:
create table? test(c? number , primary key(c) )? ; 表級約束
create table?? test( c1? number? constraints?? pkc1? primary key );?? 此約束有名字:? pkc1
create table?? test(c number , c2? number ,? primary key (c ,c1) )? ; 用表級約束可以實(shí)現(xiàn)聯(lián)合主鍵
foregin? key?? (fk)?? 外鍵約束:
(先定義父表,再定義子表)
carete?? table???? parent(c1 number? primary key );
create?? table??? child? (c? number primary key ,?? c2 number? references parent(c1));
或表級約束定義:
create?? table? child( c number primary key ,? c2? number? , foreign key(c2)? references? parent(c1));
check 約束:
create?? table?? test(c1?? number? check(c1>1000));
此表中要求c1的值必須要大于1000 才為有效值 .??
****************************************************************************
2、關(guān)于針對表操作的語法知識:
? 創(chuàng)建表:
?? create??? table? 表名?? (??? 字段名1??? 類型?? 約束條件,?? 字段名2??? 類型??? 約束條件 );
?
?插入數(shù)據(jù)命令:
?方式一:(指定字段名插入數(shù)據(jù))
? insert? into?? 表名? ( 字段名 )??? values ( 數(shù)據(jù));
?方式二:
? insert? into? 表名?? values(數(shù)據(jù)1,? 數(shù)據(jù)2);
?修改數(shù)據(jù):
?update?? table?? 表名? set ( 字段名?? 數(shù)據(jù), 字段名? 數(shù)據(jù));
****************************************************************************
3、關(guān)于alter table 命令知識:
alter table 命令用于修改表的結(jié)構(gòu)(這些命令不會(huì)經(jīng)常用):
增加約束:
alter table? 表名 add?? constraint 約束名? primary key? (字段);
解除約束:(刪除約束)
alter? table 表名? drop? primary? key(對于主鍵約束可以直接用此方法,因?yàn)橐粡埍碇兄挥幸粋€(gè)主鍵約束名, 注意如果主鍵此時(shí)還有其它表引用時(shí)刪除主鍵時(shí)會(huì)出錯(cuò))
alter? tbale?? father?? drop? primary key??? cascade ;? (如果有子表引用主鍵時(shí),要用此語法來刪除主鍵,這時(shí)子表還存在只是子表中的外鍵約束被及聯(lián)刪除了)
alter table? 表名 drop? constraint?? 約束名;
(怎樣取一個(gè)約束名:
a、人為的違反約束規(guī)定根據(jù)錯(cuò)誤信息獲取!
b、查詢示圖獲取約束名!)
alter? table?? 表名? disable??? from?? primary? key ;? (相當(dāng)于把一個(gè)表的主鍵禁用)
alter? table?? 表名? enable??? primary key ;(enable 時(shí)會(huì)自動(dòng)去檢查表的記錄是不是符合要求,如果有臟數(shù)據(jù)時(shí)必須要先刪除臟數(shù)據(jù)才可以 enable)
增加字段:
alter? table 表名 add(字段字,字段類型)
刪除字段:
alter table 表名???? drop(字段)
alter tbale???????? 表名??? drop??? column?? 字段 ; (8i 以后才支持)
給列改名:920才支持
alter? table?? 表名?? rename?? column?? 舊字段名??? to???? 新字段名;
修改字段
(此時(shí)應(yīng)注意的問題,更改時(shí)要看具體值情況之間的轉(zhuǎn)達(dá)換, 改為字符類型時(shí),必須要為空)
alter? table?? 表名???? modify( 字段,類型)
更改表中的字段:
update 表名?? set???? 字段???? =????? 值???? where?????? 條件
更改表名
rename?????? 舊表名?????????? to ? 新表名?????????? ;
刪除表:
trucate?? table??? 表名:(表結(jié)構(gòu)還在,數(shù)據(jù)全部刪除,釋放表所占的空間,不支持回退,常用刪除大表)
****************************************************************************
4、關(guān)于oralce中產(chǎn)生序列(sequence)
create sequence?? 序列名alter system? flush?? shared_pool;
(不帶參數(shù)時(shí)默認(rèn)為從1 開始每次遞增 1,oracle中為了提高產(chǎn)生序列的效率一般一次性產(chǎn)生20個(gè)序列放入當(dāng)前會(huì)話的序列池中備用以加快效率,序列會(huì)出現(xiàn)不連續(xù)的動(dòng)作回退操作不會(huì)影響序列取值)
sequence 的參數(shù):
?increment by? n 起始值,??? start with? n 遞增量, maxvalue? n 最大值,? minvalue n? 最小值,cycle | no cycle 輪回,? cache n? 綬存(第一次取時(shí)會(huì)一次取多少個(gè)id存起來)
查看?? sequence 示圖:
desc??? user_sequences ;
select?? sequence_name , cache_size , last_number? from? user_sequences?? where?? sequence_name? like 's_';
select? 序列名.currval? from?? dual??? 查看當(dāng)前的序列數(shù)
select? 序列名.nextval? from?? dual??? 查看下一個(gè)序列數(shù),它會(huì)自動(dòng)給當(dāng)前的序列加1
為列:nextval????????? currval
(開另一個(gè)session時(shí)取當(dāng)前值不成功時(shí),應(yīng)該先取下一個(gè)值,再取當(dāng)前值)
清空當(dāng)前會(huì)話的內(nèi)存:
alter system? flush?? shared_pool;(執(zhí)行此命令要有DBA權(quán)限,一般用戶執(zhí)行出錯(cuò))
修改序列:(此命令不常用,只需了解就行不必深究)
alter? sequence? 序列名? 修改項(xiàng);
刪除序列sequence
drop? sequence 序列名;
****************************************************************************
5、創(chuàng)建示圖: creating????? views(屬于了解知識)
示圖就相當(dāng)于一條select 語句,定義了一個(gè)示圖就是定義了一個(gè)sql語句,示圖不占空間,使用view 不會(huì)提高性能,但是能簡單化sql語句
(擴(kuò)展知識: oracle? 8i 以后的新示圖)MV?? 物化視圖(占存儲(chǔ)空間,把select 結(jié)果存在一個(gè)空間,會(huì)提高查詢視圖,增強(qiáng)實(shí)時(shí)性,但是存在刷新問題, 主要應(yīng)用在數(shù)據(jù)倉庫中用要用于聚合表)
使用示圖的好處:控制數(shù)據(jù)訪問權(quán)限.
如何創(chuàng)建一個(gè)示圖:
create?? or replace?? views?? test_vi??? as?????? select?????? *?? from??? test1?? where c1=1;
此時(shí)往表test1(base?? table? 基表)中插入數(shù)據(jù)時(shí):表中沒能變化,示圖中的數(shù)據(jù)發(fā)生改變
從示圖中插數(shù)據(jù)時(shí)相對應(yīng)的表會(huì)發(fā)生改變:
往示圖中插數(shù)據(jù)時(shí),會(huì)直接插進(jìn)基表中,查看示圖中的數(shù)據(jù)時(shí),相當(dāng)于就是執(zhí)行創(chuàng)建時(shí)的select語句。
簡單示圖:能進(jìn)行DML操作。
復(fù)雜示圖:來源于多張表,不能執(zhí)行DML操作。
關(guān)于rownum:
rownum? 有個(gè)特點(diǎn)要么等于1 要么小于某個(gè)值, 不能直接等于某個(gè)值, 不能大于某個(gè)值。rownum常用于分頁顯示。
練習(xí):查詢出第5條數(shù)據(jù)和第10條數(shù)據(jù)之間:
select?? first_name? , rnum??? from?? (? select?? rownum?? rnum??? , first_name?? from?? s_emp??? where rownum <=10 )??? where rnum? between 5? and? 10;
練習(xí):哪些員工的工資比本部門的平均工資高?
select?? first_name? , salary?? , avgsal???? from? s_emp?? e , ( select?? dept_id? , avg (salary )?? avgsal? from?? s_emp? group? by dept_id )? a?? where?? e.dept_id =a.dept_id and e.salary > a.avgsal;
?關(guān)于同義詞:
同義詞:相當(dāng)于別名的作用(***只需了解***)系統(tǒng)自建的同義詞:??? user_tables
create? synonym??? asd_s_emp?? for??? asd_0607.s_emp ;
目的就是為了給asd_0607_s_emp表起另一個(gè)代替的名稱asd.s_emp;注意這個(gè)同義詞只能自己使用;
create? public???? synonym? p_s_emp? fro asd_0607.s_emp; 創(chuàng)建公共的同義詞,但是要權(quán)限.
刪除同義詞:
drop? synonym??? 同義詞名稱
****************************************************************************
6、創(chuàng)建索引:? Creating??? indexes(概念很重要對系統(tǒng)的性能影響非常大)
建索引的目的就是為了加快查詢速度。
索引就相于一本的書的目錄。索引點(diǎn)系統(tǒng)空間,屬于表的附屬物。刪除一個(gè)表時(shí),相對應(yīng)的索引也會(huì)刪除。truncate 表時(shí)索引結(jié)構(gòu)在,但是數(shù)據(jù)不存在。
full?? table??? scan? 全表掃描
用索引就是為了快速定位數(shù)據(jù):(理解時(shí)就以字典的目錄為例)
查看表的rowid:
select???? rowid? , first_name??? from? s_emp;
rowid 定義的信息有:? object?? block? table
每條記錄都有自己的rowid
索引由誰創(chuàng)建:用戶,建索引后會(huì)使DML操作效率慢,但是對用戶查詢會(huì)提高效率,這就是我們建索引的最終目的,
創(chuàng)建一個(gè)索引:
create? index???? 索引名???? on?? 表名 (? 字段名);
create?? insex testindex? on test(c1, c2);
哪些字段應(yīng)該建索引:
經(jīng)常要用where的子句的地方,所以要用索引.用不用索引,關(guān)鍵要看所查詢的數(shù)據(jù)與所有數(shù)據(jù)的百分比,表越大,查詢的記錄越少,索引的效率最高.
替換變量:用&符號來定義替換變量支持交互性提示,對于字符性的數(shù)字,一定要寫在單引號之間
set??? verify on
set??? verify off;
相當(dāng)于開關(guān)變量,用于控制是否顯示新舊的sql語句
select?? id ,last_name? ,salary?? from s_emp? where? title='&job_title';
更改交互的提示信息:
accept? p_dname prompt ' 提示信息';
定義變量:
define???? p_dname='abc';