第一部分 數(shù)據(jù)庫(kù)基礎(chǔ) 
一、基本概念 
1.什么是sql? 
sql即結(jié)構(gòu)化查詢語(yǔ)言,是用于關(guān)系數(shù)據(jù)庫(kù)通迅的標(biāo)準(zhǔn)語(yǔ)言,通過(guò)sql用戶可向數(shù)據(jù)庫(kù)表達(dá)自已的需求。 
2.什么是客戶機(jī)/服務(wù)器技術(shù)? 
在客戶機(jī)/服務(wù)器中,主機(jī)叫作服務(wù)器,網(wǎng)絡(luò)通常是局域網(wǎng)(lan)或是廣域網(wǎng)(wan).服務(wù)器通常被個(gè)人計(jì)算機(jī)或是其他的服務(wù)器或個(gè)人計(jì)算機(jī)而不是終端訪問.每一臺(tái)pc機(jī)都叫做一個(gè)客戶機(jī),都有訪問網(wǎng)絡(luò)的功能,允許在客戶機(jī)和服務(wù)器之間通信因此叫客戶機(jī)/服務(wù)器技術(shù). 
二、數(shù)據(jù)處理的發(fā)展階段 
1、手工處理階段: 
50年代中期,計(jì)算機(jī)內(nèi)存、外存容量都很小,外存只有磁帶,沒有磁盤,硬件性能很差;軟件中也沒有數(shù)據(jù)管理部分,因而主要限于科學(xué)計(jì)算;這一階段主要是用手工進(jìn)行數(shù)據(jù)處理。 
2、文件系統(tǒng)階段:數(shù)據(jù)和程序的分離導(dǎo)致了文件系統(tǒng)的誕生。 
順序與隨機(jī)文件組織并用; 
批量與實(shí)時(shí)處理并存; 
物理和邏輯結(jié)構(gòu)有了簡(jiǎn)單變換; 
軟件提供存取方法; 
數(shù)據(jù)冗余較多,數(shù)據(jù)不能共享; 
文件或數(shù)據(jù)屬于一個(gè)用戶; 
數(shù)據(jù)獨(dú)立性差,數(shù)據(jù)結(jié)構(gòu)和程序合為一體; 
無(wú)集中管理,無(wú)法用全局觀點(diǎn)而只能面向局部應(yīng)用進(jìn)行數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)。 
3、數(shù)據(jù)庫(kù)系統(tǒng)階段: 
數(shù)據(jù)庫(kù)技術(shù)的目標(biāo)就是克服文件系統(tǒng)的弊病,解決冗余和數(shù)據(jù)獨(dú)立性問題,并通過(guò)一軟件系統(tǒng)(dbms)來(lái)集中管理所有文件,從而實(shí)現(xiàn)數(shù)據(jù)共享,其本質(zhì)是通過(guò)分層,第一層面向用戶,第二層面向全局,第三層面向物理存貯,層和層這間通過(guò)兩級(jí)變換,這樣將用戶應(yīng)用文件與物理文件完全分離,分別實(shí)現(xiàn)了數(shù)據(jù)的邏輯和物理獨(dú)立性。 
模型:為把數(shù)據(jù)和程序完全分離,分為三種模型: 
內(nèi)模型或物理模型:面向計(jì)算機(jī)物理存貯; 
概念模型:面向系統(tǒng)整體;(dbms) 
外模型:面向用戶。 
模式:用ddl精確定義數(shù)據(jù)模型的程序; 
內(nèi)模式:定義內(nèi)模型的模式; 
概念模式:定義概念模型的模式; 
外模式:定義外模型(面向用戶)的模式。 
數(shù)據(jù)模型:表示實(shí)體(entity)以及實(shí)體間聯(lián)系(relationship)的模型;有四種形式: 
e-r模型:最直觀的描述方式,實(shí)體集用長(zhǎng)方形表示,屬性用橢圓表示,聯(lián)系用菱形表示; 
層次模型:可表示1-m關(guān)系,有唯一根(和父)結(jié)點(diǎn); 
網(wǎng)狀模型:可表示m-m關(guān)系; 
關(guān)系模型:以表來(lái)表示數(shù)據(jù)間的聯(lián)系,通過(guò)表格數(shù)據(jù)而不是通過(guò)指針鏈接來(lái)表示和實(shí)現(xiàn)兩實(shí)體間的關(guān)系。 
三、oracle簡(jiǎn)介 
自1970年codd發(fā)表關(guān)系理論后,關(guān)系型數(shù)據(jù)庫(kù)得到了飛速的發(fā)展,如informix、db2等; 
起初oracle是一發(fā)展較完善的關(guān)系型數(shù)據(jù)庫(kù),已由單一的數(shù)據(jù)庫(kù)發(fā)展到多種服務(wù),多種產(chǎn)品的;oracle rdbms是oracle系統(tǒng)的核心,它由數(shù)據(jù)庫(kù)管理的核心模塊以及幫助用戶和數(shù)據(jù)庫(kù)管理員dba維護(hù)、監(jiān)視和使用數(shù)據(jù)庫(kù)的一些實(shí)用程序組成。rdbms的核心模塊就是sql的實(shí)現(xiàn)模塊,它是所有實(shí)用程序的基礎(chǔ)。 
問:在三種模式(內(nèi)、概念、外模式)中oracle是對(duì)應(yīng)于哪種模式; 
在四種數(shù)據(jù)模型(e-r、層次、網(wǎng)狀、關(guān)系型)中oracle相應(yīng)于哪種? 
四、sql命令簡(jiǎn)介 
1、ddl數(shù)據(jù)定義語(yǔ)言. 
create table; alter table; drop table; 
create view; alter view; drop view; 
create index; alter index; drop index; 
2、 dml數(shù)據(jù)操縱語(yǔ)言. 
insert 向表中插入新數(shù)據(jù)行; 
update 更新表中現(xiàn)有的數(shù)據(jù); 
delete 從表中刪除幾行數(shù)據(jù); 
3、 dql數(shù)據(jù)查詢語(yǔ)言. 
select 從一張或多張表中返回?cái)?shù)據(jù); 
4、 dcl數(shù)據(jù)控制語(yǔ)言. 
alter password 更改用戶密碼; 
grant 為用戶賦予特權(quán); 
revoke 收回用戶特權(quán); 
create synonym 創(chuàng)建同義詞 
5、 數(shù)據(jù)管理命令. 
start audit 
stop audit 
6、 事務(wù)性控制命令. 
commit 將事務(wù)保存到數(shù)據(jù)庫(kù); 
rollback 撤消一個(gè)數(shù)據(jù)庫(kù)事務(wù); 
savepoint 創(chuàng)建在必要時(shí)可以撤消事務(wù)的保存點(diǎn); 
set transaction 設(shè)置事務(wù)。 
第二部分 建立數(shù)據(jù)庫(kù) 
一、基本概念 
1、數(shù)據(jù)庫(kù)和數(shù)據(jù)庫(kù)對(duì)象? 
數(shù)據(jù)庫(kù)是數(shù)據(jù)的集合體; 
數(shù)據(jù)庫(kù)對(duì)象可以是數(shù)據(jù)庫(kù)中定義的任何對(duì)象,用于存儲(chǔ)和參照數(shù)據(jù),表是關(guān)系數(shù)據(jù)庫(kù)最簡(jiǎn)單的存儲(chǔ)形式,還有視圖、索引等。 
2、什么是模式? 
模式數(shù)據(jù)庫(kù)對(duì)象的集合,并有一個(gè)與之相關(guān)連著的特定的用戶名,一個(gè)模式可以包括一張表和沒有數(shù)量限制的對(duì)象. 
3、數(shù)據(jù)結(jié)構(gòu) 
定長(zhǎng)字符串char 變長(zhǎng)字符串varchar2 數(shù)字值number 
小數(shù)點(diǎn)數(shù)值decimal 整型integer 浮點(diǎn)小數(shù)float 
日期和時(shí)間date 文字串 string null型數(shù)據(jù). 
二、表及其基本操作 
在關(guān)系數(shù)據(jù)庫(kù)中,表是數(shù)據(jù)的主要存儲(chǔ)對(duì)象,表包含了行和列,存放著實(shí)體及其聯(lián)系,表在數(shù)據(jù)庫(kù)中占有的物理空間可以是永久性的,也可是暫時(shí)的。 
1、創(chuàng)建表的基本語(yǔ)法為: 
create table table_name 
(field1 datatype [ not null ] 
field2 datatype [ not null ] 
field3 datatype [ not null ]) 
storage 
(initial x 初始空間 
next x 附加空間); 
例: 
create table employee_tbl 
(emp_id char(9) not null , 
emp_name varchar(40) not null , 
emp_zip number(6) null , 
emp_phone number(10) null) 
storage 
( initial 3k 
next 2k); 
2、修改表的語(yǔ)法為: 
alter table table_name 
[modify][column column_name] [datatype not null | null)] [restrict | cascade] 
[drop] [constraint constraint_name] 
[add] [column ] column definition; 
例: 
alter table employee_tbl 
modify (emp_phone number(11) null); 
3、從現(xiàn)有表中創(chuàng)建一新表 
create table new_table_name as 
select [* | column1 colum2] 
from table_name 
[where condition] 
例: 
create table phone_tbl as 
select ( emp_id, emp_phone) 
from employee_tbl; 
4、刪除表的語(yǔ)法為: 
drop table table_name 
例: 
drop table phone_tbl ; 
5、orcale補(bǔ): 
describe 顯示表的結(jié)構(gòu)及所有表列的概要; 
三、完整性約束. 
在關(guān)系數(shù)據(jù)庫(kù)中,完整性約束用于確保數(shù)據(jù)的準(zhǔn)確性和一致性,是通過(guò)參照完整性實(shí)現(xiàn)的。 
1、主鍵約束:primary key 在表中標(biāo)識(shí)一列或多個(gè)列,來(lái)使一行數(shù)據(jù)保持唯一性,用于查詢參考,表的聯(lián)合; 
例: 
alter table employee_tbl 
modify (emp_id char(9) not null primary key); 
2、唯一性約束:unique 對(duì)應(yīng)列不存在相同的數(shù)值; 
例: 
alter table employee_tbl 
modify (emp_phone number(11) null unique); 
3、外鍵約束: 
foreign key key_name_fk(key_name) 
feferences tbl_name(key_name) 
例: 
create table employee_pay_tbl 
(emp_id char(9) not null , 
position varchar2(15) not null , 
pay_rate number(4,2) not null ) 
foreign key emp_id_fk(emp_id) 
references employee_tbl(emp_id)); 
注:要在子表中插入值,必須在父表中key_name首先存在對(duì)應(yīng)的值; 
4、not null約束:不允許有null值; 
注:改變表屬性時(shí),not null ->null可, 
而null->not null值時(shí),要看是否有nul值; 
5、使用check約束:用來(lái)檢查數(shù)據(jù)有效性; 
例: 
create table customer_tbl 
(cust_id char(9) not null , 
cust_name varchar2 not null , 
cust_city varchar2 not null , 
cust_zip number(5) null , 
cust_phone number(11) ), 
primary key (cust_id), 
constraint chk_emp_zip check( emp_zip=‘430079’); 
四、規(guī)范化過(guò)程. 
1、什么是規(guī)范化? 
規(guī)范化是數(shù)據(jù)庫(kù)中減少數(shù)據(jù)冗余的過(guò)程. 除了數(shù)據(jù)庫(kù)中,名稱,對(duì)象名稱和形式都需要規(guī)范化. 
2、將數(shù)據(jù)庫(kù)規(guī)范化 
在數(shù)據(jù)庫(kù)規(guī)范的過(guò)程中,三個(gè)常用的范式為: 
第一范式:將基本數(shù)據(jù)按主鍵分成稱為表的邏輯單元; 
第二范式:使用僅僅部分依賴于主鍵的數(shù)據(jù)輸入到其他表中; 
第三范式:從表中將不依賴于主鍵的數(shù)據(jù)除去; 
例: 
company_database 
( emp_id last_name first_name middle_name 
address city zip phone 
position position_desc date_hire pay_rate 
date_last_raise cust_id cust_name cust_address 
cust_city cust_zip cust_fax qty 
ord_num ord_date prod_id prod_desc 
cost ) 
按第一范式的要求可分為三個(gè)表: 
employee_tbl 
( emp_id last_name first_name middle_name 
address city zip phone 
position date_hire pay_rate date_last_raise) 
customer_tbl 
( cust_id cust_name cust_address cust_city 
cust_zip cust_phone cust_fax qty 
ord_num ord_date ) 
products_tbl 
( prod_id prod_desc cost ) 
按第二范式的要求employee_tbl可分為兩張表: 
employee_tbl 
( emp_id last_name first_name middle_name 
address city zip phone ) 
employee_pay_tbl 
( emp_id position position_desc date_hire 
pay_rate date_last_raise ) 
根據(jù)第三范式employee_pay_tbl可分為兩個(gè)表: 
employee_pay_tbl 
( emp_id position date_hisre pay_rate 
date_last_raise ) 
position_tbl 
( position position_desc ) 
3、規(guī)范式的好處和弊端 
大型的總體數(shù)據(jù)庫(kù)組織, 
減少數(shù)據(jù)冗余, 
數(shù)據(jù)庫(kù)中的數(shù)據(jù)一致性, 
更加靈活的數(shù)據(jù)庫(kù)設(shè)計(jì), 
更好的數(shù)據(jù)庫(kù)安全處理; 
其主要弊端是降低了數(shù)據(jù)庫(kù)的可操作性,因而要進(jìn)行適當(dāng)?shù)姆且?guī)范化。 
第三部分.操縱數(shù)據(jù). 
一、數(shù)據(jù)操縱綜述. 
數(shù)據(jù)操縱語(yǔ)言(dml)是sql的一部分,它允許數(shù)據(jù)庫(kù)用戶在關(guān)系型數(shù)據(jù)庫(kù)中進(jìn)行數(shù)據(jù)的改變. 
在sql中有三種基本的dml命令 
1、insert(插入) 
2、update(更新) 
3. delete (刪除) 
二、給表中填充數(shù)據(jù). 
1、 往表中插入新數(shù)據(jù) 
insert into table_name 
values(‘values1’,’values2’) 
例: 
insert into employee_tbl 
values ( ‘1’,‘武建軍’,‘430079’,‘027111’) 
2、往表中有限列中插入數(shù)據(jù). 
insert into table_name (‘column1’,’column2’) 
values(‘values1’,’values2’) 
例: 
insert into employee_tbl(‘emp_id’,’emp_name’) 
values ( ‘2’,‘大劉’); 
3、插入另一張表中的數(shù)據(jù) 
insert into table_name 
select [* | (‘column1’,’column2’)] 
from table_name [where conditions(s)]; 
例: 
insert into customer_tbl 
select (‘emp_id’,’emp_name’,’emp_zip’,’emp_phone’) 
from employee_tbl 
where emp_zip=’430079’; 
4、插入null值. 
基本語(yǔ)法為: insert into table_name (‘column1’, null,’column3’) 
思考: 
插入時(shí),值列表中多值、少值行嗎? 
null和‘null’是否一樣? 
, ,和,‘’,行嗎? 
三、更新已存在的數(shù)據(jù). 
1、只更新單獨(dú)一列的數(shù)據(jù) 
update table_name 
set column_name = ‘value’ 
[where condition]; 
2、在一行或多行記錄中更新多個(gè)列 
update table_name 
set column1 =‘value’, 
[column2 = ‘vlaue’]; 
4.從表中刪除數(shù)據(jù) 
從表中刪除一條單獨(dú)的記錄或是被選的多條記錄 
delete from table_name 
where [condition]; 
第四部分.數(shù)據(jù)庫(kù)事務(wù)管理. 
一、事務(wù)控制命令 
事務(wù)控制是對(duì)在關(guān)系數(shù)據(jù)庫(kù)中可能發(fā)生的不同事務(wù)的管理能力. 
用于事務(wù)控制的命令: 
1、commit 命令 
commit 命令是一條事務(wù)命令,用于保存數(shù)據(jù)庫(kù)中由事務(wù)引起的改變.commit 命令保存自從上一條commit或rollback 命令以來(lái)的所有事務(wù). 
語(yǔ)法為:commit [word]; 
2、rollback 命令 
commit 命令是用于撤消那些還沒有保存到數(shù)據(jù)庫(kù)中由事務(wù)的事務(wù)控制命令.rollback 命令只能用于撤消上一條commit命令或rollback命令執(zhí)行后的事務(wù)。 
語(yǔ)法為: rollback [word]; 
3、savepoint 命令 
savepoint 命令保存點(diǎn)是在事務(wù)中,能夠進(jìn)行事務(wù)滾回的點(diǎn),而不需要滾回整個(gè)事務(wù) 
語(yǔ)法為: savepoint point_name; 
rollback to point_name; 
二、事務(wù)控制和數(shù)據(jù)庫(kù)的性能 
糟糕的事務(wù)控制會(huì)破壞數(shù)據(jù)庫(kù)的性能,甚至給數(shù)據(jù)庫(kù)帶來(lái)危害。數(shù)據(jù)庫(kù)反復(fù)的低性能可能是因?yàn)樵诖罅坎迦耄禄騽h除操作中缺乏事務(wù)控制;當(dāng)執(zhí)行大量事務(wù)工作時(shí),應(yīng)頻繁使用commit和rollback命令來(lái)釋放數(shù)據(jù)庫(kù)空間。 
第五部分 數(shù)據(jù)查詢語(yǔ)言dql 
一、基本的select語(yǔ)句 
select [all | * | [distinct] column1,column2…] 
from table1 [,table2…] 
[where conditons ] 
[group by column1, column2,…… 
[having conditions ] 
[order by column1 ,colunm2…] 
二、使用操作符給數(shù)據(jù)分類 
1、 比較操作符(= ,<>, <, >) 
例: 
select * 
from employee_tbl 
where emp_zip=’430079’; 
2、 邏輯操作符(is null , between , in , like , exists , unique , all ,any) 
is null:用于比較帶有null值的值; 
between:用于在一個(gè)范圍中取值; 
in :用于返回在一系列指定的值中的結(jié)果; 
like :取同指定值相似的值(%代表任意個(gè)字符,而_代表一個(gè)); 
exists :查詢是否存在一行符合某條件的數(shù)據(jù); 
unique :測(cè)試是否有重復(fù)數(shù)據(jù); 
all :指數(shù)據(jù)集中的每一個(gè)值; 
any :指數(shù)據(jù)集中的任一個(gè)值; 
例: 
select * 
from employee_tbl 
where emp_salary >all 
(select emp_salary 
from emp_salary 
where emp_city=’武漢’); 
3、 連接符(and , or) 
例: 
select * 
from employee_tbl 
where emp_zip=’430079’ 
or emp_zip=’430074’; 
4、 數(shù)學(xué)運(yùn)算符的組合 
例: 
select emp_salary*12 
from employee_tbl; 
三、統(tǒng)計(jì)查詢結(jié)果數(shù)據(jù) 
1、count:用于統(tǒng)計(jì)行數(shù); 
例: 
select count(emp_id) 
from employee_tbl; 
2、sum:用于返回一組數(shù)據(jù)行中某列值的總和; 
例: 
select sum(emp_salary) 
from employee_tbl; 
3、avg:用于確定一組數(shù)據(jù)的平均值; 
例: 
select avg(emp_salary) 
from employee_tbl; 
4、max:用于返回一組數(shù)據(jù)的最大值; 
例: 
select max(emp_salary) 
from employee_tbl; 
5、min:用于返回一組數(shù)據(jù)的最小值; 
例: 
select min(emp_salary) 
from employee_tbl; 
四、排序和分組 
1、 分組:group by 
例: 
select employee_name ,emp_city 
from employee_tbl 
group by emp_city,employee_name; 
注:如果在select子句中引用的是列名,列名一定也會(huì)用于group by的中; 
不能像order by一樣通過(guò)使用整數(shù)列名來(lái)進(jìn)行排序,除非便用了union,并且列名都各自不同。 
2、 排序:order by 
例: 
select emp_city, count(*) 
from employee_tbl 
group by emp_city 
order by 2,1; 
3、 having子句:跟在group by之后,order by 之前,設(shè)置條件; 
例: 
select emp_city,avg(pay_rate),avg(emp_salary) 
from emp_pay_tbl 
where city <>‘武漢‘ 
group by emp_city 
having avg(emp_salary)>2000; 
第六部分 實(shí)習(xí)題 
1請(qǐng)問sql命令可分為哪六類,每一類都有哪些具體的命令? 
2 請(qǐng)建立以下五個(gè)表格,并用desc命令檢查它們的結(jié)構(gòu)。 
表employee_tbl結(jié)構(gòu) 
emp_id varchar2(9) not null, 
last_name varchar2(15) not null, 
first_name varchar2(15) not null, 
middle_name varchar2(15), 
address varchar2(30) not null, 
city varchar2(15) not null, 
state char(2) not null, 
zip number(5) not null, 
phone char(10), 
pager char(10), 
constraint emp_pk primary key(emp_id) 
表employee_pay_tbl結(jié)構(gòu) 
emp_id varchar2(9) not null, 
position varchar2(15) not null, 
date_hire date, 
pay_rate number(4,2), 
date_last_raise date, 
salary number(8,2), 
bonus number(6,2), 
constraint emp_fk foreign key(emp_id) references employee_tbl(emp_id) 
表customer_tbl結(jié)構(gòu) 
cust_id varchar2(10) not null primary key, 
cust_name varchar2(30) not null, 
cust_address varchar(20) not null, 
cust_city varchar2(15) not null, 
cust_state char(2) not null, 
cust_zip number(5) not null, 
cust_phone number(10), 
cust_fax number(10) 
表orders_tbl結(jié)構(gòu) 
ord_num varchar2(10) not null primary key, 
cust_id varchar2(10) not null, 
prod_id varchar2(10) not null, 
qty number(6) not null, 
ord_date date 
表products_tbl結(jié)構(gòu) 
prod_id varchar2(10) not null primary key, 
prod_desc varchar2(40) not null, 
cost number(6,2) not null