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